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"
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 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
647 g_source_destroy(iwp
->src
);
648 g_source_unref(iwp
->src
);
652 static GSourceFuncs io_watch_poll_funcs
= {
653 .prepare
= io_watch_poll_prepare
,
654 .check
= io_watch_poll_check
,
655 .dispatch
= io_watch_poll_dispatch
,
656 .finalize
= io_watch_poll_finalize
,
659 /* Can only be used for read */
660 static guint
io_add_watch_poll(GIOChannel
*channel
,
661 IOCanReadHandler
*fd_can_read
,
667 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
668 iwp
->fd_can_read
= fd_can_read
;
669 iwp
->opaque
= user_data
;
670 iwp
->channel
= channel
;
671 iwp
->fd_read
= (GSourceFunc
) fd_read
;
674 return g_source_attach(&iwp
->parent
, NULL
);
678 static GIOChannel
*io_channel_from_fd(int fd
)
686 chan
= g_io_channel_unix_new(fd
);
688 g_io_channel_set_encoding(chan
, NULL
, NULL
);
689 g_io_channel_set_buffered(chan
, FALSE
);
695 static GIOChannel
*io_channel_from_socket(int fd
)
704 chan
= g_io_channel_win32_new_socket(fd
);
706 chan
= g_io_channel_unix_new(fd
);
709 g_io_channel_set_encoding(chan
, NULL
, NULL
);
710 g_io_channel_set_buffered(chan
, FALSE
);
715 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
721 while (offset
< len
) {
724 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
725 &bytes_written
, NULL
);
726 if (status
!= G_IO_STATUS_NORMAL
) {
727 if (status
== G_IO_STATUS_AGAIN
) {
728 /* If we've written any data, return a partial write. */
738 } else if (status
== G_IO_STATUS_EOF
) {
742 offset
+= bytes_written
;
750 typedef struct FDCharDriver
{
751 CharDriverState
*chr
;
752 GIOChannel
*fd_in
, *fd_out
;
755 QTAILQ_ENTRY(FDCharDriver
) node
;
758 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
760 FDCharDriver
*s
= chr
->opaque
;
762 return io_channel_send(s
->fd_out
, buf
, len
);
765 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
767 CharDriverState
*chr
= opaque
;
768 FDCharDriver
*s
= chr
->opaque
;
770 uint8_t buf
[READ_BUF_LEN
];
775 if (len
> s
->max_size
) {
782 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
783 len
, &bytes_read
, NULL
);
784 if (status
== G_IO_STATUS_EOF
) {
785 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
788 if (status
== G_IO_STATUS_NORMAL
) {
789 qemu_chr_be_write(chr
, buf
, bytes_read
);
795 static int fd_chr_read_poll(void *opaque
)
797 CharDriverState
*chr
= opaque
;
798 FDCharDriver
*s
= chr
->opaque
;
800 s
->max_size
= qemu_chr_be_can_write(chr
);
804 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
806 FDCharDriver
*s
= chr
->opaque
;
807 return g_io_create_watch(s
->fd_out
, cond
);
810 static void fd_chr_update_read_handler(CharDriverState
*chr
)
812 FDCharDriver
*s
= chr
->opaque
;
815 g_source_remove(s
->fd_in_tag
);
819 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
823 static void fd_chr_close(struct CharDriverState
*chr
)
825 FDCharDriver
*s
= chr
->opaque
;
828 g_source_remove(s
->fd_in_tag
);
833 g_io_channel_unref(s
->fd_in
);
836 g_io_channel_unref(s
->fd_out
);
840 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
843 /* open a character device to a unix fd */
844 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
846 CharDriverState
*chr
;
849 chr
= g_malloc0(sizeof(CharDriverState
));
850 s
= g_malloc0(sizeof(FDCharDriver
));
851 s
->fd_in
= io_channel_from_fd(fd_in
);
852 s
->fd_out
= io_channel_from_fd(fd_out
);
853 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
856 chr
->chr_add_watch
= fd_chr_add_watch
;
857 chr
->chr_write
= fd_chr_write
;
858 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
859 chr
->chr_close
= fd_chr_close
;
861 qemu_chr_be_generic_open(chr
);
866 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
869 char filename_in
[256], filename_out
[256];
870 const char *filename
= opts
->device
;
872 if (filename
== NULL
) {
873 fprintf(stderr
, "chardev: pipe: no filename given\n");
877 snprintf(filename_in
, 256, "%s.in", filename
);
878 snprintf(filename_out
, 256, "%s.out", filename
);
879 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
880 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
881 if (fd_in
< 0 || fd_out
< 0) {
886 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
891 return qemu_chr_open_fd(fd_in
, fd_out
);
894 /* init terminal so that we can grab keys */
895 static struct termios oldtty
;
896 static int old_fd0_flags
;
897 static bool stdio_allow_signal
;
899 static void term_exit(void)
901 tcsetattr (0, TCSANOW
, &oldtty
);
902 fcntl(0, F_SETFL
, old_fd0_flags
);
905 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
911 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
912 |INLCR
|IGNCR
|ICRNL
|IXON
);
913 tty
.c_oflag
|= OPOST
;
914 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
915 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
920 /* if graphical mode, we allow Ctrl-C handling */
921 if (!stdio_allow_signal
)
922 tty
.c_lflag
&= ~ISIG
;
924 tcsetattr (0, TCSANOW
, &tty
);
927 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
933 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
935 CharDriverState
*chr
;
937 if (is_daemonized()) {
938 error_report("cannot use stdio with -daemonize");
941 old_fd0_flags
= fcntl(0, F_GETFL
);
942 tcgetattr (0, &oldtty
);
943 fcntl(0, F_SETFL
, O_NONBLOCK
);
946 chr
= qemu_chr_open_fd(0, 1);
947 chr
->chr_close
= qemu_chr_close_stdio
;
948 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
949 stdio_allow_signal
= display_type
!= DT_NOGRAPHIC
;
950 if (opts
->has_signal
) {
951 stdio_allow_signal
= opts
->signal
;
953 qemu_chr_fe_set_echo(chr
, false);
959 /* Once Solaris has openpty(), this is going to be removed. */
960 static int openpty(int *amaster
, int *aslave
, char *name
,
961 struct termios
*termp
, struct winsize
*winp
)
964 int mfd
= -1, sfd
= -1;
966 *amaster
= *aslave
= -1;
968 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
972 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
975 if ((slave
= ptsname(mfd
)) == NULL
)
978 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
981 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
982 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
990 ioctl(sfd
, TIOCSWINSZ
, winp
);
1001 static void cfmakeraw (struct termios
*termios_p
)
1003 termios_p
->c_iflag
&=
1004 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
1005 termios_p
->c_oflag
&= ~OPOST
;
1006 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
1007 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
1008 termios_p
->c_cflag
|= CS8
;
1010 termios_p
->c_cc
[VMIN
] = 0;
1011 termios_p
->c_cc
[VTIME
] = 0;
1015 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1016 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1017 || defined(__GLIBC__)
1019 #define HAVE_CHARDEV_TTY 1
1030 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1031 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1033 static gboolean
pty_chr_timer(gpointer opaque
)
1035 struct CharDriverState
*chr
= opaque
;
1036 PtyCharDriver
*s
= chr
->opaque
;
1042 /* If we arrive here without polling being cleared due
1043 * read returning -EIO, then we are (re-)connected */
1044 pty_chr_state(chr
, 1);
1049 pty_chr_update_read_handler(chr
);
1055 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1057 PtyCharDriver
*s
= chr
->opaque
;
1060 g_source_remove(s
->timer_tag
);
1065 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1067 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1071 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1073 PtyCharDriver
*s
= chr
->opaque
;
1075 if (!s
->connected
) {
1076 /* guest sends data, check for (re-)connect */
1077 pty_chr_update_read_handler(chr
);
1080 return io_channel_send(s
->fd
, buf
, len
);
1083 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1085 PtyCharDriver
*s
= chr
->opaque
;
1086 return g_io_create_watch(s
->fd
, cond
);
1089 static int pty_chr_read_poll(void *opaque
)
1091 CharDriverState
*chr
= opaque
;
1092 PtyCharDriver
*s
= chr
->opaque
;
1094 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1095 return s
->read_bytes
;
1098 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1100 CharDriverState
*chr
= opaque
;
1101 PtyCharDriver
*s
= chr
->opaque
;
1103 uint8_t buf
[READ_BUF_LEN
];
1107 if (len
> s
->read_bytes
)
1108 len
= s
->read_bytes
;
1111 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1112 if (status
!= G_IO_STATUS_NORMAL
) {
1113 pty_chr_state(chr
, 0);
1116 pty_chr_state(chr
, 1);
1117 qemu_chr_be_write(chr
, buf
, size
);
1122 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1124 PtyCharDriver
*s
= chr
->opaque
;
1127 g_source_remove(s
->fd_tag
);
1130 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1133 * Short timeout here: just need wait long enougth that qemu makes
1134 * it through the poll loop once. When reconnected we want a
1135 * short timeout so we notice it almost instantly. Otherwise
1136 * read() gives us -EIO instantly, making pty_chr_state() reset the
1137 * timeout to the normal (much longer) poll interval before the
1140 pty_chr_rearm_timer(chr
, 10);
1143 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1145 PtyCharDriver
*s
= chr
->opaque
;
1148 g_source_remove(s
->fd_tag
);
1152 /* (re-)connect poll interval for idle guests: once per second.
1153 * We check more frequently in case the guests sends data to
1154 * the virtual device linked to our pty. */
1155 pty_chr_rearm_timer(chr
, 1000);
1158 qemu_chr_be_generic_open(chr
);
1164 static void pty_chr_close(struct CharDriverState
*chr
)
1166 PtyCharDriver
*s
= chr
->opaque
;
1170 g_source_remove(s
->fd_tag
);
1172 fd
= g_io_channel_unix_get_fd(s
->fd
);
1173 g_io_channel_unref(s
->fd
);
1176 g_source_remove(s
->timer_tag
);
1179 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1182 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1185 CharDriverState
*chr
;
1188 int master_fd
, slave_fd
;
1189 #if defined(__OpenBSD__) || defined(__DragonFly__)
1190 char pty_name
[PATH_MAX
];
1191 #define q_ptsname(x) pty_name
1193 char *pty_name
= NULL
;
1194 #define q_ptsname(x) ptsname(x)
1197 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1201 /* Set raw attributes on the pty. */
1202 tcgetattr(slave_fd
, &tty
);
1204 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1207 chr
= g_malloc0(sizeof(CharDriverState
));
1209 chr
->filename
= g_strdup_printf("pty:%s", q_ptsname(master_fd
));
1210 ret
->pty
= g_strdup(q_ptsname(master_fd
));
1211 ret
->has_pty
= true;
1213 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1214 q_ptsname(master_fd
), id
);
1216 s
= g_malloc0(sizeof(PtyCharDriver
));
1218 chr
->chr_write
= pty_chr_write
;
1219 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1220 chr
->chr_close
= pty_chr_close
;
1221 chr
->chr_add_watch
= pty_chr_add_watch
;
1223 s
->fd
= io_channel_from_fd(master_fd
);
1229 static void tty_serial_init(int fd
, int speed
,
1230 int parity
, int data_bits
, int stop_bits
)
1236 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1237 speed
, parity
, data_bits
, stop_bits
);
1239 tcgetattr (fd
, &tty
);
1241 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1242 speed
= speed
* 10 / 11;
1259 /* Non-Posix values follow. They may be unsupported on some systems. */
1261 check_speed(115200);
1263 check_speed(230400);
1266 check_speed(460800);
1269 check_speed(500000);
1272 check_speed(576000);
1275 check_speed(921600);
1278 check_speed(1000000);
1281 check_speed(1152000);
1284 check_speed(1500000);
1287 check_speed(2000000);
1290 check_speed(2500000);
1293 check_speed(3000000);
1296 check_speed(3500000);
1299 check_speed(4000000);
1304 cfsetispeed(&tty
, spd
);
1305 cfsetospeed(&tty
, spd
);
1307 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1308 |INLCR
|IGNCR
|ICRNL
|IXON
);
1309 tty
.c_oflag
|= OPOST
;
1310 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1311 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1332 tty
.c_cflag
|= PARENB
;
1335 tty
.c_cflag
|= PARENB
| PARODD
;
1339 tty
.c_cflag
|= CSTOPB
;
1341 tcsetattr (fd
, TCSANOW
, &tty
);
1344 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1346 FDCharDriver
*s
= chr
->opaque
;
1349 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1351 QEMUSerialSetParams
*ssp
= arg
;
1352 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1353 ssp
->speed
, ssp
->parity
,
1354 ssp
->data_bits
, ssp
->stop_bits
);
1357 case CHR_IOCTL_SERIAL_SET_BREAK
:
1359 int enable
= *(int *)arg
;
1361 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1365 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1368 int *targ
= (int *)arg
;
1369 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1371 if (sarg
& TIOCM_CTS
)
1372 *targ
|= CHR_TIOCM_CTS
;
1373 if (sarg
& TIOCM_CAR
)
1374 *targ
|= CHR_TIOCM_CAR
;
1375 if (sarg
& TIOCM_DSR
)
1376 *targ
|= CHR_TIOCM_DSR
;
1377 if (sarg
& TIOCM_RI
)
1378 *targ
|= CHR_TIOCM_RI
;
1379 if (sarg
& TIOCM_DTR
)
1380 *targ
|= CHR_TIOCM_DTR
;
1381 if (sarg
& TIOCM_RTS
)
1382 *targ
|= CHR_TIOCM_RTS
;
1385 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1387 int sarg
= *(int *)arg
;
1389 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1390 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1391 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1392 if (sarg
& CHR_TIOCM_CTS
)
1394 if (sarg
& CHR_TIOCM_CAR
)
1396 if (sarg
& CHR_TIOCM_DSR
)
1398 if (sarg
& CHR_TIOCM_RI
)
1400 if (sarg
& CHR_TIOCM_DTR
)
1402 if (sarg
& CHR_TIOCM_RTS
)
1404 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1413 static void qemu_chr_close_tty(CharDriverState
*chr
)
1415 FDCharDriver
*s
= chr
->opaque
;
1419 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1429 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1431 CharDriverState
*chr
;
1433 tty_serial_init(fd
, 115200, 'N', 8, 1);
1434 chr
= qemu_chr_open_fd(fd
, fd
);
1435 chr
->chr_ioctl
= tty_serial_ioctl
;
1436 chr
->chr_close
= qemu_chr_close_tty
;
1439 #endif /* __linux__ || __sun__ */
1441 #if defined(__linux__)
1443 #define HAVE_CHARDEV_PARPORT 1
1448 } ParallelCharDriver
;
1450 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1452 if (s
->mode
!= mode
) {
1454 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1461 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1463 ParallelCharDriver
*drv
= chr
->opaque
;
1468 case CHR_IOCTL_PP_READ_DATA
:
1469 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1471 *(uint8_t *)arg
= b
;
1473 case CHR_IOCTL_PP_WRITE_DATA
:
1474 b
= *(uint8_t *)arg
;
1475 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1478 case CHR_IOCTL_PP_READ_CONTROL
:
1479 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1481 /* Linux gives only the lowest bits, and no way to know data
1482 direction! For better compatibility set the fixed upper
1484 *(uint8_t *)arg
= b
| 0xc0;
1486 case CHR_IOCTL_PP_WRITE_CONTROL
:
1487 b
= *(uint8_t *)arg
;
1488 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1491 case CHR_IOCTL_PP_READ_STATUS
:
1492 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1494 *(uint8_t *)arg
= b
;
1496 case CHR_IOCTL_PP_DATA_DIR
:
1497 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1500 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1501 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1502 struct ParallelIOArg
*parg
= arg
;
1503 int n
= read(fd
, parg
->buffer
, parg
->count
);
1504 if (n
!= parg
->count
) {
1509 case CHR_IOCTL_PP_EPP_READ
:
1510 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1511 struct ParallelIOArg
*parg
= arg
;
1512 int n
= read(fd
, parg
->buffer
, parg
->count
);
1513 if (n
!= parg
->count
) {
1518 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1519 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1520 struct ParallelIOArg
*parg
= arg
;
1521 int n
= write(fd
, parg
->buffer
, parg
->count
);
1522 if (n
!= parg
->count
) {
1527 case CHR_IOCTL_PP_EPP_WRITE
:
1528 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1529 struct ParallelIOArg
*parg
= arg
;
1530 int n
= write(fd
, parg
->buffer
, parg
->count
);
1531 if (n
!= parg
->count
) {
1542 static void pp_close(CharDriverState
*chr
)
1544 ParallelCharDriver
*drv
= chr
->opaque
;
1547 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1548 ioctl(fd
, PPRELEASE
);
1551 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1554 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1556 CharDriverState
*chr
;
1557 ParallelCharDriver
*drv
;
1559 if (ioctl(fd
, PPCLAIM
) < 0) {
1564 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1566 drv
->mode
= IEEE1284_MODE_COMPAT
;
1568 chr
= g_malloc0(sizeof(CharDriverState
));
1569 chr
->chr_write
= null_chr_write
;
1570 chr
->chr_ioctl
= pp_ioctl
;
1571 chr
->chr_close
= pp_close
;
1574 qemu_chr_be_generic_open(chr
);
1578 #endif /* __linux__ */
1580 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1582 #define HAVE_CHARDEV_PARPORT 1
1584 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1586 int fd
= (int)(intptr_t)chr
->opaque
;
1590 case CHR_IOCTL_PP_READ_DATA
:
1591 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1593 *(uint8_t *)arg
= b
;
1595 case CHR_IOCTL_PP_WRITE_DATA
:
1596 b
= *(uint8_t *)arg
;
1597 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1600 case CHR_IOCTL_PP_READ_CONTROL
:
1601 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1603 *(uint8_t *)arg
= b
;
1605 case CHR_IOCTL_PP_WRITE_CONTROL
:
1606 b
= *(uint8_t *)arg
;
1607 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1610 case CHR_IOCTL_PP_READ_STATUS
:
1611 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1613 *(uint8_t *)arg
= b
;
1621 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1623 CharDriverState
*chr
;
1625 chr
= g_malloc0(sizeof(CharDriverState
));
1626 chr
->opaque
= (void *)(intptr_t)fd
;
1627 chr
->chr_write
= null_chr_write
;
1628 chr
->chr_ioctl
= pp_ioctl
;
1637 HANDLE hcom
, hrecv
, hsend
;
1638 OVERLAPPED orecv
, osend
;
1645 HANDLE hInputReadyEvent
;
1646 HANDLE hInputDoneEvent
;
1647 HANDLE hInputThread
;
1648 uint8_t win_stdio_buf
;
1649 } WinStdioCharState
;
1651 #define NSENDBUF 2048
1652 #define NRECVBUF 2048
1653 #define MAXCONNECT 1
1654 #define NTIMEOUT 5000
1656 static int win_chr_poll(void *opaque
);
1657 static int win_chr_pipe_poll(void *opaque
);
1659 static void win_chr_close(CharDriverState
*chr
)
1661 WinCharState
*s
= chr
->opaque
;
1664 CloseHandle(s
->hsend
);
1668 CloseHandle(s
->hrecv
);
1672 CloseHandle(s
->hcom
);
1676 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1678 qemu_del_polling_cb(win_chr_poll
, chr
);
1680 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1683 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1685 WinCharState
*s
= chr
->opaque
;
1687 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1692 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1694 fprintf(stderr
, "Failed CreateEvent\n");
1697 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1699 fprintf(stderr
, "Failed CreateEvent\n");
1703 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1704 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1705 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1706 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1711 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1712 fprintf(stderr
, "Failed SetupComm\n");
1716 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1717 size
= sizeof(COMMCONFIG
);
1718 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1719 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1720 CommConfigDialog(filename
, NULL
, &comcfg
);
1722 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1723 fprintf(stderr
, "Failed SetCommState\n");
1727 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1728 fprintf(stderr
, "Failed SetCommMask\n");
1732 cto
.ReadIntervalTimeout
= MAXDWORD
;
1733 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1734 fprintf(stderr
, "Failed SetCommTimeouts\n");
1738 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1739 fprintf(stderr
, "Failed ClearCommError\n");
1742 qemu_add_polling_cb(win_chr_poll
, chr
);
1750 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1752 WinCharState
*s
= chr
->opaque
;
1753 DWORD len
, ret
, size
, err
;
1756 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1757 s
->osend
.hEvent
= s
->hsend
;
1760 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1762 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1764 err
= GetLastError();
1765 if (err
== ERROR_IO_PENDING
) {
1766 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1784 static int win_chr_read_poll(CharDriverState
*chr
)
1786 WinCharState
*s
= chr
->opaque
;
1788 s
->max_size
= qemu_chr_be_can_write(chr
);
1792 static void win_chr_readfile(CharDriverState
*chr
)
1794 WinCharState
*s
= chr
->opaque
;
1796 uint8_t buf
[READ_BUF_LEN
];
1799 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1800 s
->orecv
.hEvent
= s
->hrecv
;
1801 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1803 err
= GetLastError();
1804 if (err
== ERROR_IO_PENDING
) {
1805 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1810 qemu_chr_be_write(chr
, buf
, size
);
1814 static void win_chr_read(CharDriverState
*chr
)
1816 WinCharState
*s
= chr
->opaque
;
1818 if (s
->len
> s
->max_size
)
1819 s
->len
= s
->max_size
;
1823 win_chr_readfile(chr
);
1826 static int win_chr_poll(void *opaque
)
1828 CharDriverState
*chr
= opaque
;
1829 WinCharState
*s
= chr
->opaque
;
1833 ClearCommError(s
->hcom
, &comerr
, &status
);
1834 if (status
.cbInQue
> 0) {
1835 s
->len
= status
.cbInQue
;
1836 win_chr_read_poll(chr
);
1843 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1845 CharDriverState
*chr
;
1848 chr
= g_malloc0(sizeof(CharDriverState
));
1849 s
= g_malloc0(sizeof(WinCharState
));
1851 chr
->chr_write
= win_chr_write
;
1852 chr
->chr_close
= win_chr_close
;
1854 if (win_chr_init(chr
, filename
) < 0) {
1859 qemu_chr_be_generic_open(chr
);
1863 static int win_chr_pipe_poll(void *opaque
)
1865 CharDriverState
*chr
= opaque
;
1866 WinCharState
*s
= chr
->opaque
;
1869 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1872 win_chr_read_poll(chr
);
1879 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1881 WinCharState
*s
= chr
->opaque
;
1889 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1891 fprintf(stderr
, "Failed CreateEvent\n");
1894 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1896 fprintf(stderr
, "Failed CreateEvent\n");
1900 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1901 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1902 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1904 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1905 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1906 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1911 ZeroMemory(&ov
, sizeof(ov
));
1912 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1913 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1915 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1919 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1921 fprintf(stderr
, "Failed GetOverlappedResult\n");
1923 CloseHandle(ov
.hEvent
);
1930 CloseHandle(ov
.hEvent
);
1933 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1942 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1944 const char *filename
= opts
->device
;
1945 CharDriverState
*chr
;
1948 chr
= g_malloc0(sizeof(CharDriverState
));
1949 s
= g_malloc0(sizeof(WinCharState
));
1951 chr
->chr_write
= win_chr_write
;
1952 chr
->chr_close
= win_chr_close
;
1954 if (win_chr_pipe_init(chr
, filename
) < 0) {
1959 qemu_chr_be_generic_open(chr
);
1963 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1965 CharDriverState
*chr
;
1968 chr
= g_malloc0(sizeof(CharDriverState
));
1969 s
= g_malloc0(sizeof(WinCharState
));
1972 chr
->chr_write
= win_chr_write
;
1973 qemu_chr_be_generic_open(chr
);
1977 static CharDriverState
*qemu_chr_open_win_con(void)
1979 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1982 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1984 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1991 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2001 static void win_stdio_wait_func(void *opaque
)
2003 CharDriverState
*chr
= opaque
;
2004 WinStdioCharState
*stdio
= chr
->opaque
;
2005 INPUT_RECORD buf
[4];
2010 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
2014 /* Avoid error storm */
2015 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2019 for (i
= 0; i
< dwSize
; i
++) {
2020 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2022 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2024 if (kev
->uChar
.AsciiChar
!= 0) {
2025 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2026 if (qemu_chr_be_can_write(chr
)) {
2027 uint8_t c
= kev
->uChar
.AsciiChar
;
2028 qemu_chr_be_write(chr
, &c
, 1);
2036 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2038 CharDriverState
*chr
= param
;
2039 WinStdioCharState
*stdio
= chr
->opaque
;
2045 /* Wait for one byte */
2046 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2048 /* Exit in case of error, continue if nothing read */
2056 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2057 if (stdio
->win_stdio_buf
== '\r') {
2061 /* Signal the main thread and wait until the byte was eaten */
2062 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2065 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2071 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2075 static void win_stdio_thread_wait_func(void *opaque
)
2077 CharDriverState
*chr
= opaque
;
2078 WinStdioCharState
*stdio
= chr
->opaque
;
2080 if (qemu_chr_be_can_write(chr
)) {
2081 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2084 SetEvent(stdio
->hInputDoneEvent
);
2087 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2089 WinStdioCharState
*stdio
= chr
->opaque
;
2092 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2095 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2097 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2101 static void win_stdio_close(CharDriverState
*chr
)
2103 WinStdioCharState
*stdio
= chr
->opaque
;
2105 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2106 CloseHandle(stdio
->hInputReadyEvent
);
2108 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2109 CloseHandle(stdio
->hInputDoneEvent
);
2111 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2112 TerminateThread(stdio
->hInputThread
, 0);
2115 g_free(chr
->opaque
);
2119 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2121 CharDriverState
*chr
;
2122 WinStdioCharState
*stdio
;
2126 chr
= g_malloc0(sizeof(CharDriverState
));
2127 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2129 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2130 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2131 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2135 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2137 chr
->opaque
= stdio
;
2138 chr
->chr_write
= win_stdio_write
;
2139 chr
->chr_close
= win_stdio_close
;
2142 if (qemu_add_wait_object(stdio
->hStdIn
,
2143 win_stdio_wait_func
, chr
)) {
2144 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2149 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2150 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2151 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2154 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2155 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2156 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2157 fprintf(stderr
, "cannot create stdio thread or event\n");
2160 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2161 win_stdio_thread_wait_func
, chr
)) {
2162 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2166 dwMode
|= ENABLE_LINE_INPUT
;
2169 /* set the terminal in raw mode */
2170 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2171 dwMode
|= ENABLE_PROCESSED_INPUT
;
2174 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2176 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2177 qemu_chr_fe_set_echo(chr
, false);
2181 #endif /* !_WIN32 */
2184 /***********************************************************/
2185 /* UDP Net console */
2191 uint8_t buf
[READ_BUF_LEN
];
2197 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2199 NetCharDriver
*s
= chr
->opaque
;
2200 gsize bytes_written
;
2203 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2204 if (status
== G_IO_STATUS_EOF
) {
2206 } else if (status
!= G_IO_STATUS_NORMAL
) {
2210 return bytes_written
;
2213 static int udp_chr_read_poll(void *opaque
)
2215 CharDriverState
*chr
= opaque
;
2216 NetCharDriver
*s
= chr
->opaque
;
2218 s
->max_size
= qemu_chr_be_can_write(chr
);
2220 /* If there were any stray characters in the queue process them
2223 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2224 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2226 s
->max_size
= qemu_chr_be_can_write(chr
);
2231 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2233 CharDriverState
*chr
= opaque
;
2234 NetCharDriver
*s
= chr
->opaque
;
2235 gsize bytes_read
= 0;
2238 if (s
->max_size
== 0)
2240 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2242 s
->bufcnt
= bytes_read
;
2243 s
->bufptr
= s
->bufcnt
;
2244 if (status
!= G_IO_STATUS_NORMAL
) {
2249 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2250 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2252 s
->max_size
= qemu_chr_be_can_write(chr
);
2258 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2260 NetCharDriver
*s
= chr
->opaque
;
2263 g_source_remove(s
->tag
);
2268 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2272 static void udp_chr_close(CharDriverState
*chr
)
2274 NetCharDriver
*s
= chr
->opaque
;
2276 g_source_remove(s
->tag
);
2279 g_io_channel_unref(s
->chan
);
2283 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2286 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2288 CharDriverState
*chr
= NULL
;
2289 NetCharDriver
*s
= NULL
;
2291 chr
= g_malloc0(sizeof(CharDriverState
));
2292 s
= g_malloc0(sizeof(NetCharDriver
));
2295 s
->chan
= io_channel_from_socket(s
->fd
);
2299 chr
->chr_write
= udp_chr_write
;
2300 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2301 chr
->chr_close
= udp_chr_close
;
2305 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2307 Error
*local_err
= NULL
;
2310 fd
= inet_dgram_opts(opts
, &local_err
);
2314 return qemu_chr_open_udp_fd(fd
);
2317 /***********************************************************/
2318 /* TCP Net console */
2322 GIOChannel
*chan
, *listen_chan
;
2323 guint tag
, listen_tag
;
2333 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2335 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2337 TCPCharDriver
*s
= chr
->opaque
;
2339 return io_channel_send(s
->chan
, buf
, len
);
2341 /* XXX: indicate an error ? */
2346 static int tcp_chr_read_poll(void *opaque
)
2348 CharDriverState
*chr
= opaque
;
2349 TCPCharDriver
*s
= chr
->opaque
;
2352 s
->max_size
= qemu_chr_be_can_write(chr
);
2357 #define IAC_BREAK 243
2358 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2360 uint8_t *buf
, int *size
)
2362 /* Handle any telnet client's basic IAC options to satisfy char by
2363 * char mode with no echo. All IAC options will be removed from
2364 * the buf and the do_telnetopt variable will be used to track the
2365 * state of the width of the IAC information.
2367 * IAC commands come in sets of 3 bytes with the exception of the
2368 * "IAC BREAK" command and the double IAC.
2374 for (i
= 0; i
< *size
; i
++) {
2375 if (s
->do_telnetopt
> 1) {
2376 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2377 /* Double IAC means send an IAC */
2381 s
->do_telnetopt
= 1;
2383 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2384 /* Handle IAC break commands by sending a serial break */
2385 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2390 if (s
->do_telnetopt
>= 4) {
2391 s
->do_telnetopt
= 1;
2394 if ((unsigned char)buf
[i
] == IAC
) {
2395 s
->do_telnetopt
= 2;
2406 static int tcp_get_msgfd(CharDriverState
*chr
)
2408 TCPCharDriver
*s
= chr
->opaque
;
2415 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2417 TCPCharDriver
*s
= chr
->opaque
;
2418 struct cmsghdr
*cmsg
;
2420 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2423 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2424 cmsg
->cmsg_level
!= SOL_SOCKET
||
2425 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2428 fd
= *((int *)CMSG_DATA(cmsg
));
2432 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2435 #ifndef MSG_CMSG_CLOEXEC
2436 qemu_set_cloexec(fd
);
2444 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2446 TCPCharDriver
*s
= chr
->opaque
;
2447 struct msghdr msg
= { NULL
, };
2448 struct iovec iov
[1];
2450 struct cmsghdr cmsg
;
2451 char control
[CMSG_SPACE(sizeof(int))];
2456 iov
[0].iov_base
= buf
;
2457 iov
[0].iov_len
= len
;
2461 msg
.msg_control
= &msg_control
;
2462 msg
.msg_controllen
= sizeof(msg_control
);
2464 #ifdef MSG_CMSG_CLOEXEC
2465 flags
|= MSG_CMSG_CLOEXEC
;
2467 ret
= recvmsg(s
->fd
, &msg
, flags
);
2468 if (ret
> 0 && s
->is_unix
) {
2469 unix_process_msgfd(chr
, &msg
);
2475 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2477 TCPCharDriver
*s
= chr
->opaque
;
2478 return qemu_recv(s
->fd
, buf
, len
, 0);
2482 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2484 TCPCharDriver
*s
= chr
->opaque
;
2485 return g_io_create_watch(s
->chan
, cond
);
2488 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2490 CharDriverState
*chr
= opaque
;
2491 TCPCharDriver
*s
= chr
->opaque
;
2492 uint8_t buf
[READ_BUF_LEN
];
2495 if (!s
->connected
|| s
->max_size
<= 0) {
2499 if (len
> s
->max_size
)
2501 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2503 /* connection closed */
2505 if (s
->listen_chan
) {
2506 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2508 g_source_remove(s
->tag
);
2510 g_io_channel_unref(s
->chan
);
2514 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2515 } else if (size
> 0) {
2516 if (s
->do_telnetopt
)
2517 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2519 qemu_chr_be_write(chr
, buf
, size
);
2526 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2528 return qemu_chr_open_fd(eventfd
, eventfd
);
2532 static void tcp_chr_connect(void *opaque
)
2534 CharDriverState
*chr
= opaque
;
2535 TCPCharDriver
*s
= chr
->opaque
;
2539 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2541 qemu_chr_be_generic_open(chr
);
2544 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2545 static void tcp_chr_telnet_init(int fd
)
2548 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2549 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2550 send(fd
, (char *)buf
, 3, 0);
2551 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2552 send(fd
, (char *)buf
, 3, 0);
2553 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2554 send(fd
, (char *)buf
, 3, 0);
2555 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2556 send(fd
, (char *)buf
, 3, 0);
2559 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2561 TCPCharDriver
*s
= chr
->opaque
;
2565 qemu_set_nonblock(fd
);
2567 socket_set_nodelay(fd
);
2569 s
->chan
= io_channel_from_socket(fd
);
2570 g_source_remove(s
->listen_tag
);
2572 tcp_chr_connect(chr
);
2577 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2579 CharDriverState
*chr
= opaque
;
2580 TCPCharDriver
*s
= chr
->opaque
;
2581 struct sockaddr_in saddr
;
2583 struct sockaddr_un uaddr
;
2585 struct sockaddr
*addr
;
2592 len
= sizeof(uaddr
);
2593 addr
= (struct sockaddr
*)&uaddr
;
2597 len
= sizeof(saddr
);
2598 addr
= (struct sockaddr
*)&saddr
;
2600 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2601 if (fd
< 0 && errno
!= EINTR
) {
2603 } else if (fd
>= 0) {
2604 if (s
->do_telnetopt
)
2605 tcp_chr_telnet_init(fd
);
2609 if (tcp_chr_add_client(chr
, fd
) < 0)
2615 static void tcp_chr_close(CharDriverState
*chr
)
2617 TCPCharDriver
*s
= chr
->opaque
;
2620 g_source_remove(s
->tag
);
2623 g_io_channel_unref(s
->chan
);
2627 if (s
->listen_fd
>= 0) {
2628 if (s
->listen_tag
) {
2629 g_source_remove(s
->listen_tag
);
2631 if (s
->listen_chan
) {
2632 g_io_channel_unref(s
->listen_chan
);
2634 closesocket(s
->listen_fd
);
2637 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2640 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2641 bool is_listen
, bool is_telnet
,
2642 bool is_waitconnect
,
2645 CharDriverState
*chr
= NULL
;
2646 TCPCharDriver
*s
= NULL
;
2647 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2648 const char *left
= "", *right
= "";
2649 struct sockaddr_storage ss
;
2650 socklen_t ss_len
= sizeof(ss
);
2652 memset(&ss
, 0, ss_len
);
2653 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2654 error_setg(errp
, "getsockname: %s", strerror(errno
));
2658 chr
= g_malloc0(sizeof(CharDriverState
));
2659 s
= g_malloc0(sizeof(TCPCharDriver
));
2666 chr
->filename
= g_malloc(256);
2667 switch (ss
.ss_family
) {
2671 snprintf(chr
->filename
, 256, "unix:%s%s",
2672 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2673 is_listen
? ",server" : "");
2681 s
->do_nodelay
= do_nodelay
;
2682 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2683 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2684 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2685 is_telnet
? "telnet" : "tcp",
2686 left
, host
, right
, serv
,
2687 is_listen
? ",server" : "");
2692 chr
->chr_write
= tcp_chr_write
;
2693 chr
->chr_close
= tcp_chr_close
;
2694 chr
->get_msgfd
= tcp_get_msgfd
;
2695 chr
->chr_add_client
= tcp_chr_add_client
;
2696 chr
->chr_add_watch
= tcp_chr_add_watch
;
2700 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2701 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2703 s
->do_telnetopt
= 1;
2708 socket_set_nodelay(fd
);
2709 s
->chan
= io_channel_from_socket(s
->fd
);
2710 tcp_chr_connect(chr
);
2713 if (is_listen
&& is_waitconnect
) {
2714 printf("QEMU waiting for connection on: %s\n",
2716 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2717 qemu_set_nonblock(s
->listen_fd
);
2722 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2724 CharDriverState
*chr
= NULL
;
2725 Error
*local_err
= NULL
;
2733 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2734 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2735 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2736 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2737 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2743 fd
= unix_listen_opts(opts
, &local_err
);
2745 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2749 fd
= inet_listen_opts(opts
, 0, &local_err
);
2751 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2758 if (!is_waitconnect
)
2759 qemu_set_nonblock(fd
);
2761 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2762 is_waitconnect
, &local_err
);
2763 if (error_is_set(&local_err
)) {
2771 qerror_report_err(local_err
);
2772 error_free(local_err
);
2778 g_free(chr
->opaque
);
2784 /*********************************************************/
2785 /* Ring buffer chardev */
2792 } RingBufCharDriver
;
2794 static size_t ringbuf_count(const CharDriverState
*chr
)
2796 const RingBufCharDriver
*d
= chr
->opaque
;
2798 return d
->prod
- d
->cons
;
2801 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2803 RingBufCharDriver
*d
= chr
->opaque
;
2806 if (!buf
|| (len
< 0)) {
2810 for (i
= 0; i
< len
; i
++ ) {
2811 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2812 if (d
->prod
- d
->cons
> d
->size
) {
2813 d
->cons
= d
->prod
- d
->size
;
2820 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2822 RingBufCharDriver
*d
= chr
->opaque
;
2825 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2826 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2832 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2834 RingBufCharDriver
*d
= chr
->opaque
;
2841 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2844 CharDriverState
*chr
;
2845 RingBufCharDriver
*d
;
2847 chr
= g_malloc0(sizeof(CharDriverState
));
2848 d
= g_malloc(sizeof(*d
));
2850 d
->size
= opts
->has_size
? opts
->size
: 65536;
2852 /* The size must be power of 2 */
2853 if (d
->size
& (d
->size
- 1)) {
2854 error_setg(errp
, "size of ringbuf chardev must be power of two");
2860 d
->cbuf
= g_malloc0(d
->size
);
2863 chr
->chr_write
= ringbuf_chr_write
;
2864 chr
->chr_close
= ringbuf_chr_close
;
2874 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2876 return chr
->chr_write
== ringbuf_chr_write
;
2879 void qmp_ringbuf_write(const char *device
, const char *data
,
2880 bool has_format
, enum DataFormat format
,
2883 CharDriverState
*chr
;
2884 const uint8_t *write_data
;
2888 chr
= qemu_chr_find(device
);
2890 error_setg(errp
, "Device '%s' not found", device
);
2894 if (!chr_is_ringbuf(chr
)) {
2895 error_setg(errp
,"%s is not a ringbuf device", device
);
2899 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2900 write_data
= g_base64_decode(data
, &write_count
);
2902 write_data
= (uint8_t *)data
;
2903 write_count
= strlen(data
);
2906 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2908 if (write_data
!= (uint8_t *)data
) {
2909 g_free((void *)write_data
);
2913 error_setg(errp
, "Failed to write to device %s", device
);
2918 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2919 bool has_format
, enum DataFormat format
,
2922 CharDriverState
*chr
;
2927 chr
= qemu_chr_find(device
);
2929 error_setg(errp
, "Device '%s' not found", device
);
2933 if (!chr_is_ringbuf(chr
)) {
2934 error_setg(errp
,"%s is not a ringbuf device", device
);
2939 error_setg(errp
, "size must be greater than zero");
2943 count
= ringbuf_count(chr
);
2944 size
= size
> count
? count
: size
;
2945 read_data
= g_malloc(size
+ 1);
2947 ringbuf_chr_read(chr
, read_data
, size
);
2949 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2950 data
= g_base64_encode(read_data
, size
);
2954 * FIXME should read only complete, valid UTF-8 characters up
2955 * to @size bytes. Invalid sequences should be replaced by a
2956 * suitable replacement character. Except when (and only
2957 * when) ring buffer lost characters since last read, initial
2958 * continuation characters should be dropped.
2960 read_data
[size
] = 0;
2961 data
= (char *)read_data
;
2967 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2969 char host
[65], port
[33], width
[8], height
[8];
2973 Error
*local_err
= NULL
;
2975 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2976 if (error_is_set(&local_err
)) {
2977 qerror_report_err(local_err
);
2978 error_free(local_err
);
2982 if (strstart(filename
, "mon:", &p
)) {
2984 qemu_opt_set(opts
, "mux", "on");
2987 if (strcmp(filename
, "null") == 0 ||
2988 strcmp(filename
, "pty") == 0 ||
2989 strcmp(filename
, "msmouse") == 0 ||
2990 strcmp(filename
, "braille") == 0 ||
2991 strcmp(filename
, "stdio") == 0) {
2992 qemu_opt_set(opts
, "backend", filename
);
2995 if (strstart(filename
, "vc", &p
)) {
2996 qemu_opt_set(opts
, "backend", "vc");
2998 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3000 qemu_opt_set(opts
, "width", width
);
3001 qemu_opt_set(opts
, "height", height
);
3002 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3004 qemu_opt_set(opts
, "cols", width
);
3005 qemu_opt_set(opts
, "rows", height
);
3012 if (strcmp(filename
, "con:") == 0) {
3013 qemu_opt_set(opts
, "backend", "console");
3016 if (strstart(filename
, "COM", NULL
)) {
3017 qemu_opt_set(opts
, "backend", "serial");
3018 qemu_opt_set(opts
, "path", filename
);
3021 if (strstart(filename
, "file:", &p
)) {
3022 qemu_opt_set(opts
, "backend", "file");
3023 qemu_opt_set(opts
, "path", p
);
3026 if (strstart(filename
, "pipe:", &p
)) {
3027 qemu_opt_set(opts
, "backend", "pipe");
3028 qemu_opt_set(opts
, "path", p
);
3031 if (strstart(filename
, "tcp:", &p
) ||
3032 strstart(filename
, "telnet:", &p
)) {
3033 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3035 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3038 qemu_opt_set(opts
, "backend", "socket");
3039 qemu_opt_set(opts
, "host", host
);
3040 qemu_opt_set(opts
, "port", port
);
3041 if (p
[pos
] == ',') {
3042 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3045 if (strstart(filename
, "telnet:", &p
))
3046 qemu_opt_set(opts
, "telnet", "on");
3049 if (strstart(filename
, "udp:", &p
)) {
3050 qemu_opt_set(opts
, "backend", "udp");
3051 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3053 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3057 qemu_opt_set(opts
, "host", host
);
3058 qemu_opt_set(opts
, "port", port
);
3059 if (p
[pos
] == '@') {
3061 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3063 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3067 qemu_opt_set(opts
, "localaddr", host
);
3068 qemu_opt_set(opts
, "localport", port
);
3072 if (strstart(filename
, "unix:", &p
)) {
3073 qemu_opt_set(opts
, "backend", "socket");
3074 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3078 if (strstart(filename
, "/dev/parport", NULL
) ||
3079 strstart(filename
, "/dev/ppi", NULL
)) {
3080 qemu_opt_set(opts
, "backend", "parport");
3081 qemu_opt_set(opts
, "path", filename
);
3084 if (strstart(filename
, "/dev/", NULL
)) {
3085 qemu_opt_set(opts
, "backend", "tty");
3086 qemu_opt_set(opts
, "path", filename
);
3091 qemu_opts_del(opts
);
3095 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3098 const char *path
= qemu_opt_get(opts
, "path");
3101 error_setg(errp
, "chardev: file: no filename given");
3104 backend
->file
= g_new0(ChardevFile
, 1);
3105 backend
->file
->out
= g_strdup(path
);
3108 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3111 backend
->stdio
= g_new0(ChardevStdio
, 1);
3112 backend
->stdio
->has_signal
= true;
3113 backend
->stdio
->signal
=
3114 qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
);
3117 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3120 const char *device
= qemu_opt_get(opts
, "path");
3122 if (device
== NULL
) {
3123 error_setg(errp
, "chardev: serial/tty: no device path given");
3126 backend
->serial
= g_new0(ChardevHostdev
, 1);
3127 backend
->serial
->device
= g_strdup(device
);
3130 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3133 const char *device
= qemu_opt_get(opts
, "path");
3135 if (device
== NULL
) {
3136 error_setg(errp
, "chardev: parallel: no device path given");
3139 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3140 backend
->parallel
->device
= g_strdup(device
);
3143 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3146 const char *device
= qemu_opt_get(opts
, "path");
3148 if (device
== NULL
) {
3149 error_setg(errp
, "chardev: pipe: no device path given");
3152 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3153 backend
->pipe
->device
= g_strdup(device
);
3156 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3161 backend
->memory
= g_new0(ChardevRingbuf
, 1);
3163 val
= qemu_opt_get_number(opts
, "size", 0);
3165 backend
->memory
->has_size
= true;
3166 backend
->memory
->size
= val
;
3170 typedef struct CharDriver
{
3173 CharDriverState
*(*open
)(QemuOpts
*opts
);
3174 /* new, qapi-based */
3176 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3179 static GSList
*backends
;
3181 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3185 s
= g_malloc0(sizeof(*s
));
3186 s
->name
= g_strdup(name
);
3189 backends
= g_slist_append(backends
, s
);
3192 void register_char_driver_qapi(const char *name
, int kind
,
3193 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3197 s
= g_malloc0(sizeof(*s
));
3198 s
->name
= g_strdup(name
);
3202 backends
= g_slist_append(backends
, s
);
3205 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3206 void (*init
)(struct CharDriverState
*s
),
3210 CharDriverState
*chr
;
3213 if (qemu_opts_id(opts
) == NULL
) {
3214 error_setg(errp
, "chardev: no id specified");
3218 if (qemu_opt_get(opts
, "backend") == NULL
) {
3219 error_setg(errp
, "chardev: \"%s\" missing backend",
3220 qemu_opts_id(opts
));
3223 for (i
= backends
; i
; i
= i
->next
) {
3226 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3231 error_setg(errp
, "chardev: backend \"%s\" not found",
3232 qemu_opt_get(opts
, "backend"));
3237 /* using new, qapi init */
3238 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3239 ChardevReturn
*ret
= NULL
;
3240 const char *id
= qemu_opts_id(opts
);
3241 const char *bid
= NULL
;
3243 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3244 bid
= g_strdup_printf("%s-base", id
);
3248 backend
->kind
= cd
->kind
;
3250 cd
->parse(opts
, backend
, errp
);
3251 if (error_is_set(errp
)) {
3255 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3256 if (error_is_set(errp
)) {
3261 qapi_free_ChardevBackend(backend
);
3262 qapi_free_ChardevReturn(ret
);
3263 backend
= g_new0(ChardevBackend
, 1);
3264 backend
->mux
= g_new0(ChardevMux
, 1);
3265 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3266 backend
->mux
->chardev
= g_strdup(bid
);
3267 ret
= qmp_chardev_add(id
, backend
, errp
);
3268 if (error_is_set(errp
)) {
3273 chr
= qemu_chr_find(id
);
3276 qapi_free_ChardevBackend(backend
);
3277 qapi_free_ChardevReturn(ret
);
3281 chr
= cd
->open(opts
);
3283 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3284 qemu_opt_get(opts
, "backend"));
3289 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3291 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3293 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3294 CharDriverState
*base
= chr
;
3295 int len
= strlen(qemu_opts_id(opts
)) + 6;
3296 base
->label
= g_malloc(len
);
3297 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3298 chr
= qemu_chr_open_mux(base
);
3299 chr
->filename
= base
->filename
;
3300 chr
->avail_connections
= MAX_MUX
;
3301 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3303 chr
->avail_connections
= 1;
3305 chr
->label
= g_strdup(qemu_opts_id(opts
));
3310 qemu_opts_del(opts
);
3314 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3317 CharDriverState
*chr
;
3321 if (strstart(filename
, "chardev:", &p
)) {
3322 return qemu_chr_find(p
);
3325 opts
= qemu_chr_parse_compat(label
, filename
);
3329 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3330 if (error_is_set(&err
)) {
3331 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3334 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3335 qemu_chr_fe_claim_no_fail(chr
);
3336 monitor_init(chr
, MONITOR_USE_READLINE
);
3341 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3343 if (chr
->chr_set_echo
) {
3344 chr
->chr_set_echo(chr
, echo
);
3348 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3350 if (chr
->fe_open
== fe_open
) {
3353 chr
->fe_open
= fe_open
;
3354 if (chr
->chr_set_fe_open
) {
3355 chr
->chr_set_fe_open(chr
, fe_open
);
3359 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3360 GIOFunc func
, void *user_data
)
3365 if (s
->chr_add_watch
== NULL
) {
3369 src
= s
->chr_add_watch(s
, cond
);
3370 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3371 tag
= g_source_attach(src
, NULL
);
3372 g_source_unref(src
);
3377 int qemu_chr_fe_claim(CharDriverState
*s
)
3379 if (s
->avail_connections
< 1) {
3382 s
->avail_connections
--;
3386 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3388 if (qemu_chr_fe_claim(s
) != 0) {
3389 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3390 __func__
, s
->label
);
3395 void qemu_chr_fe_release(CharDriverState
*s
)
3397 s
->avail_connections
++;
3400 void qemu_chr_delete(CharDriverState
*chr
)
3402 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3403 if (chr
->chr_close
) {
3404 chr
->chr_close(chr
);
3406 g_free(chr
->filename
);
3409 qemu_opts_del(chr
->opts
);
3414 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3416 ChardevInfoList
*chr_list
= NULL
;
3417 CharDriverState
*chr
;
3419 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3420 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3421 info
->value
= g_malloc0(sizeof(*info
->value
));
3422 info
->value
->label
= g_strdup(chr
->label
);
3423 info
->value
->filename
= g_strdup(chr
->filename
);
3425 info
->next
= chr_list
;
3432 CharDriverState
*qemu_chr_find(const char *name
)
3434 CharDriverState
*chr
;
3436 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3437 if (strcmp(chr
->label
, name
) != 0)
3444 /* Get a character (serial) device interface. */
3445 CharDriverState
*qemu_char_get_next_serial(void)
3447 static int next_serial
;
3448 CharDriverState
*chr
;
3450 /* FIXME: This function needs to go away: use chardev properties! */
3452 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3453 chr
= serial_hds
[next_serial
++];
3454 qemu_chr_fe_claim_no_fail(chr
);
3460 QemuOptsList qemu_chardev_opts
= {
3462 .implied_opt_name
= "backend",
3463 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3467 .type
= QEMU_OPT_STRING
,
3470 .type
= QEMU_OPT_STRING
,
3473 .type
= QEMU_OPT_STRING
,
3476 .type
= QEMU_OPT_STRING
,
3478 .name
= "localaddr",
3479 .type
= QEMU_OPT_STRING
,
3481 .name
= "localport",
3482 .type
= QEMU_OPT_STRING
,
3485 .type
= QEMU_OPT_NUMBER
,
3488 .type
= QEMU_OPT_BOOL
,
3491 .type
= QEMU_OPT_BOOL
,
3494 .type
= QEMU_OPT_BOOL
,
3497 .type
= QEMU_OPT_BOOL
,
3500 .type
= QEMU_OPT_BOOL
,
3503 .type
= QEMU_OPT_BOOL
,
3506 .type
= QEMU_OPT_NUMBER
,
3509 .type
= QEMU_OPT_NUMBER
,
3512 .type
= QEMU_OPT_NUMBER
,
3515 .type
= QEMU_OPT_NUMBER
,
3518 .type
= QEMU_OPT_BOOL
,
3521 .type
= QEMU_OPT_BOOL
,
3524 .type
= QEMU_OPT_STRING
,
3527 .type
= QEMU_OPT_NUMBER
,
3530 .type
= QEMU_OPT_SIZE
,
3532 { /* end of list */ }
3538 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3543 error_setg(errp
, "input file not supported");
3547 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3548 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3549 if (out
== INVALID_HANDLE_VALUE
) {
3550 error_setg(errp
, "open %s failed", file
->out
);
3553 return qemu_chr_open_win_file(out
);
3556 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3559 return qemu_chr_open_win_path(serial
->device
);
3562 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3565 error_setg(errp
, "character device backend type 'parallel' not supported");
3571 static int qmp_chardev_open_file_source(char *src
, int flags
,
3576 TFR(fd
= qemu_open(src
, flags
, 0666));
3578 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3583 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3585 int flags
, in
= -1, out
= -1;
3587 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3588 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3589 if (error_is_set(errp
)) {
3595 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3596 if (error_is_set(errp
)) {
3602 return qemu_chr_open_fd(in
, out
);
3605 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3608 #ifdef HAVE_CHARDEV_TTY
3611 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3612 if (error_is_set(errp
)) {
3615 qemu_set_nonblock(fd
);
3616 return qemu_chr_open_tty_fd(fd
);
3618 error_setg(errp
, "character device backend type 'serial' not supported");
3623 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3626 #ifdef HAVE_CHARDEV_PARPORT
3629 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3630 if (error_is_set(errp
)) {
3633 return qemu_chr_open_pp_fd(fd
);
3635 error_setg(errp
, "character device backend type 'parallel' not supported");
3642 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3645 SocketAddress
*addr
= sock
->addr
;
3646 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3647 bool is_listen
= sock
->has_server
? sock
->server
: true;
3648 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3649 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3653 fd
= socket_listen(addr
, errp
);
3655 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3657 if (error_is_set(errp
)) {
3660 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3661 is_telnet
, is_waitconnect
, errp
);
3664 static CharDriverState
*qmp_chardev_open_dgram(ChardevDgram
*dgram
,
3669 fd
= socket_dgram(dgram
->remote
, dgram
->local
, errp
);
3670 if (error_is_set(errp
)) {
3673 return qemu_chr_open_udp_fd(fd
);
3676 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3679 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3680 CharDriverState
*base
, *chr
= NULL
;
3682 chr
= qemu_chr_find(id
);
3684 error_setg(errp
, "Chardev '%s' already exists", id
);
3689 switch (backend
->kind
) {
3690 case CHARDEV_BACKEND_KIND_FILE
:
3691 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3693 case CHARDEV_BACKEND_KIND_SERIAL
:
3694 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3696 case CHARDEV_BACKEND_KIND_PARALLEL
:
3697 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3699 case CHARDEV_BACKEND_KIND_PIPE
:
3700 chr
= qemu_chr_open_pipe(backend
->pipe
);
3702 case CHARDEV_BACKEND_KIND_SOCKET
:
3703 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3705 case CHARDEV_BACKEND_KIND_DGRAM
:
3706 chr
= qmp_chardev_open_dgram(backend
->dgram
, errp
);
3708 #ifdef HAVE_CHARDEV_TTY
3709 case CHARDEV_BACKEND_KIND_PTY
:
3710 chr
= qemu_chr_open_pty(id
, ret
);
3713 case CHARDEV_BACKEND_KIND_NULL
:
3714 chr
= qemu_chr_open_null();
3716 case CHARDEV_BACKEND_KIND_MUX
:
3717 base
= qemu_chr_find(backend
->mux
->chardev
);
3719 error_setg(errp
, "mux: base chardev %s not found",
3720 backend
->mux
->chardev
);
3723 chr
= qemu_chr_open_mux(base
);
3725 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3726 chr
= qemu_chr_open_msmouse();
3728 #ifdef CONFIG_BRLAPI
3729 case CHARDEV_BACKEND_KIND_BRAILLE
:
3730 chr
= chr_baum_init();
3733 case CHARDEV_BACKEND_KIND_STDIO
:
3734 chr
= qemu_chr_open_stdio(backend
->stdio
);
3737 case CHARDEV_BACKEND_KIND_CONSOLE
:
3738 chr
= qemu_chr_open_win_con();
3742 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3743 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3745 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3746 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3749 case CHARDEV_BACKEND_KIND_VC
:
3750 chr
= vc_init(backend
->vc
);
3752 case CHARDEV_BACKEND_KIND_MEMORY
:
3753 chr
= qemu_chr_open_ringbuf(backend
->memory
, errp
);
3756 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3760 if (chr
== NULL
&& !error_is_set(errp
)) {
3761 error_setg(errp
, "Failed to create chardev");
3764 chr
->label
= g_strdup(id
);
3765 chr
->avail_connections
=
3766 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3767 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3775 void qmp_chardev_remove(const char *id
, Error
**errp
)
3777 CharDriverState
*chr
;
3779 chr
= qemu_chr_find(id
);
3781 error_setg(errp
, "Chardev '%s' not found", id
);
3784 if (chr
->chr_can_read
|| chr
->chr_read
||
3785 chr
->chr_event
|| chr
->handler_opaque
) {
3786 error_setg(errp
, "Chardev '%s' is busy", id
);
3789 qemu_chr_delete(chr
);
3792 static void register_types(void)
3794 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3795 register_char_driver("socket", qemu_chr_open_socket
);
3796 register_char_driver("udp", qemu_chr_open_udp
);
3797 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3798 qemu_chr_parse_ringbuf
);
3799 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3800 qemu_chr_parse_file_out
);
3801 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3802 qemu_chr_parse_stdio
);
3803 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3804 qemu_chr_parse_serial
);
3805 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3806 qemu_chr_parse_serial
);
3807 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3808 qemu_chr_parse_parallel
);
3809 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3810 qemu_chr_parse_parallel
);
3811 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3812 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3813 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3814 qemu_chr_parse_pipe
);
3817 type_init(register_types
);