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
,
668 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
669 iwp
->fd_can_read
= fd_can_read
;
670 iwp
->opaque
= user_data
;
671 iwp
->channel
= channel
;
672 iwp
->fd_read
= (GSourceFunc
) fd_read
;
675 tag
= g_source_attach(&iwp
->parent
, NULL
);
676 g_source_unref(&iwp
->parent
);
681 static GIOChannel
*io_channel_from_fd(int fd
)
689 chan
= g_io_channel_unix_new(fd
);
691 g_io_channel_set_encoding(chan
, NULL
, NULL
);
692 g_io_channel_set_buffered(chan
, FALSE
);
698 static GIOChannel
*io_channel_from_socket(int fd
)
707 chan
= g_io_channel_win32_new_socket(fd
);
709 chan
= g_io_channel_unix_new(fd
);
712 g_io_channel_set_encoding(chan
, NULL
, NULL
);
713 g_io_channel_set_buffered(chan
, FALSE
);
718 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
724 while (offset
< len
) {
727 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
728 &bytes_written
, NULL
);
729 if (status
!= G_IO_STATUS_NORMAL
) {
730 if (status
== G_IO_STATUS_AGAIN
) {
731 /* If we've written any data, return a partial write. */
741 } else if (status
== G_IO_STATUS_EOF
) {
745 offset
+= bytes_written
;
753 typedef struct FDCharDriver
{
754 CharDriverState
*chr
;
755 GIOChannel
*fd_in
, *fd_out
;
758 QTAILQ_ENTRY(FDCharDriver
) node
;
761 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
763 FDCharDriver
*s
= chr
->opaque
;
765 return io_channel_send(s
->fd_out
, buf
, len
);
768 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
770 CharDriverState
*chr
= opaque
;
771 FDCharDriver
*s
= chr
->opaque
;
773 uint8_t buf
[READ_BUF_LEN
];
778 if (len
> s
->max_size
) {
785 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
786 len
, &bytes_read
, NULL
);
787 if (status
== G_IO_STATUS_EOF
) {
788 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
791 if (status
== G_IO_STATUS_NORMAL
) {
792 qemu_chr_be_write(chr
, buf
, bytes_read
);
798 static int fd_chr_read_poll(void *opaque
)
800 CharDriverState
*chr
= opaque
;
801 FDCharDriver
*s
= chr
->opaque
;
803 s
->max_size
= qemu_chr_be_can_write(chr
);
807 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
809 FDCharDriver
*s
= chr
->opaque
;
810 return g_io_create_watch(s
->fd_out
, cond
);
813 static void fd_chr_update_read_handler(CharDriverState
*chr
)
815 FDCharDriver
*s
= chr
->opaque
;
818 g_source_remove(s
->fd_in_tag
);
822 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
826 static void fd_chr_close(struct CharDriverState
*chr
)
828 FDCharDriver
*s
= chr
->opaque
;
831 g_source_remove(s
->fd_in_tag
);
836 g_io_channel_unref(s
->fd_in
);
839 g_io_channel_unref(s
->fd_out
);
843 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
846 /* open a character device to a unix fd */
847 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
849 CharDriverState
*chr
;
852 chr
= g_malloc0(sizeof(CharDriverState
));
853 s
= g_malloc0(sizeof(FDCharDriver
));
854 s
->fd_in
= io_channel_from_fd(fd_in
);
855 s
->fd_out
= io_channel_from_fd(fd_out
);
856 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
859 chr
->chr_add_watch
= fd_chr_add_watch
;
860 chr
->chr_write
= fd_chr_write
;
861 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
862 chr
->chr_close
= fd_chr_close
;
864 qemu_chr_be_generic_open(chr
);
869 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
872 char filename_in
[256], filename_out
[256];
873 const char *filename
= opts
->device
;
875 if (filename
== NULL
) {
876 fprintf(stderr
, "chardev: pipe: no filename given\n");
880 snprintf(filename_in
, 256, "%s.in", filename
);
881 snprintf(filename_out
, 256, "%s.out", filename
);
882 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
883 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
884 if (fd_in
< 0 || fd_out
< 0) {
889 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
894 return qemu_chr_open_fd(fd_in
, fd_out
);
897 /* init terminal so that we can grab keys */
898 static struct termios oldtty
;
899 static int old_fd0_flags
;
900 static bool stdio_allow_signal
;
902 static void term_exit(void)
904 tcsetattr (0, TCSANOW
, &oldtty
);
905 fcntl(0, F_SETFL
, old_fd0_flags
);
908 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
914 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
915 |INLCR
|IGNCR
|ICRNL
|IXON
);
916 tty
.c_oflag
|= OPOST
;
917 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
918 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
923 /* if graphical mode, we allow Ctrl-C handling */
924 if (!stdio_allow_signal
)
925 tty
.c_lflag
&= ~ISIG
;
927 tcsetattr (0, TCSANOW
, &tty
);
930 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
936 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
938 CharDriverState
*chr
;
940 if (is_daemonized()) {
941 error_report("cannot use stdio with -daemonize");
944 old_fd0_flags
= fcntl(0, F_GETFL
);
945 tcgetattr (0, &oldtty
);
946 fcntl(0, F_SETFL
, O_NONBLOCK
);
949 chr
= qemu_chr_open_fd(0, 1);
950 chr
->chr_close
= qemu_chr_close_stdio
;
951 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
952 stdio_allow_signal
= display_type
!= DT_NOGRAPHIC
;
953 if (opts
->has_signal
) {
954 stdio_allow_signal
= opts
->signal
;
956 qemu_chr_fe_set_echo(chr
, false);
962 /* Once Solaris has openpty(), this is going to be removed. */
963 static int openpty(int *amaster
, int *aslave
, char *name
,
964 struct termios
*termp
, struct winsize
*winp
)
967 int mfd
= -1, sfd
= -1;
969 *amaster
= *aslave
= -1;
971 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
975 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
978 if ((slave
= ptsname(mfd
)) == NULL
)
981 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
984 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
985 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
993 ioctl(sfd
, TIOCSWINSZ
, winp
);
1004 static void cfmakeraw (struct termios
*termios_p
)
1006 termios_p
->c_iflag
&=
1007 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
1008 termios_p
->c_oflag
&= ~OPOST
;
1009 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
1010 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
1011 termios_p
->c_cflag
|= CS8
;
1013 termios_p
->c_cc
[VMIN
] = 0;
1014 termios_p
->c_cc
[VTIME
] = 0;
1018 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1019 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1020 || defined(__GLIBC__)
1022 #define HAVE_CHARDEV_TTY 1
1033 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1034 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1036 static gboolean
pty_chr_timer(gpointer opaque
)
1038 struct CharDriverState
*chr
= opaque
;
1039 PtyCharDriver
*s
= chr
->opaque
;
1045 /* If we arrive here without polling being cleared due
1046 * read returning -EIO, then we are (re-)connected */
1047 pty_chr_state(chr
, 1);
1052 pty_chr_update_read_handler(chr
);
1058 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1060 PtyCharDriver
*s
= chr
->opaque
;
1063 g_source_remove(s
->timer_tag
);
1068 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1070 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1074 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1076 PtyCharDriver
*s
= chr
->opaque
;
1078 if (!s
->connected
) {
1079 /* guest sends data, check for (re-)connect */
1080 pty_chr_update_read_handler(chr
);
1083 return io_channel_send(s
->fd
, buf
, len
);
1086 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1088 PtyCharDriver
*s
= chr
->opaque
;
1089 return g_io_create_watch(s
->fd
, cond
);
1092 static int pty_chr_read_poll(void *opaque
)
1094 CharDriverState
*chr
= opaque
;
1095 PtyCharDriver
*s
= chr
->opaque
;
1097 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1098 return s
->read_bytes
;
1101 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1103 CharDriverState
*chr
= opaque
;
1104 PtyCharDriver
*s
= chr
->opaque
;
1106 uint8_t buf
[READ_BUF_LEN
];
1110 if (len
> s
->read_bytes
)
1111 len
= s
->read_bytes
;
1114 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1115 if (status
!= G_IO_STATUS_NORMAL
) {
1116 pty_chr_state(chr
, 0);
1119 pty_chr_state(chr
, 1);
1120 qemu_chr_be_write(chr
, buf
, size
);
1125 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1127 PtyCharDriver
*s
= chr
->opaque
;
1130 g_source_remove(s
->fd_tag
);
1133 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1136 * Short timeout here: just need wait long enougth that qemu makes
1137 * it through the poll loop once. When reconnected we want a
1138 * short timeout so we notice it almost instantly. Otherwise
1139 * read() gives us -EIO instantly, making pty_chr_state() reset the
1140 * timeout to the normal (much longer) poll interval before the
1143 pty_chr_rearm_timer(chr
, 10);
1146 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1148 PtyCharDriver
*s
= chr
->opaque
;
1151 g_source_remove(s
->fd_tag
);
1155 /* (re-)connect poll interval for idle guests: once per second.
1156 * We check more frequently in case the guests sends data to
1157 * the virtual device linked to our pty. */
1158 pty_chr_rearm_timer(chr
, 1000);
1161 qemu_chr_be_generic_open(chr
);
1167 static void pty_chr_close(struct CharDriverState
*chr
)
1169 PtyCharDriver
*s
= chr
->opaque
;
1173 g_source_remove(s
->fd_tag
);
1175 fd
= g_io_channel_unix_get_fd(s
->fd
);
1176 g_io_channel_unref(s
->fd
);
1179 g_source_remove(s
->timer_tag
);
1182 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1185 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1188 CharDriverState
*chr
;
1191 int master_fd
, slave_fd
;
1192 #if defined(__OpenBSD__) || defined(__DragonFly__)
1193 char pty_name
[PATH_MAX
];
1194 #define q_ptsname(x) pty_name
1196 char *pty_name
= NULL
;
1197 #define q_ptsname(x) ptsname(x)
1200 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1204 /* Set raw attributes on the pty. */
1205 tcgetattr(slave_fd
, &tty
);
1207 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1210 chr
= g_malloc0(sizeof(CharDriverState
));
1212 chr
->filename
= g_strdup_printf("pty:%s", q_ptsname(master_fd
));
1213 ret
->pty
= g_strdup(q_ptsname(master_fd
));
1214 ret
->has_pty
= true;
1216 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1217 q_ptsname(master_fd
), id
);
1219 s
= g_malloc0(sizeof(PtyCharDriver
));
1221 chr
->chr_write
= pty_chr_write
;
1222 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1223 chr
->chr_close
= pty_chr_close
;
1224 chr
->chr_add_watch
= pty_chr_add_watch
;
1226 s
->fd
= io_channel_from_fd(master_fd
);
1232 static void tty_serial_init(int fd
, int speed
,
1233 int parity
, int data_bits
, int stop_bits
)
1239 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1240 speed
, parity
, data_bits
, stop_bits
);
1242 tcgetattr (fd
, &tty
);
1244 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1245 speed
= speed
* 10 / 11;
1262 /* Non-Posix values follow. They may be unsupported on some systems. */
1264 check_speed(115200);
1266 check_speed(230400);
1269 check_speed(460800);
1272 check_speed(500000);
1275 check_speed(576000);
1278 check_speed(921600);
1281 check_speed(1000000);
1284 check_speed(1152000);
1287 check_speed(1500000);
1290 check_speed(2000000);
1293 check_speed(2500000);
1296 check_speed(3000000);
1299 check_speed(3500000);
1302 check_speed(4000000);
1307 cfsetispeed(&tty
, spd
);
1308 cfsetospeed(&tty
, spd
);
1310 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1311 |INLCR
|IGNCR
|ICRNL
|IXON
);
1312 tty
.c_oflag
|= OPOST
;
1313 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1314 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1335 tty
.c_cflag
|= PARENB
;
1338 tty
.c_cflag
|= PARENB
| PARODD
;
1342 tty
.c_cflag
|= CSTOPB
;
1344 tcsetattr (fd
, TCSANOW
, &tty
);
1347 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1349 FDCharDriver
*s
= chr
->opaque
;
1352 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1354 QEMUSerialSetParams
*ssp
= arg
;
1355 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1356 ssp
->speed
, ssp
->parity
,
1357 ssp
->data_bits
, ssp
->stop_bits
);
1360 case CHR_IOCTL_SERIAL_SET_BREAK
:
1362 int enable
= *(int *)arg
;
1364 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1368 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1371 int *targ
= (int *)arg
;
1372 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1374 if (sarg
& TIOCM_CTS
)
1375 *targ
|= CHR_TIOCM_CTS
;
1376 if (sarg
& TIOCM_CAR
)
1377 *targ
|= CHR_TIOCM_CAR
;
1378 if (sarg
& TIOCM_DSR
)
1379 *targ
|= CHR_TIOCM_DSR
;
1380 if (sarg
& TIOCM_RI
)
1381 *targ
|= CHR_TIOCM_RI
;
1382 if (sarg
& TIOCM_DTR
)
1383 *targ
|= CHR_TIOCM_DTR
;
1384 if (sarg
& TIOCM_RTS
)
1385 *targ
|= CHR_TIOCM_RTS
;
1388 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1390 int sarg
= *(int *)arg
;
1392 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1393 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1394 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1395 if (sarg
& CHR_TIOCM_CTS
)
1397 if (sarg
& CHR_TIOCM_CAR
)
1399 if (sarg
& CHR_TIOCM_DSR
)
1401 if (sarg
& CHR_TIOCM_RI
)
1403 if (sarg
& CHR_TIOCM_DTR
)
1405 if (sarg
& CHR_TIOCM_RTS
)
1407 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1416 static void qemu_chr_close_tty(CharDriverState
*chr
)
1418 FDCharDriver
*s
= chr
->opaque
;
1422 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1432 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1434 CharDriverState
*chr
;
1436 tty_serial_init(fd
, 115200, 'N', 8, 1);
1437 chr
= qemu_chr_open_fd(fd
, fd
);
1438 chr
->chr_ioctl
= tty_serial_ioctl
;
1439 chr
->chr_close
= qemu_chr_close_tty
;
1442 #endif /* __linux__ || __sun__ */
1444 #if defined(__linux__)
1446 #define HAVE_CHARDEV_PARPORT 1
1451 } ParallelCharDriver
;
1453 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1455 if (s
->mode
!= mode
) {
1457 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1464 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1466 ParallelCharDriver
*drv
= chr
->opaque
;
1471 case CHR_IOCTL_PP_READ_DATA
:
1472 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1474 *(uint8_t *)arg
= b
;
1476 case CHR_IOCTL_PP_WRITE_DATA
:
1477 b
= *(uint8_t *)arg
;
1478 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1481 case CHR_IOCTL_PP_READ_CONTROL
:
1482 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1484 /* Linux gives only the lowest bits, and no way to know data
1485 direction! For better compatibility set the fixed upper
1487 *(uint8_t *)arg
= b
| 0xc0;
1489 case CHR_IOCTL_PP_WRITE_CONTROL
:
1490 b
= *(uint8_t *)arg
;
1491 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1494 case CHR_IOCTL_PP_READ_STATUS
:
1495 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1497 *(uint8_t *)arg
= b
;
1499 case CHR_IOCTL_PP_DATA_DIR
:
1500 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1503 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1504 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1505 struct ParallelIOArg
*parg
= arg
;
1506 int n
= read(fd
, parg
->buffer
, parg
->count
);
1507 if (n
!= parg
->count
) {
1512 case CHR_IOCTL_PP_EPP_READ
:
1513 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1514 struct ParallelIOArg
*parg
= arg
;
1515 int n
= read(fd
, parg
->buffer
, parg
->count
);
1516 if (n
!= parg
->count
) {
1521 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1522 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1523 struct ParallelIOArg
*parg
= arg
;
1524 int n
= write(fd
, parg
->buffer
, parg
->count
);
1525 if (n
!= parg
->count
) {
1530 case CHR_IOCTL_PP_EPP_WRITE
:
1531 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1532 struct ParallelIOArg
*parg
= arg
;
1533 int n
= write(fd
, parg
->buffer
, parg
->count
);
1534 if (n
!= parg
->count
) {
1545 static void pp_close(CharDriverState
*chr
)
1547 ParallelCharDriver
*drv
= chr
->opaque
;
1550 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1551 ioctl(fd
, PPRELEASE
);
1554 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1557 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1559 CharDriverState
*chr
;
1560 ParallelCharDriver
*drv
;
1562 if (ioctl(fd
, PPCLAIM
) < 0) {
1567 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1569 drv
->mode
= IEEE1284_MODE_COMPAT
;
1571 chr
= g_malloc0(sizeof(CharDriverState
));
1572 chr
->chr_write
= null_chr_write
;
1573 chr
->chr_ioctl
= pp_ioctl
;
1574 chr
->chr_close
= pp_close
;
1577 qemu_chr_be_generic_open(chr
);
1581 #endif /* __linux__ */
1583 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1585 #define HAVE_CHARDEV_PARPORT 1
1587 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1589 int fd
= (int)(intptr_t)chr
->opaque
;
1593 case CHR_IOCTL_PP_READ_DATA
:
1594 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1596 *(uint8_t *)arg
= b
;
1598 case CHR_IOCTL_PP_WRITE_DATA
:
1599 b
= *(uint8_t *)arg
;
1600 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1603 case CHR_IOCTL_PP_READ_CONTROL
:
1604 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1606 *(uint8_t *)arg
= b
;
1608 case CHR_IOCTL_PP_WRITE_CONTROL
:
1609 b
= *(uint8_t *)arg
;
1610 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1613 case CHR_IOCTL_PP_READ_STATUS
:
1614 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1616 *(uint8_t *)arg
= b
;
1624 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1626 CharDriverState
*chr
;
1628 chr
= g_malloc0(sizeof(CharDriverState
));
1629 chr
->opaque
= (void *)(intptr_t)fd
;
1630 chr
->chr_write
= null_chr_write
;
1631 chr
->chr_ioctl
= pp_ioctl
;
1640 HANDLE hcom
, hrecv
, hsend
;
1641 OVERLAPPED orecv
, osend
;
1648 HANDLE hInputReadyEvent
;
1649 HANDLE hInputDoneEvent
;
1650 HANDLE hInputThread
;
1651 uint8_t win_stdio_buf
;
1652 } WinStdioCharState
;
1654 #define NSENDBUF 2048
1655 #define NRECVBUF 2048
1656 #define MAXCONNECT 1
1657 #define NTIMEOUT 5000
1659 static int win_chr_poll(void *opaque
);
1660 static int win_chr_pipe_poll(void *opaque
);
1662 static void win_chr_close(CharDriverState
*chr
)
1664 WinCharState
*s
= chr
->opaque
;
1667 CloseHandle(s
->hsend
);
1671 CloseHandle(s
->hrecv
);
1675 CloseHandle(s
->hcom
);
1679 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1681 qemu_del_polling_cb(win_chr_poll
, chr
);
1683 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1686 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1688 WinCharState
*s
= chr
->opaque
;
1690 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1695 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1697 fprintf(stderr
, "Failed CreateEvent\n");
1700 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1702 fprintf(stderr
, "Failed CreateEvent\n");
1706 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1707 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1708 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1709 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1714 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1715 fprintf(stderr
, "Failed SetupComm\n");
1719 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1720 size
= sizeof(COMMCONFIG
);
1721 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1722 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1723 CommConfigDialog(filename
, NULL
, &comcfg
);
1725 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1726 fprintf(stderr
, "Failed SetCommState\n");
1730 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1731 fprintf(stderr
, "Failed SetCommMask\n");
1735 cto
.ReadIntervalTimeout
= MAXDWORD
;
1736 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1737 fprintf(stderr
, "Failed SetCommTimeouts\n");
1741 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1742 fprintf(stderr
, "Failed ClearCommError\n");
1745 qemu_add_polling_cb(win_chr_poll
, chr
);
1753 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1755 WinCharState
*s
= chr
->opaque
;
1756 DWORD len
, ret
, size
, err
;
1759 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1760 s
->osend
.hEvent
= s
->hsend
;
1763 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1765 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1767 err
= GetLastError();
1768 if (err
== ERROR_IO_PENDING
) {
1769 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1787 static int win_chr_read_poll(CharDriverState
*chr
)
1789 WinCharState
*s
= chr
->opaque
;
1791 s
->max_size
= qemu_chr_be_can_write(chr
);
1795 static void win_chr_readfile(CharDriverState
*chr
)
1797 WinCharState
*s
= chr
->opaque
;
1799 uint8_t buf
[READ_BUF_LEN
];
1802 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1803 s
->orecv
.hEvent
= s
->hrecv
;
1804 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1806 err
= GetLastError();
1807 if (err
== ERROR_IO_PENDING
) {
1808 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1813 qemu_chr_be_write(chr
, buf
, size
);
1817 static void win_chr_read(CharDriverState
*chr
)
1819 WinCharState
*s
= chr
->opaque
;
1821 if (s
->len
> s
->max_size
)
1822 s
->len
= s
->max_size
;
1826 win_chr_readfile(chr
);
1829 static int win_chr_poll(void *opaque
)
1831 CharDriverState
*chr
= opaque
;
1832 WinCharState
*s
= chr
->opaque
;
1836 ClearCommError(s
->hcom
, &comerr
, &status
);
1837 if (status
.cbInQue
> 0) {
1838 s
->len
= status
.cbInQue
;
1839 win_chr_read_poll(chr
);
1846 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1848 CharDriverState
*chr
;
1851 chr
= g_malloc0(sizeof(CharDriverState
));
1852 s
= g_malloc0(sizeof(WinCharState
));
1854 chr
->chr_write
= win_chr_write
;
1855 chr
->chr_close
= win_chr_close
;
1857 if (win_chr_init(chr
, filename
) < 0) {
1862 qemu_chr_be_generic_open(chr
);
1866 static int win_chr_pipe_poll(void *opaque
)
1868 CharDriverState
*chr
= opaque
;
1869 WinCharState
*s
= chr
->opaque
;
1872 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1875 win_chr_read_poll(chr
);
1882 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1884 WinCharState
*s
= chr
->opaque
;
1892 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1894 fprintf(stderr
, "Failed CreateEvent\n");
1897 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1899 fprintf(stderr
, "Failed CreateEvent\n");
1903 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1904 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1905 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1907 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1908 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1909 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1914 ZeroMemory(&ov
, sizeof(ov
));
1915 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1916 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1918 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1922 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1924 fprintf(stderr
, "Failed GetOverlappedResult\n");
1926 CloseHandle(ov
.hEvent
);
1933 CloseHandle(ov
.hEvent
);
1936 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1945 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1947 const char *filename
= opts
->device
;
1948 CharDriverState
*chr
;
1951 chr
= g_malloc0(sizeof(CharDriverState
));
1952 s
= g_malloc0(sizeof(WinCharState
));
1954 chr
->chr_write
= win_chr_write
;
1955 chr
->chr_close
= win_chr_close
;
1957 if (win_chr_pipe_init(chr
, filename
) < 0) {
1962 qemu_chr_be_generic_open(chr
);
1966 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1968 CharDriverState
*chr
;
1971 chr
= g_malloc0(sizeof(CharDriverState
));
1972 s
= g_malloc0(sizeof(WinCharState
));
1975 chr
->chr_write
= win_chr_write
;
1976 qemu_chr_be_generic_open(chr
);
1980 static CharDriverState
*qemu_chr_open_win_con(void)
1982 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1985 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1987 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1994 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2004 static void win_stdio_wait_func(void *opaque
)
2006 CharDriverState
*chr
= opaque
;
2007 WinStdioCharState
*stdio
= chr
->opaque
;
2008 INPUT_RECORD buf
[4];
2013 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
2017 /* Avoid error storm */
2018 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2022 for (i
= 0; i
< dwSize
; i
++) {
2023 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2025 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2027 if (kev
->uChar
.AsciiChar
!= 0) {
2028 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2029 if (qemu_chr_be_can_write(chr
)) {
2030 uint8_t c
= kev
->uChar
.AsciiChar
;
2031 qemu_chr_be_write(chr
, &c
, 1);
2039 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2041 CharDriverState
*chr
= param
;
2042 WinStdioCharState
*stdio
= chr
->opaque
;
2048 /* Wait for one byte */
2049 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2051 /* Exit in case of error, continue if nothing read */
2059 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2060 if (stdio
->win_stdio_buf
== '\r') {
2064 /* Signal the main thread and wait until the byte was eaten */
2065 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2068 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2074 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2078 static void win_stdio_thread_wait_func(void *opaque
)
2080 CharDriverState
*chr
= opaque
;
2081 WinStdioCharState
*stdio
= chr
->opaque
;
2083 if (qemu_chr_be_can_write(chr
)) {
2084 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2087 SetEvent(stdio
->hInputDoneEvent
);
2090 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2092 WinStdioCharState
*stdio
= chr
->opaque
;
2095 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2098 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2100 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2104 static void win_stdio_close(CharDriverState
*chr
)
2106 WinStdioCharState
*stdio
= chr
->opaque
;
2108 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2109 CloseHandle(stdio
->hInputReadyEvent
);
2111 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2112 CloseHandle(stdio
->hInputDoneEvent
);
2114 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2115 TerminateThread(stdio
->hInputThread
, 0);
2118 g_free(chr
->opaque
);
2122 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2124 CharDriverState
*chr
;
2125 WinStdioCharState
*stdio
;
2129 chr
= g_malloc0(sizeof(CharDriverState
));
2130 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2132 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2133 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2134 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2138 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2140 chr
->opaque
= stdio
;
2141 chr
->chr_write
= win_stdio_write
;
2142 chr
->chr_close
= win_stdio_close
;
2145 if (qemu_add_wait_object(stdio
->hStdIn
,
2146 win_stdio_wait_func
, chr
)) {
2147 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2152 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2153 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2154 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2157 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2158 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2159 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2160 fprintf(stderr
, "cannot create stdio thread or event\n");
2163 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2164 win_stdio_thread_wait_func
, chr
)) {
2165 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2169 dwMode
|= ENABLE_LINE_INPUT
;
2172 /* set the terminal in raw mode */
2173 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2174 dwMode
|= ENABLE_PROCESSED_INPUT
;
2177 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2179 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2180 qemu_chr_fe_set_echo(chr
, false);
2184 #endif /* !_WIN32 */
2187 /***********************************************************/
2188 /* UDP Net console */
2194 uint8_t buf
[READ_BUF_LEN
];
2200 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2202 NetCharDriver
*s
= chr
->opaque
;
2203 gsize bytes_written
;
2206 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2207 if (status
== G_IO_STATUS_EOF
) {
2209 } else if (status
!= G_IO_STATUS_NORMAL
) {
2213 return bytes_written
;
2216 static int udp_chr_read_poll(void *opaque
)
2218 CharDriverState
*chr
= opaque
;
2219 NetCharDriver
*s
= chr
->opaque
;
2221 s
->max_size
= qemu_chr_be_can_write(chr
);
2223 /* If there were any stray characters in the queue process them
2226 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2227 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2229 s
->max_size
= qemu_chr_be_can_write(chr
);
2234 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2236 CharDriverState
*chr
= opaque
;
2237 NetCharDriver
*s
= chr
->opaque
;
2238 gsize bytes_read
= 0;
2241 if (s
->max_size
== 0)
2243 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2245 s
->bufcnt
= bytes_read
;
2246 s
->bufptr
= s
->bufcnt
;
2247 if (status
!= G_IO_STATUS_NORMAL
) {
2252 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2253 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2255 s
->max_size
= qemu_chr_be_can_write(chr
);
2261 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2263 NetCharDriver
*s
= chr
->opaque
;
2266 g_source_remove(s
->tag
);
2271 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2275 static void udp_chr_close(CharDriverState
*chr
)
2277 NetCharDriver
*s
= chr
->opaque
;
2279 g_source_remove(s
->tag
);
2282 g_io_channel_unref(s
->chan
);
2286 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2289 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2291 CharDriverState
*chr
= NULL
;
2292 NetCharDriver
*s
= NULL
;
2294 chr
= g_malloc0(sizeof(CharDriverState
));
2295 s
= g_malloc0(sizeof(NetCharDriver
));
2298 s
->chan
= io_channel_from_socket(s
->fd
);
2302 chr
->chr_write
= udp_chr_write
;
2303 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2304 chr
->chr_close
= udp_chr_close
;
2308 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2310 Error
*local_err
= NULL
;
2313 fd
= inet_dgram_opts(opts
, &local_err
);
2317 return qemu_chr_open_udp_fd(fd
);
2320 /***********************************************************/
2321 /* TCP Net console */
2325 GIOChannel
*chan
, *listen_chan
;
2326 guint tag
, listen_tag
;
2336 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2338 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2340 TCPCharDriver
*s
= chr
->opaque
;
2342 return io_channel_send(s
->chan
, buf
, len
);
2344 /* XXX: indicate an error ? */
2349 static int tcp_chr_read_poll(void *opaque
)
2351 CharDriverState
*chr
= opaque
;
2352 TCPCharDriver
*s
= chr
->opaque
;
2355 s
->max_size
= qemu_chr_be_can_write(chr
);
2360 #define IAC_BREAK 243
2361 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2363 uint8_t *buf
, int *size
)
2365 /* Handle any telnet client's basic IAC options to satisfy char by
2366 * char mode with no echo. All IAC options will be removed from
2367 * the buf and the do_telnetopt variable will be used to track the
2368 * state of the width of the IAC information.
2370 * IAC commands come in sets of 3 bytes with the exception of the
2371 * "IAC BREAK" command and the double IAC.
2377 for (i
= 0; i
< *size
; i
++) {
2378 if (s
->do_telnetopt
> 1) {
2379 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2380 /* Double IAC means send an IAC */
2384 s
->do_telnetopt
= 1;
2386 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2387 /* Handle IAC break commands by sending a serial break */
2388 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2393 if (s
->do_telnetopt
>= 4) {
2394 s
->do_telnetopt
= 1;
2397 if ((unsigned char)buf
[i
] == IAC
) {
2398 s
->do_telnetopt
= 2;
2409 static int tcp_get_msgfd(CharDriverState
*chr
)
2411 TCPCharDriver
*s
= chr
->opaque
;
2418 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2420 TCPCharDriver
*s
= chr
->opaque
;
2421 struct cmsghdr
*cmsg
;
2423 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2426 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2427 cmsg
->cmsg_level
!= SOL_SOCKET
||
2428 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2431 fd
= *((int *)CMSG_DATA(cmsg
));
2435 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2438 #ifndef MSG_CMSG_CLOEXEC
2439 qemu_set_cloexec(fd
);
2447 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2449 TCPCharDriver
*s
= chr
->opaque
;
2450 struct msghdr msg
= { NULL
, };
2451 struct iovec iov
[1];
2453 struct cmsghdr cmsg
;
2454 char control
[CMSG_SPACE(sizeof(int))];
2459 iov
[0].iov_base
= buf
;
2460 iov
[0].iov_len
= len
;
2464 msg
.msg_control
= &msg_control
;
2465 msg
.msg_controllen
= sizeof(msg_control
);
2467 #ifdef MSG_CMSG_CLOEXEC
2468 flags
|= MSG_CMSG_CLOEXEC
;
2470 ret
= recvmsg(s
->fd
, &msg
, flags
);
2471 if (ret
> 0 && s
->is_unix
) {
2472 unix_process_msgfd(chr
, &msg
);
2478 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2480 TCPCharDriver
*s
= chr
->opaque
;
2481 return qemu_recv(s
->fd
, buf
, len
, 0);
2485 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2487 TCPCharDriver
*s
= chr
->opaque
;
2488 return g_io_create_watch(s
->chan
, cond
);
2491 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2493 CharDriverState
*chr
= opaque
;
2494 TCPCharDriver
*s
= chr
->opaque
;
2495 uint8_t buf
[READ_BUF_LEN
];
2498 if (!s
->connected
|| s
->max_size
<= 0) {
2502 if (len
> s
->max_size
)
2504 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2506 /* connection closed */
2508 if (s
->listen_chan
) {
2509 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2511 g_source_remove(s
->tag
);
2513 g_io_channel_unref(s
->chan
);
2517 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2518 } else if (size
> 0) {
2519 if (s
->do_telnetopt
)
2520 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2522 qemu_chr_be_write(chr
, buf
, size
);
2529 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2531 return qemu_chr_open_fd(eventfd
, eventfd
);
2535 static void tcp_chr_connect(void *opaque
)
2537 CharDriverState
*chr
= opaque
;
2538 TCPCharDriver
*s
= chr
->opaque
;
2542 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2544 qemu_chr_be_generic_open(chr
);
2547 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2548 static void tcp_chr_telnet_init(int fd
)
2551 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2552 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2553 send(fd
, (char *)buf
, 3, 0);
2554 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2555 send(fd
, (char *)buf
, 3, 0);
2556 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2557 send(fd
, (char *)buf
, 3, 0);
2558 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2559 send(fd
, (char *)buf
, 3, 0);
2562 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2564 TCPCharDriver
*s
= chr
->opaque
;
2568 qemu_set_nonblock(fd
);
2570 socket_set_nodelay(fd
);
2572 s
->chan
= io_channel_from_socket(fd
);
2573 g_source_remove(s
->listen_tag
);
2575 tcp_chr_connect(chr
);
2580 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2582 CharDriverState
*chr
= opaque
;
2583 TCPCharDriver
*s
= chr
->opaque
;
2584 struct sockaddr_in saddr
;
2586 struct sockaddr_un uaddr
;
2588 struct sockaddr
*addr
;
2595 len
= sizeof(uaddr
);
2596 addr
= (struct sockaddr
*)&uaddr
;
2600 len
= sizeof(saddr
);
2601 addr
= (struct sockaddr
*)&saddr
;
2603 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2604 if (fd
< 0 && errno
!= EINTR
) {
2606 } else if (fd
>= 0) {
2607 if (s
->do_telnetopt
)
2608 tcp_chr_telnet_init(fd
);
2612 if (tcp_chr_add_client(chr
, fd
) < 0)
2618 static void tcp_chr_close(CharDriverState
*chr
)
2620 TCPCharDriver
*s
= chr
->opaque
;
2623 g_source_remove(s
->tag
);
2626 g_io_channel_unref(s
->chan
);
2630 if (s
->listen_fd
>= 0) {
2631 if (s
->listen_tag
) {
2632 g_source_remove(s
->listen_tag
);
2634 if (s
->listen_chan
) {
2635 g_io_channel_unref(s
->listen_chan
);
2637 closesocket(s
->listen_fd
);
2640 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2643 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2644 bool is_listen
, bool is_telnet
,
2645 bool is_waitconnect
,
2648 CharDriverState
*chr
= NULL
;
2649 TCPCharDriver
*s
= NULL
;
2650 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2651 const char *left
= "", *right
= "";
2652 struct sockaddr_storage ss
;
2653 socklen_t ss_len
= sizeof(ss
);
2655 memset(&ss
, 0, ss_len
);
2656 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2657 error_setg(errp
, "getsockname: %s", strerror(errno
));
2661 chr
= g_malloc0(sizeof(CharDriverState
));
2662 s
= g_malloc0(sizeof(TCPCharDriver
));
2669 chr
->filename
= g_malloc(256);
2670 switch (ss
.ss_family
) {
2674 snprintf(chr
->filename
, 256, "unix:%s%s",
2675 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2676 is_listen
? ",server" : "");
2684 s
->do_nodelay
= do_nodelay
;
2685 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2686 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2687 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2688 is_telnet
? "telnet" : "tcp",
2689 left
, host
, right
, serv
,
2690 is_listen
? ",server" : "");
2695 chr
->chr_write
= tcp_chr_write
;
2696 chr
->chr_close
= tcp_chr_close
;
2697 chr
->get_msgfd
= tcp_get_msgfd
;
2698 chr
->chr_add_client
= tcp_chr_add_client
;
2699 chr
->chr_add_watch
= tcp_chr_add_watch
;
2703 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2704 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2706 s
->do_telnetopt
= 1;
2711 socket_set_nodelay(fd
);
2712 s
->chan
= io_channel_from_socket(s
->fd
);
2713 tcp_chr_connect(chr
);
2716 if (is_listen
&& is_waitconnect
) {
2717 printf("QEMU waiting for connection on: %s\n",
2719 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2720 qemu_set_nonblock(s
->listen_fd
);
2725 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2727 CharDriverState
*chr
= NULL
;
2728 Error
*local_err
= NULL
;
2736 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2737 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2738 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2739 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2740 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2746 fd
= unix_listen_opts(opts
, &local_err
);
2748 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2752 fd
= inet_listen_opts(opts
, 0, &local_err
);
2754 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2761 if (!is_waitconnect
)
2762 qemu_set_nonblock(fd
);
2764 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2765 is_waitconnect
, &local_err
);
2766 if (error_is_set(&local_err
)) {
2774 qerror_report_err(local_err
);
2775 error_free(local_err
);
2781 g_free(chr
->opaque
);
2787 /*********************************************************/
2788 /* Ring buffer chardev */
2795 } RingBufCharDriver
;
2797 static size_t ringbuf_count(const CharDriverState
*chr
)
2799 const RingBufCharDriver
*d
= chr
->opaque
;
2801 return d
->prod
- d
->cons
;
2804 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2806 RingBufCharDriver
*d
= chr
->opaque
;
2809 if (!buf
|| (len
< 0)) {
2813 for (i
= 0; i
< len
; i
++ ) {
2814 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2815 if (d
->prod
- d
->cons
> d
->size
) {
2816 d
->cons
= d
->prod
- d
->size
;
2823 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2825 RingBufCharDriver
*d
= chr
->opaque
;
2828 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2829 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2835 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2837 RingBufCharDriver
*d
= chr
->opaque
;
2844 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2847 CharDriverState
*chr
;
2848 RingBufCharDriver
*d
;
2850 chr
= g_malloc0(sizeof(CharDriverState
));
2851 d
= g_malloc(sizeof(*d
));
2853 d
->size
= opts
->has_size
? opts
->size
: 65536;
2855 /* The size must be power of 2 */
2856 if (d
->size
& (d
->size
- 1)) {
2857 error_setg(errp
, "size of ringbuf chardev must be power of two");
2863 d
->cbuf
= g_malloc0(d
->size
);
2866 chr
->chr_write
= ringbuf_chr_write
;
2867 chr
->chr_close
= ringbuf_chr_close
;
2877 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2879 return chr
->chr_write
== ringbuf_chr_write
;
2882 void qmp_ringbuf_write(const char *device
, const char *data
,
2883 bool has_format
, enum DataFormat format
,
2886 CharDriverState
*chr
;
2887 const uint8_t *write_data
;
2891 chr
= qemu_chr_find(device
);
2893 error_setg(errp
, "Device '%s' not found", device
);
2897 if (!chr_is_ringbuf(chr
)) {
2898 error_setg(errp
,"%s is not a ringbuf device", device
);
2902 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2903 write_data
= g_base64_decode(data
, &write_count
);
2905 write_data
= (uint8_t *)data
;
2906 write_count
= strlen(data
);
2909 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2911 if (write_data
!= (uint8_t *)data
) {
2912 g_free((void *)write_data
);
2916 error_setg(errp
, "Failed to write to device %s", device
);
2921 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2922 bool has_format
, enum DataFormat format
,
2925 CharDriverState
*chr
;
2930 chr
= qemu_chr_find(device
);
2932 error_setg(errp
, "Device '%s' not found", device
);
2936 if (!chr_is_ringbuf(chr
)) {
2937 error_setg(errp
,"%s is not a ringbuf device", device
);
2942 error_setg(errp
, "size must be greater than zero");
2946 count
= ringbuf_count(chr
);
2947 size
= size
> count
? count
: size
;
2948 read_data
= g_malloc(size
+ 1);
2950 ringbuf_chr_read(chr
, read_data
, size
);
2952 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2953 data
= g_base64_encode(read_data
, size
);
2957 * FIXME should read only complete, valid UTF-8 characters up
2958 * to @size bytes. Invalid sequences should be replaced by a
2959 * suitable replacement character. Except when (and only
2960 * when) ring buffer lost characters since last read, initial
2961 * continuation characters should be dropped.
2963 read_data
[size
] = 0;
2964 data
= (char *)read_data
;
2970 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2972 char host
[65], port
[33], width
[8], height
[8];
2976 Error
*local_err
= NULL
;
2978 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2979 if (error_is_set(&local_err
)) {
2980 qerror_report_err(local_err
);
2981 error_free(local_err
);
2985 if (strstart(filename
, "mon:", &p
)) {
2987 qemu_opt_set(opts
, "mux", "on");
2990 if (strcmp(filename
, "null") == 0 ||
2991 strcmp(filename
, "pty") == 0 ||
2992 strcmp(filename
, "msmouse") == 0 ||
2993 strcmp(filename
, "braille") == 0 ||
2994 strcmp(filename
, "stdio") == 0) {
2995 qemu_opt_set(opts
, "backend", filename
);
2998 if (strstart(filename
, "vc", &p
)) {
2999 qemu_opt_set(opts
, "backend", "vc");
3001 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3003 qemu_opt_set(opts
, "width", width
);
3004 qemu_opt_set(opts
, "height", height
);
3005 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3007 qemu_opt_set(opts
, "cols", width
);
3008 qemu_opt_set(opts
, "rows", height
);
3015 if (strcmp(filename
, "con:") == 0) {
3016 qemu_opt_set(opts
, "backend", "console");
3019 if (strstart(filename
, "COM", NULL
)) {
3020 qemu_opt_set(opts
, "backend", "serial");
3021 qemu_opt_set(opts
, "path", filename
);
3024 if (strstart(filename
, "file:", &p
)) {
3025 qemu_opt_set(opts
, "backend", "file");
3026 qemu_opt_set(opts
, "path", p
);
3029 if (strstart(filename
, "pipe:", &p
)) {
3030 qemu_opt_set(opts
, "backend", "pipe");
3031 qemu_opt_set(opts
, "path", p
);
3034 if (strstart(filename
, "tcp:", &p
) ||
3035 strstart(filename
, "telnet:", &p
)) {
3036 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3038 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3041 qemu_opt_set(opts
, "backend", "socket");
3042 qemu_opt_set(opts
, "host", host
);
3043 qemu_opt_set(opts
, "port", port
);
3044 if (p
[pos
] == ',') {
3045 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3048 if (strstart(filename
, "telnet:", &p
))
3049 qemu_opt_set(opts
, "telnet", "on");
3052 if (strstart(filename
, "udp:", &p
)) {
3053 qemu_opt_set(opts
, "backend", "udp");
3054 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3056 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3060 qemu_opt_set(opts
, "host", host
);
3061 qemu_opt_set(opts
, "port", port
);
3062 if (p
[pos
] == '@') {
3064 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3066 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3070 qemu_opt_set(opts
, "localaddr", host
);
3071 qemu_opt_set(opts
, "localport", port
);
3075 if (strstart(filename
, "unix:", &p
)) {
3076 qemu_opt_set(opts
, "backend", "socket");
3077 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3081 if (strstart(filename
, "/dev/parport", NULL
) ||
3082 strstart(filename
, "/dev/ppi", NULL
)) {
3083 qemu_opt_set(opts
, "backend", "parport");
3084 qemu_opt_set(opts
, "path", filename
);
3087 if (strstart(filename
, "/dev/", NULL
)) {
3088 qemu_opt_set(opts
, "backend", "tty");
3089 qemu_opt_set(opts
, "path", filename
);
3094 qemu_opts_del(opts
);
3098 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3101 const char *path
= qemu_opt_get(opts
, "path");
3104 error_setg(errp
, "chardev: file: no filename given");
3107 backend
->file
= g_new0(ChardevFile
, 1);
3108 backend
->file
->out
= g_strdup(path
);
3111 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3114 backend
->stdio
= g_new0(ChardevStdio
, 1);
3115 backend
->stdio
->has_signal
= true;
3116 backend
->stdio
->signal
=
3117 qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
);
3120 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3123 const char *device
= qemu_opt_get(opts
, "path");
3125 if (device
== NULL
) {
3126 error_setg(errp
, "chardev: serial/tty: no device path given");
3129 backend
->serial
= g_new0(ChardevHostdev
, 1);
3130 backend
->serial
->device
= g_strdup(device
);
3133 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3136 const char *device
= qemu_opt_get(opts
, "path");
3138 if (device
== NULL
) {
3139 error_setg(errp
, "chardev: parallel: no device path given");
3142 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3143 backend
->parallel
->device
= g_strdup(device
);
3146 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3149 const char *device
= qemu_opt_get(opts
, "path");
3151 if (device
== NULL
) {
3152 error_setg(errp
, "chardev: pipe: no device path given");
3155 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3156 backend
->pipe
->device
= g_strdup(device
);
3159 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3164 backend
->memory
= g_new0(ChardevRingbuf
, 1);
3166 val
= qemu_opt_get_number(opts
, "size", 0);
3168 backend
->memory
->has_size
= true;
3169 backend
->memory
->size
= val
;
3173 typedef struct CharDriver
{
3176 CharDriverState
*(*open
)(QemuOpts
*opts
);
3177 /* new, qapi-based */
3179 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3182 static GSList
*backends
;
3184 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3188 s
= g_malloc0(sizeof(*s
));
3189 s
->name
= g_strdup(name
);
3192 backends
= g_slist_append(backends
, s
);
3195 void register_char_driver_qapi(const char *name
, int kind
,
3196 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3200 s
= g_malloc0(sizeof(*s
));
3201 s
->name
= g_strdup(name
);
3205 backends
= g_slist_append(backends
, s
);
3208 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3209 void (*init
)(struct CharDriverState
*s
),
3213 CharDriverState
*chr
;
3216 if (qemu_opts_id(opts
) == NULL
) {
3217 error_setg(errp
, "chardev: no id specified");
3221 if (qemu_opt_get(opts
, "backend") == NULL
) {
3222 error_setg(errp
, "chardev: \"%s\" missing backend",
3223 qemu_opts_id(opts
));
3226 for (i
= backends
; i
; i
= i
->next
) {
3229 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3234 error_setg(errp
, "chardev: backend \"%s\" not found",
3235 qemu_opt_get(opts
, "backend"));
3240 /* using new, qapi init */
3241 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3242 ChardevReturn
*ret
= NULL
;
3243 const char *id
= qemu_opts_id(opts
);
3244 const char *bid
= NULL
;
3246 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3247 bid
= g_strdup_printf("%s-base", id
);
3251 backend
->kind
= cd
->kind
;
3253 cd
->parse(opts
, backend
, errp
);
3254 if (error_is_set(errp
)) {
3258 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3259 if (error_is_set(errp
)) {
3264 qapi_free_ChardevBackend(backend
);
3265 qapi_free_ChardevReturn(ret
);
3266 backend
= g_new0(ChardevBackend
, 1);
3267 backend
->mux
= g_new0(ChardevMux
, 1);
3268 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3269 backend
->mux
->chardev
= g_strdup(bid
);
3270 ret
= qmp_chardev_add(id
, backend
, errp
);
3271 if (error_is_set(errp
)) {
3276 chr
= qemu_chr_find(id
);
3279 qapi_free_ChardevBackend(backend
);
3280 qapi_free_ChardevReturn(ret
);
3284 chr
= cd
->open(opts
);
3286 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3287 qemu_opt_get(opts
, "backend"));
3292 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3294 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3296 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3297 CharDriverState
*base
= chr
;
3298 int len
= strlen(qemu_opts_id(opts
)) + 6;
3299 base
->label
= g_malloc(len
);
3300 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3301 chr
= qemu_chr_open_mux(base
);
3302 chr
->filename
= base
->filename
;
3303 chr
->avail_connections
= MAX_MUX
;
3304 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3306 chr
->avail_connections
= 1;
3308 chr
->label
= g_strdup(qemu_opts_id(opts
));
3313 qemu_opts_del(opts
);
3317 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3320 CharDriverState
*chr
;
3324 if (strstart(filename
, "chardev:", &p
)) {
3325 return qemu_chr_find(p
);
3328 opts
= qemu_chr_parse_compat(label
, filename
);
3332 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3333 if (error_is_set(&err
)) {
3334 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3337 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3338 qemu_chr_fe_claim_no_fail(chr
);
3339 monitor_init(chr
, MONITOR_USE_READLINE
);
3344 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3346 if (chr
->chr_set_echo
) {
3347 chr
->chr_set_echo(chr
, echo
);
3351 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3353 if (chr
->fe_open
== fe_open
) {
3356 chr
->fe_open
= fe_open
;
3357 if (chr
->chr_set_fe_open
) {
3358 chr
->chr_set_fe_open(chr
, fe_open
);
3362 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3363 GIOFunc func
, void *user_data
)
3368 if (s
->chr_add_watch
== NULL
) {
3372 src
= s
->chr_add_watch(s
, cond
);
3373 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3374 tag
= g_source_attach(src
, NULL
);
3375 g_source_unref(src
);
3380 int qemu_chr_fe_claim(CharDriverState
*s
)
3382 if (s
->avail_connections
< 1) {
3385 s
->avail_connections
--;
3389 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3391 if (qemu_chr_fe_claim(s
) != 0) {
3392 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3393 __func__
, s
->label
);
3398 void qemu_chr_fe_release(CharDriverState
*s
)
3400 s
->avail_connections
++;
3403 void qemu_chr_delete(CharDriverState
*chr
)
3405 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3406 if (chr
->chr_close
) {
3407 chr
->chr_close(chr
);
3409 g_free(chr
->filename
);
3412 qemu_opts_del(chr
->opts
);
3417 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3419 ChardevInfoList
*chr_list
= NULL
;
3420 CharDriverState
*chr
;
3422 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3423 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3424 info
->value
= g_malloc0(sizeof(*info
->value
));
3425 info
->value
->label
= g_strdup(chr
->label
);
3426 info
->value
->filename
= g_strdup(chr
->filename
);
3428 info
->next
= chr_list
;
3435 CharDriverState
*qemu_chr_find(const char *name
)
3437 CharDriverState
*chr
;
3439 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3440 if (strcmp(chr
->label
, name
) != 0)
3447 /* Get a character (serial) device interface. */
3448 CharDriverState
*qemu_char_get_next_serial(void)
3450 static int next_serial
;
3451 CharDriverState
*chr
;
3453 /* FIXME: This function needs to go away: use chardev properties! */
3455 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3456 chr
= serial_hds
[next_serial
++];
3457 qemu_chr_fe_claim_no_fail(chr
);
3463 QemuOptsList qemu_chardev_opts
= {
3465 .implied_opt_name
= "backend",
3466 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3470 .type
= QEMU_OPT_STRING
,
3473 .type
= QEMU_OPT_STRING
,
3476 .type
= QEMU_OPT_STRING
,
3479 .type
= QEMU_OPT_STRING
,
3481 .name
= "localaddr",
3482 .type
= QEMU_OPT_STRING
,
3484 .name
= "localport",
3485 .type
= QEMU_OPT_STRING
,
3488 .type
= QEMU_OPT_NUMBER
,
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_BOOL
,
3509 .type
= QEMU_OPT_NUMBER
,
3512 .type
= QEMU_OPT_NUMBER
,
3515 .type
= QEMU_OPT_NUMBER
,
3518 .type
= QEMU_OPT_NUMBER
,
3521 .type
= QEMU_OPT_BOOL
,
3524 .type
= QEMU_OPT_BOOL
,
3527 .type
= QEMU_OPT_STRING
,
3530 .type
= QEMU_OPT_NUMBER
,
3533 .type
= QEMU_OPT_SIZE
,
3535 { /* end of list */ }
3541 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3546 error_setg(errp
, "input file not supported");
3550 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3551 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3552 if (out
== INVALID_HANDLE_VALUE
) {
3553 error_setg(errp
, "open %s failed", file
->out
);
3556 return qemu_chr_open_win_file(out
);
3559 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3562 return qemu_chr_open_win_path(serial
->device
);
3565 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3568 error_setg(errp
, "character device backend type 'parallel' not supported");
3574 static int qmp_chardev_open_file_source(char *src
, int flags
,
3579 TFR(fd
= qemu_open(src
, flags
, 0666));
3581 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3586 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3588 int flags
, in
= -1, out
= -1;
3590 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3591 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3592 if (error_is_set(errp
)) {
3598 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3599 if (error_is_set(errp
)) {
3605 return qemu_chr_open_fd(in
, out
);
3608 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3611 #ifdef HAVE_CHARDEV_TTY
3614 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3615 if (error_is_set(errp
)) {
3618 qemu_set_nonblock(fd
);
3619 return qemu_chr_open_tty_fd(fd
);
3621 error_setg(errp
, "character device backend type 'serial' not supported");
3626 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3629 #ifdef HAVE_CHARDEV_PARPORT
3632 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3633 if (error_is_set(errp
)) {
3636 return qemu_chr_open_pp_fd(fd
);
3638 error_setg(errp
, "character device backend type 'parallel' not supported");
3645 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3648 SocketAddress
*addr
= sock
->addr
;
3649 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3650 bool is_listen
= sock
->has_server
? sock
->server
: true;
3651 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3652 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3656 fd
= socket_listen(addr
, errp
);
3658 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3660 if (error_is_set(errp
)) {
3663 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3664 is_telnet
, is_waitconnect
, errp
);
3667 static CharDriverState
*qmp_chardev_open_dgram(ChardevDgram
*dgram
,
3672 fd
= socket_dgram(dgram
->remote
, dgram
->local
, errp
);
3673 if (error_is_set(errp
)) {
3676 return qemu_chr_open_udp_fd(fd
);
3679 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3682 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3683 CharDriverState
*base
, *chr
= NULL
;
3685 chr
= qemu_chr_find(id
);
3687 error_setg(errp
, "Chardev '%s' already exists", id
);
3692 switch (backend
->kind
) {
3693 case CHARDEV_BACKEND_KIND_FILE
:
3694 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3696 case CHARDEV_BACKEND_KIND_SERIAL
:
3697 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3699 case CHARDEV_BACKEND_KIND_PARALLEL
:
3700 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3702 case CHARDEV_BACKEND_KIND_PIPE
:
3703 chr
= qemu_chr_open_pipe(backend
->pipe
);
3705 case CHARDEV_BACKEND_KIND_SOCKET
:
3706 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3708 case CHARDEV_BACKEND_KIND_DGRAM
:
3709 chr
= qmp_chardev_open_dgram(backend
->dgram
, errp
);
3711 #ifdef HAVE_CHARDEV_TTY
3712 case CHARDEV_BACKEND_KIND_PTY
:
3713 chr
= qemu_chr_open_pty(id
, ret
);
3716 case CHARDEV_BACKEND_KIND_NULL
:
3717 chr
= qemu_chr_open_null();
3719 case CHARDEV_BACKEND_KIND_MUX
:
3720 base
= qemu_chr_find(backend
->mux
->chardev
);
3722 error_setg(errp
, "mux: base chardev %s not found",
3723 backend
->mux
->chardev
);
3726 chr
= qemu_chr_open_mux(base
);
3728 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3729 chr
= qemu_chr_open_msmouse();
3731 #ifdef CONFIG_BRLAPI
3732 case CHARDEV_BACKEND_KIND_BRAILLE
:
3733 chr
= chr_baum_init();
3736 case CHARDEV_BACKEND_KIND_STDIO
:
3737 chr
= qemu_chr_open_stdio(backend
->stdio
);
3740 case CHARDEV_BACKEND_KIND_CONSOLE
:
3741 chr
= qemu_chr_open_win_con();
3745 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3746 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3748 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3749 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3752 case CHARDEV_BACKEND_KIND_VC
:
3753 chr
= vc_init(backend
->vc
);
3755 case CHARDEV_BACKEND_KIND_MEMORY
:
3756 chr
= qemu_chr_open_ringbuf(backend
->memory
, errp
);
3759 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3763 if (chr
== NULL
&& !error_is_set(errp
)) {
3764 error_setg(errp
, "Failed to create chardev");
3767 chr
->label
= g_strdup(id
);
3768 chr
->avail_connections
=
3769 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3770 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3778 void qmp_chardev_remove(const char *id
, Error
**errp
)
3780 CharDriverState
*chr
;
3782 chr
= qemu_chr_find(id
);
3784 error_setg(errp
, "Chardev '%s' not found", id
);
3787 if (chr
->chr_can_read
|| chr
->chr_read
||
3788 chr
->chr_event
|| chr
->handler_opaque
) {
3789 error_setg(errp
, "Chardev '%s' is busy", id
);
3792 qemu_chr_delete(chr
);
3795 static void register_types(void)
3797 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3798 register_char_driver("socket", qemu_chr_open_socket
);
3799 register_char_driver("udp", qemu_chr_open_udp
);
3800 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3801 qemu_chr_parse_ringbuf
);
3802 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3803 qemu_chr_parse_file_out
);
3804 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3805 qemu_chr_parse_stdio
);
3806 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3807 qemu_chr_parse_serial
);
3808 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3809 qemu_chr_parse_serial
);
3810 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3811 qemu_chr_parse_parallel
);
3812 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3813 qemu_chr_parse_parallel
);
3814 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3815 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3816 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3817 qemu_chr_parse_pipe
);
3820 type_init(register_types
);