4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "ui/console.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/timer.h"
29 #include "sysemu/char.h"
31 #include "qmp-commands.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <arpa/inet.h>
53 #include <sys/select.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57 #include <dev/ppbus/ppi.h>
58 #include <dev/ppbus/ppbconf.h>
59 #elif defined(__DragonFly__)
60 #include <dev/misc/ppi/ppi.h>
61 #include <bus/ppbus/ppbconf.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
83 #include "qemu/sockets.h"
84 #include "ui/qemu-spice.h"
86 #define READ_BUF_LEN 4096
88 /***********************************************************/
89 /* character device */
91 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
92 QTAILQ_HEAD_INITIALIZER(chardevs
);
94 void qemu_chr_be_event(CharDriverState
*s
, int event
)
96 /* Keep track if the char device is open */
98 case CHR_EVENT_OPENED
:
101 case CHR_EVENT_CLOSED
:
108 s
->chr_event(s
->handler_opaque
, event
);
111 void qemu_chr_be_generic_open(CharDriverState
*s
)
113 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
116 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
118 return s
->chr_write(s
, buf
, len
);
121 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
126 while (offset
< len
) {
128 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
129 if (res
== -1 && errno
== EAGAIN
) {
132 } while (res
== -1 && errno
== EAGAIN
);
148 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
152 return s
->chr_ioctl(s
, cmd
, arg
);
155 int qemu_chr_be_can_write(CharDriverState
*s
)
157 if (!s
->chr_can_read
)
159 return s
->chr_can_read(s
->handler_opaque
);
162 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
165 s
->chr_read(s
->handler_opaque
, buf
, len
);
169 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
171 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
174 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
176 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
179 void qemu_chr_accept_input(CharDriverState
*s
)
181 if (s
->chr_accept_input
)
182 s
->chr_accept_input(s
);
186 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
188 char buf
[READ_BUF_LEN
];
191 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
192 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
196 void qemu_chr_add_handlers(CharDriverState
*s
,
197 IOCanReadHandler
*fd_can_read
,
198 IOReadHandler
*fd_read
,
199 IOEventHandler
*fd_event
,
204 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
209 s
->chr_can_read
= fd_can_read
;
210 s
->chr_read
= fd_read
;
211 s
->chr_event
= fd_event
;
212 s
->handler_opaque
= opaque
;
213 if (s
->chr_update_read_handler
)
214 s
->chr_update_read_handler(s
);
216 if (!s
->explicit_fe_open
) {
217 qemu_chr_fe_set_open(s
, fe_open
);
220 /* We're connecting to an already opened device, so let's make sure we
221 also get the open event */
222 if (fe_open
&& s
->be_open
) {
223 qemu_chr_be_generic_open(s
);
227 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
232 static CharDriverState
*qemu_chr_open_null(void)
234 CharDriverState
*chr
;
236 chr
= g_malloc0(sizeof(CharDriverState
));
237 chr
->chr_write
= null_chr_write
;
238 chr
->explicit_be_open
= true;
242 /* MUX driver for serial I/O splitting */
244 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
245 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
247 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
248 IOReadHandler
*chr_read
[MAX_MUX
];
249 IOEventHandler
*chr_event
[MAX_MUX
];
250 void *ext_opaque
[MAX_MUX
];
251 CharDriverState
*drv
;
256 /* Intermediate input buffer allows to catch escape sequences even if the
257 currently active device is not accepting any input - but only until it
259 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
264 int64_t timestamps_start
;
268 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
270 MuxDriver
*d
= chr
->opaque
;
272 if (!d
->timestamps
) {
273 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
278 for (i
= 0; i
< len
; i
++) {
284 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
285 if (d
->timestamps_start
== -1)
286 d
->timestamps_start
= ti
;
287 ti
-= d
->timestamps_start
;
289 snprintf(buf1
, sizeof(buf1
),
290 "[%02d:%02d:%02d.%03d] ",
295 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
298 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
299 if (buf
[i
] == '\n') {
307 static const char * const mux_help
[] = {
308 "% h print this help\n\r",
309 "% x exit emulator\n\r",
310 "% s save disk data back to file (if -snapshot)\n\r",
311 "% t toggle console timestamps\n\r"
312 "% b send break (magic sysrq)\n\r",
313 "% c switch between console and monitor\n\r",
318 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
319 static void mux_print_help(CharDriverState
*chr
)
322 char ebuf
[15] = "Escape-Char";
323 char cbuf
[50] = "\n\r";
325 if (term_escape_char
> 0 && term_escape_char
< 26) {
326 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
327 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
329 snprintf(cbuf
, sizeof(cbuf
),
330 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
333 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
334 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
335 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
336 if (mux_help
[i
][j
] == '%')
337 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
339 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
344 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
346 if (d
->chr_event
[mux_nr
])
347 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
350 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
352 if (d
->term_got_escape
) {
353 d
->term_got_escape
= 0;
354 if (ch
== term_escape_char
)
363 const char *term
= "QEMU: Terminated\n\r";
364 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
372 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
375 /* Switch to the next registered device */
376 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
378 if (d
->focus
>= d
->mux_cnt
)
380 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
383 d
->timestamps
= !d
->timestamps
;
384 d
->timestamps_start
= -1;
388 } else if (ch
== term_escape_char
) {
389 d
->term_got_escape
= 1;
397 static void mux_chr_accept_input(CharDriverState
*chr
)
399 MuxDriver
*d
= chr
->opaque
;
402 while (d
->prod
[m
] != d
->cons
[m
] &&
403 d
->chr_can_read
[m
] &&
404 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
405 d
->chr_read
[m
](d
->ext_opaque
[m
],
406 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
410 static int mux_chr_can_read(void *opaque
)
412 CharDriverState
*chr
= opaque
;
413 MuxDriver
*d
= chr
->opaque
;
416 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
418 if (d
->chr_can_read
[m
])
419 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
423 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
425 CharDriverState
*chr
= opaque
;
426 MuxDriver
*d
= chr
->opaque
;
430 mux_chr_accept_input (opaque
);
432 for(i
= 0; i
< size
; i
++)
433 if (mux_proc_byte(chr
, d
, buf
[i
])) {
434 if (d
->prod
[m
] == d
->cons
[m
] &&
435 d
->chr_can_read
[m
] &&
436 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
437 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
439 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
443 static void mux_chr_event(void *opaque
, int event
)
445 CharDriverState
*chr
= opaque
;
446 MuxDriver
*d
= chr
->opaque
;
449 /* Send the event to all registered listeners */
450 for (i
= 0; i
< d
->mux_cnt
; i
++)
451 mux_chr_send_event(d
, i
, event
);
454 static void mux_chr_update_read_handler(CharDriverState
*chr
)
456 MuxDriver
*d
= chr
->opaque
;
458 if (d
->mux_cnt
>= MAX_MUX
) {
459 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
462 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
463 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
464 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
465 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
466 /* Fix up the real driver with mux routines */
467 if (d
->mux_cnt
== 0) {
468 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
471 if (d
->focus
!= -1) {
472 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
474 d
->focus
= d
->mux_cnt
;
476 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
479 static bool muxes_realized
;
482 * Called after processing of default and command-line-specified
483 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
484 * to a mux chardev. This is done here to ensure that
485 * output/prompts/banners are only displayed for the FE that has
486 * focus when initial command-line processing/machine init is
489 * After this point, any new FE attached to any new or existing
490 * mux will receive CHR_EVENT_OPENED notifications for the BE
493 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
495 CharDriverState
*chr
;
497 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
499 MuxDriver
*d
= chr
->opaque
;
502 /* send OPENED to all already-attached FEs */
503 for (i
= 0; i
< d
->mux_cnt
; i
++) {
504 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
506 /* mark mux as OPENED so any new FEs will immediately receive
509 qemu_chr_be_generic_open(chr
);
512 muxes_realized
= true;
515 static Notifier muxes_realize_notify
= {
516 .notify
= muxes_realize_done
,
519 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
521 CharDriverState
*chr
;
524 chr
= g_malloc0(sizeof(CharDriverState
));
525 d
= g_malloc0(sizeof(MuxDriver
));
530 chr
->chr_write
= mux_chr_write
;
531 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
532 chr
->chr_accept_input
= mux_chr_accept_input
;
533 /* Frontend guest-open / -close notification is not support with muxes */
534 chr
->chr_set_fe_open
= NULL
;
535 /* only default to opened state if we've realized the initial
538 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
546 int send_all(int fd
, const void *buf
, int len1
)
552 ret
= send(fd
, buf
, len
, 0);
554 errno
= WSAGetLastError();
555 if (errno
!= WSAEWOULDBLOCK
) {
558 } else if (ret
== 0) {
570 int send_all(int fd
, const void *_buf
, int len1
)
573 const uint8_t *buf
= _buf
;
577 ret
= write(fd
, buf
, len
);
579 if (errno
!= EINTR
&& errno
!= EAGAIN
)
581 } else if (ret
== 0) {
591 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
597 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
599 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
616 typedef struct IOWatchPoll
623 IOCanReadHandler
*fd_can_read
;
628 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
630 return container_of(source
, IOWatchPoll
, parent
);
633 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
635 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
636 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
637 bool was_active
= iwp
->src
!= NULL
;
638 if (was_active
== now_active
) {
643 iwp
->src
= g_io_create_watch(iwp
->channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
644 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
645 g_source_attach(iwp
->src
, NULL
);
647 g_source_destroy(iwp
->src
);
648 g_source_unref(iwp
->src
);
654 static gboolean
io_watch_poll_check(GSource
*source
)
659 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
665 static void io_watch_poll_finalize(GSource
*source
)
667 /* Due to a glib bug, removing the last reference to a source
668 * inside a finalize callback causes recursive locking (and a
669 * deadlock). This is not a problem inside other callbacks,
670 * including dispatch callbacks, so we call io_remove_watch_poll
671 * to remove this source. At this point, iwp->src must
672 * be NULL, or we would leak it.
674 * This would be solved much more elegantly by child sources,
675 * but we support older glib versions that do not have them.
677 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
678 assert(iwp
->src
== NULL
);
681 static GSourceFuncs io_watch_poll_funcs
= {
682 .prepare
= io_watch_poll_prepare
,
683 .check
= io_watch_poll_check
,
684 .dispatch
= io_watch_poll_dispatch
,
685 .finalize
= io_watch_poll_finalize
,
688 /* Can only be used for read */
689 static guint
io_add_watch_poll(GIOChannel
*channel
,
690 IOCanReadHandler
*fd_can_read
,
697 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
698 iwp
->fd_can_read
= fd_can_read
;
699 iwp
->opaque
= user_data
;
700 iwp
->channel
= channel
;
701 iwp
->fd_read
= (GSourceFunc
) fd_read
;
704 tag
= g_source_attach(&iwp
->parent
, NULL
);
705 g_source_unref(&iwp
->parent
);
709 static void io_remove_watch_poll(guint tag
)
714 g_return_if_fail (tag
> 0);
716 source
= g_main_context_find_source_by_id(NULL
, tag
);
717 g_return_if_fail (source
!= NULL
);
719 iwp
= io_watch_poll_from_source(source
);
721 g_source_destroy(iwp
->src
);
722 g_source_unref(iwp
->src
);
725 g_source_destroy(&iwp
->parent
);
729 static GIOChannel
*io_channel_from_fd(int fd
)
737 chan
= g_io_channel_unix_new(fd
);
739 g_io_channel_set_encoding(chan
, NULL
, NULL
);
740 g_io_channel_set_buffered(chan
, FALSE
);
746 static GIOChannel
*io_channel_from_socket(int fd
)
755 chan
= g_io_channel_win32_new_socket(fd
);
757 chan
= g_io_channel_unix_new(fd
);
760 g_io_channel_set_encoding(chan
, NULL
, NULL
);
761 g_io_channel_set_buffered(chan
, FALSE
);
766 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
769 GIOStatus status
= G_IO_STATUS_NORMAL
;
771 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
772 gsize bytes_written
= 0;
774 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
775 &bytes_written
, NULL
);
776 offset
+= bytes_written
;
783 case G_IO_STATUS_NORMAL
:
786 case G_IO_STATUS_AGAIN
:
798 typedef struct FDCharDriver
{
799 CharDriverState
*chr
;
800 GIOChannel
*fd_in
, *fd_out
;
803 QTAILQ_ENTRY(FDCharDriver
) node
;
806 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
808 FDCharDriver
*s
= chr
->opaque
;
810 return io_channel_send(s
->fd_out
, buf
, len
);
813 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
815 CharDriverState
*chr
= opaque
;
816 FDCharDriver
*s
= chr
->opaque
;
818 uint8_t buf
[READ_BUF_LEN
];
823 if (len
> s
->max_size
) {
830 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
831 len
, &bytes_read
, NULL
);
832 if (status
== G_IO_STATUS_EOF
) {
834 io_remove_watch_poll(s
->fd_in_tag
);
837 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
840 if (status
== G_IO_STATUS_NORMAL
) {
841 qemu_chr_be_write(chr
, buf
, bytes_read
);
847 static int fd_chr_read_poll(void *opaque
)
849 CharDriverState
*chr
= opaque
;
850 FDCharDriver
*s
= chr
->opaque
;
852 s
->max_size
= qemu_chr_be_can_write(chr
);
856 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
858 FDCharDriver
*s
= chr
->opaque
;
859 return g_io_create_watch(s
->fd_out
, cond
);
862 static void fd_chr_update_read_handler(CharDriverState
*chr
)
864 FDCharDriver
*s
= chr
->opaque
;
867 io_remove_watch_poll(s
->fd_in_tag
);
872 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
876 static void fd_chr_close(struct CharDriverState
*chr
)
878 FDCharDriver
*s
= chr
->opaque
;
881 io_remove_watch_poll(s
->fd_in_tag
);
886 g_io_channel_unref(s
->fd_in
);
889 g_io_channel_unref(s
->fd_out
);
893 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
896 /* open a character device to a unix fd */
897 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
899 CharDriverState
*chr
;
902 chr
= g_malloc0(sizeof(CharDriverState
));
903 s
= g_malloc0(sizeof(FDCharDriver
));
904 s
->fd_in
= io_channel_from_fd(fd_in
);
905 s
->fd_out
= io_channel_from_fd(fd_out
);
906 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
909 chr
->chr_add_watch
= fd_chr_add_watch
;
910 chr
->chr_write
= fd_chr_write
;
911 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
912 chr
->chr_close
= fd_chr_close
;
917 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
920 char filename_in
[256], filename_out
[256];
921 const char *filename
= opts
->device
;
923 if (filename
== NULL
) {
924 fprintf(stderr
, "chardev: pipe: no filename given\n");
928 snprintf(filename_in
, 256, "%s.in", filename
);
929 snprintf(filename_out
, 256, "%s.out", filename
);
930 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
931 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
932 if (fd_in
< 0 || fd_out
< 0) {
937 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
942 return qemu_chr_open_fd(fd_in
, fd_out
);
945 /* init terminal so that we can grab keys */
946 static struct termios oldtty
;
947 static int old_fd0_flags
;
948 static bool stdio_allow_signal
;
950 static void term_exit(void)
952 tcsetattr (0, TCSANOW
, &oldtty
);
953 fcntl(0, F_SETFL
, old_fd0_flags
);
956 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
962 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
963 |INLCR
|IGNCR
|ICRNL
|IXON
);
964 tty
.c_oflag
|= OPOST
;
965 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
966 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
971 if (!stdio_allow_signal
)
972 tty
.c_lflag
&= ~ISIG
;
974 tcsetattr (0, TCSANOW
, &tty
);
977 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
983 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
985 CharDriverState
*chr
;
987 if (is_daemonized()) {
988 error_report("cannot use stdio with -daemonize");
991 old_fd0_flags
= fcntl(0, F_GETFL
);
992 tcgetattr (0, &oldtty
);
993 fcntl(0, F_SETFL
, O_NONBLOCK
);
996 chr
= qemu_chr_open_fd(0, 1);
997 chr
->chr_close
= qemu_chr_close_stdio
;
998 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
999 if (opts
->has_signal
) {
1000 stdio_allow_signal
= opts
->signal
;
1002 qemu_chr_fe_set_echo(chr
, false);
1007 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1008 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1009 || defined(__GLIBC__)
1011 #define HAVE_CHARDEV_TTY 1
1021 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1022 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1024 static gboolean
pty_chr_timer(gpointer opaque
)
1026 struct CharDriverState
*chr
= opaque
;
1027 PtyCharDriver
*s
= chr
->opaque
;
1034 pty_chr_update_read_handler(chr
);
1041 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1043 PtyCharDriver
*s
= chr
->opaque
;
1046 g_source_remove(s
->timer_tag
);
1051 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1053 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1057 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1059 PtyCharDriver
*s
= chr
->opaque
;
1061 if (!s
->connected
) {
1062 /* guest sends data, check for (re-)connect */
1063 pty_chr_update_read_handler(chr
);
1066 return io_channel_send(s
->fd
, buf
, len
);
1069 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1071 PtyCharDriver
*s
= chr
->opaque
;
1072 return g_io_create_watch(s
->fd
, cond
);
1075 static int pty_chr_read_poll(void *opaque
)
1077 CharDriverState
*chr
= opaque
;
1078 PtyCharDriver
*s
= chr
->opaque
;
1080 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1081 return s
->read_bytes
;
1084 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1086 CharDriverState
*chr
= opaque
;
1087 PtyCharDriver
*s
= chr
->opaque
;
1089 uint8_t buf
[READ_BUF_LEN
];
1093 if (len
> s
->read_bytes
)
1094 len
= s
->read_bytes
;
1098 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1099 if (status
!= G_IO_STATUS_NORMAL
) {
1100 pty_chr_state(chr
, 0);
1103 pty_chr_state(chr
, 1);
1104 qemu_chr_be_write(chr
, buf
, size
);
1109 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1111 PtyCharDriver
*s
= chr
->opaque
;
1114 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1115 pfd
.events
= G_IO_OUT
;
1118 if (pfd
.revents
& G_IO_HUP
) {
1119 pty_chr_state(chr
, 0);
1121 pty_chr_state(chr
, 1);
1125 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1127 PtyCharDriver
*s
= chr
->opaque
;
1131 io_remove_watch_poll(s
->fd_tag
);
1135 /* (re-)connect poll interval for idle guests: once per second.
1136 * We check more frequently in case the guests sends data to
1137 * the virtual device linked to our pty. */
1138 pty_chr_rearm_timer(chr
, 1000);
1141 g_source_remove(s
->timer_tag
);
1144 if (!s
->connected
) {
1146 qemu_chr_be_generic_open(chr
);
1147 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1153 static void pty_chr_close(struct CharDriverState
*chr
)
1155 PtyCharDriver
*s
= chr
->opaque
;
1159 io_remove_watch_poll(s
->fd_tag
);
1162 fd
= g_io_channel_unix_get_fd(s
->fd
);
1163 g_io_channel_unref(s
->fd
);
1166 g_source_remove(s
->timer_tag
);
1170 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1173 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1176 CharDriverState
*chr
;
1178 int master_fd
, slave_fd
;
1179 char pty_name
[PATH_MAX
];
1181 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1182 if (master_fd
< 0) {
1188 chr
= g_malloc0(sizeof(CharDriverState
));
1190 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1191 ret
->pty
= g_strdup(pty_name
);
1192 ret
->has_pty
= true;
1194 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1197 s
= g_malloc0(sizeof(PtyCharDriver
));
1199 chr
->chr_write
= pty_chr_write
;
1200 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1201 chr
->chr_close
= pty_chr_close
;
1202 chr
->chr_add_watch
= pty_chr_add_watch
;
1203 chr
->explicit_be_open
= true;
1205 s
->fd
= io_channel_from_fd(master_fd
);
1211 static void tty_serial_init(int fd
, int speed
,
1212 int parity
, int data_bits
, int stop_bits
)
1218 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1219 speed
, parity
, data_bits
, stop_bits
);
1221 tcgetattr (fd
, &tty
);
1223 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1224 speed
= speed
* 10 / 11;
1241 /* Non-Posix values follow. They may be unsupported on some systems. */
1243 check_speed(115200);
1245 check_speed(230400);
1248 check_speed(460800);
1251 check_speed(500000);
1254 check_speed(576000);
1257 check_speed(921600);
1260 check_speed(1000000);
1263 check_speed(1152000);
1266 check_speed(1500000);
1269 check_speed(2000000);
1272 check_speed(2500000);
1275 check_speed(3000000);
1278 check_speed(3500000);
1281 check_speed(4000000);
1286 cfsetispeed(&tty
, spd
);
1287 cfsetospeed(&tty
, spd
);
1289 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1290 |INLCR
|IGNCR
|ICRNL
|IXON
);
1291 tty
.c_oflag
|= OPOST
;
1292 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1293 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1314 tty
.c_cflag
|= PARENB
;
1317 tty
.c_cflag
|= PARENB
| PARODD
;
1321 tty
.c_cflag
|= CSTOPB
;
1323 tcsetattr (fd
, TCSANOW
, &tty
);
1326 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1328 FDCharDriver
*s
= chr
->opaque
;
1331 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1333 QEMUSerialSetParams
*ssp
= arg
;
1334 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1335 ssp
->speed
, ssp
->parity
,
1336 ssp
->data_bits
, ssp
->stop_bits
);
1339 case CHR_IOCTL_SERIAL_SET_BREAK
:
1341 int enable
= *(int *)arg
;
1343 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1347 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1350 int *targ
= (int *)arg
;
1351 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1353 if (sarg
& TIOCM_CTS
)
1354 *targ
|= CHR_TIOCM_CTS
;
1355 if (sarg
& TIOCM_CAR
)
1356 *targ
|= CHR_TIOCM_CAR
;
1357 if (sarg
& TIOCM_DSR
)
1358 *targ
|= CHR_TIOCM_DSR
;
1359 if (sarg
& TIOCM_RI
)
1360 *targ
|= CHR_TIOCM_RI
;
1361 if (sarg
& TIOCM_DTR
)
1362 *targ
|= CHR_TIOCM_DTR
;
1363 if (sarg
& TIOCM_RTS
)
1364 *targ
|= CHR_TIOCM_RTS
;
1367 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1369 int sarg
= *(int *)arg
;
1371 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1372 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1373 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1374 if (sarg
& CHR_TIOCM_CTS
)
1376 if (sarg
& CHR_TIOCM_CAR
)
1378 if (sarg
& CHR_TIOCM_DSR
)
1380 if (sarg
& CHR_TIOCM_RI
)
1382 if (sarg
& CHR_TIOCM_DTR
)
1384 if (sarg
& CHR_TIOCM_RTS
)
1386 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1395 static void qemu_chr_close_tty(CharDriverState
*chr
)
1397 FDCharDriver
*s
= chr
->opaque
;
1401 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1411 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1413 CharDriverState
*chr
;
1415 tty_serial_init(fd
, 115200, 'N', 8, 1);
1416 chr
= qemu_chr_open_fd(fd
, fd
);
1417 chr
->chr_ioctl
= tty_serial_ioctl
;
1418 chr
->chr_close
= qemu_chr_close_tty
;
1421 #endif /* __linux__ || __sun__ */
1423 #if defined(__linux__)
1425 #define HAVE_CHARDEV_PARPORT 1
1430 } ParallelCharDriver
;
1432 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1434 if (s
->mode
!= mode
) {
1436 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1443 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1445 ParallelCharDriver
*drv
= chr
->opaque
;
1450 case CHR_IOCTL_PP_READ_DATA
:
1451 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1453 *(uint8_t *)arg
= b
;
1455 case CHR_IOCTL_PP_WRITE_DATA
:
1456 b
= *(uint8_t *)arg
;
1457 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1460 case CHR_IOCTL_PP_READ_CONTROL
:
1461 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1463 /* Linux gives only the lowest bits, and no way to know data
1464 direction! For better compatibility set the fixed upper
1466 *(uint8_t *)arg
= b
| 0xc0;
1468 case CHR_IOCTL_PP_WRITE_CONTROL
:
1469 b
= *(uint8_t *)arg
;
1470 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1473 case CHR_IOCTL_PP_READ_STATUS
:
1474 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1476 *(uint8_t *)arg
= b
;
1478 case CHR_IOCTL_PP_DATA_DIR
:
1479 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1482 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1483 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1484 struct ParallelIOArg
*parg
= arg
;
1485 int n
= read(fd
, parg
->buffer
, parg
->count
);
1486 if (n
!= parg
->count
) {
1491 case CHR_IOCTL_PP_EPP_READ
:
1492 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1493 struct ParallelIOArg
*parg
= arg
;
1494 int n
= read(fd
, parg
->buffer
, parg
->count
);
1495 if (n
!= parg
->count
) {
1500 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1501 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1502 struct ParallelIOArg
*parg
= arg
;
1503 int n
= write(fd
, parg
->buffer
, parg
->count
);
1504 if (n
!= parg
->count
) {
1509 case CHR_IOCTL_PP_EPP_WRITE
:
1510 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1511 struct ParallelIOArg
*parg
= arg
;
1512 int n
= write(fd
, parg
->buffer
, parg
->count
);
1513 if (n
!= parg
->count
) {
1524 static void pp_close(CharDriverState
*chr
)
1526 ParallelCharDriver
*drv
= chr
->opaque
;
1529 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1530 ioctl(fd
, PPRELEASE
);
1533 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1536 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1538 CharDriverState
*chr
;
1539 ParallelCharDriver
*drv
;
1541 if (ioctl(fd
, PPCLAIM
) < 0) {
1546 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1548 drv
->mode
= IEEE1284_MODE_COMPAT
;
1550 chr
= g_malloc0(sizeof(CharDriverState
));
1551 chr
->chr_write
= null_chr_write
;
1552 chr
->chr_ioctl
= pp_ioctl
;
1553 chr
->chr_close
= pp_close
;
1558 #endif /* __linux__ */
1560 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1562 #define HAVE_CHARDEV_PARPORT 1
1564 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1566 int fd
= (int)(intptr_t)chr
->opaque
;
1570 case CHR_IOCTL_PP_READ_DATA
:
1571 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1573 *(uint8_t *)arg
= b
;
1575 case CHR_IOCTL_PP_WRITE_DATA
:
1576 b
= *(uint8_t *)arg
;
1577 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1580 case CHR_IOCTL_PP_READ_CONTROL
:
1581 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1583 *(uint8_t *)arg
= b
;
1585 case CHR_IOCTL_PP_WRITE_CONTROL
:
1586 b
= *(uint8_t *)arg
;
1587 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1590 case CHR_IOCTL_PP_READ_STATUS
:
1591 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1593 *(uint8_t *)arg
= b
;
1601 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1603 CharDriverState
*chr
;
1605 chr
= g_malloc0(sizeof(CharDriverState
));
1606 chr
->opaque
= (void *)(intptr_t)fd
;
1607 chr
->chr_write
= null_chr_write
;
1608 chr
->chr_ioctl
= pp_ioctl
;
1609 chr
->explicit_be_open
= true;
1618 HANDLE hcom
, hrecv
, hsend
;
1619 OVERLAPPED orecv
, osend
;
1626 HANDLE hInputReadyEvent
;
1627 HANDLE hInputDoneEvent
;
1628 HANDLE hInputThread
;
1629 uint8_t win_stdio_buf
;
1630 } WinStdioCharState
;
1632 #define NSENDBUF 2048
1633 #define NRECVBUF 2048
1634 #define MAXCONNECT 1
1635 #define NTIMEOUT 5000
1637 static int win_chr_poll(void *opaque
);
1638 static int win_chr_pipe_poll(void *opaque
);
1640 static void win_chr_close(CharDriverState
*chr
)
1642 WinCharState
*s
= chr
->opaque
;
1645 CloseHandle(s
->hsend
);
1649 CloseHandle(s
->hrecv
);
1653 CloseHandle(s
->hcom
);
1657 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1659 qemu_del_polling_cb(win_chr_poll
, chr
);
1661 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1664 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1666 WinCharState
*s
= chr
->opaque
;
1668 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1673 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1675 fprintf(stderr
, "Failed CreateEvent\n");
1678 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1680 fprintf(stderr
, "Failed CreateEvent\n");
1684 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1685 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1686 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1687 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1692 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1693 fprintf(stderr
, "Failed SetupComm\n");
1697 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1698 size
= sizeof(COMMCONFIG
);
1699 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1700 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1701 CommConfigDialog(filename
, NULL
, &comcfg
);
1703 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1704 fprintf(stderr
, "Failed SetCommState\n");
1708 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1709 fprintf(stderr
, "Failed SetCommMask\n");
1713 cto
.ReadIntervalTimeout
= MAXDWORD
;
1714 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1715 fprintf(stderr
, "Failed SetCommTimeouts\n");
1719 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1720 fprintf(stderr
, "Failed ClearCommError\n");
1723 qemu_add_polling_cb(win_chr_poll
, chr
);
1731 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1733 WinCharState
*s
= chr
->opaque
;
1734 DWORD len
, ret
, size
, err
;
1737 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1738 s
->osend
.hEvent
= s
->hsend
;
1741 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1743 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1745 err
= GetLastError();
1746 if (err
== ERROR_IO_PENDING
) {
1747 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1765 static int win_chr_read_poll(CharDriverState
*chr
)
1767 WinCharState
*s
= chr
->opaque
;
1769 s
->max_size
= qemu_chr_be_can_write(chr
);
1773 static void win_chr_readfile(CharDriverState
*chr
)
1775 WinCharState
*s
= chr
->opaque
;
1777 uint8_t buf
[READ_BUF_LEN
];
1780 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1781 s
->orecv
.hEvent
= s
->hrecv
;
1782 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1784 err
= GetLastError();
1785 if (err
== ERROR_IO_PENDING
) {
1786 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1791 qemu_chr_be_write(chr
, buf
, size
);
1795 static void win_chr_read(CharDriverState
*chr
)
1797 WinCharState
*s
= chr
->opaque
;
1799 if (s
->len
> s
->max_size
)
1800 s
->len
= s
->max_size
;
1804 win_chr_readfile(chr
);
1807 static int win_chr_poll(void *opaque
)
1809 CharDriverState
*chr
= opaque
;
1810 WinCharState
*s
= chr
->opaque
;
1814 ClearCommError(s
->hcom
, &comerr
, &status
);
1815 if (status
.cbInQue
> 0) {
1816 s
->len
= status
.cbInQue
;
1817 win_chr_read_poll(chr
);
1824 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1826 CharDriverState
*chr
;
1829 chr
= g_malloc0(sizeof(CharDriverState
));
1830 s
= g_malloc0(sizeof(WinCharState
));
1832 chr
->chr_write
= win_chr_write
;
1833 chr
->chr_close
= win_chr_close
;
1835 if (win_chr_init(chr
, filename
) < 0) {
1843 static int win_chr_pipe_poll(void *opaque
)
1845 CharDriverState
*chr
= opaque
;
1846 WinCharState
*s
= chr
->opaque
;
1849 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1852 win_chr_read_poll(chr
);
1859 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1861 WinCharState
*s
= chr
->opaque
;
1869 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1871 fprintf(stderr
, "Failed CreateEvent\n");
1874 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1876 fprintf(stderr
, "Failed CreateEvent\n");
1880 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1881 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1882 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1884 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1885 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1886 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1891 ZeroMemory(&ov
, sizeof(ov
));
1892 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1893 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1895 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1899 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1901 fprintf(stderr
, "Failed GetOverlappedResult\n");
1903 CloseHandle(ov
.hEvent
);
1910 CloseHandle(ov
.hEvent
);
1913 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1922 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1924 const char *filename
= opts
->device
;
1925 CharDriverState
*chr
;
1928 chr
= g_malloc0(sizeof(CharDriverState
));
1929 s
= g_malloc0(sizeof(WinCharState
));
1931 chr
->chr_write
= win_chr_write
;
1932 chr
->chr_close
= win_chr_close
;
1934 if (win_chr_pipe_init(chr
, filename
) < 0) {
1942 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1944 CharDriverState
*chr
;
1947 chr
= g_malloc0(sizeof(CharDriverState
));
1948 s
= g_malloc0(sizeof(WinCharState
));
1951 chr
->chr_write
= win_chr_write
;
1955 static CharDriverState
*qemu_chr_open_win_con(void)
1957 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1960 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1962 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1969 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1979 static void win_stdio_wait_func(void *opaque
)
1981 CharDriverState
*chr
= opaque
;
1982 WinStdioCharState
*stdio
= chr
->opaque
;
1983 INPUT_RECORD buf
[4];
1988 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1992 /* Avoid error storm */
1993 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1997 for (i
= 0; i
< dwSize
; i
++) {
1998 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2000 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2002 if (kev
->uChar
.AsciiChar
!= 0) {
2003 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2004 if (qemu_chr_be_can_write(chr
)) {
2005 uint8_t c
= kev
->uChar
.AsciiChar
;
2006 qemu_chr_be_write(chr
, &c
, 1);
2014 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2016 CharDriverState
*chr
= param
;
2017 WinStdioCharState
*stdio
= chr
->opaque
;
2023 /* Wait for one byte */
2024 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2026 /* Exit in case of error, continue if nothing read */
2034 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2035 if (stdio
->win_stdio_buf
== '\r') {
2039 /* Signal the main thread and wait until the byte was eaten */
2040 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2043 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2049 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2053 static void win_stdio_thread_wait_func(void *opaque
)
2055 CharDriverState
*chr
= opaque
;
2056 WinStdioCharState
*stdio
= chr
->opaque
;
2058 if (qemu_chr_be_can_write(chr
)) {
2059 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2062 SetEvent(stdio
->hInputDoneEvent
);
2065 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2067 WinStdioCharState
*stdio
= chr
->opaque
;
2070 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2073 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2075 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2079 static void win_stdio_close(CharDriverState
*chr
)
2081 WinStdioCharState
*stdio
= chr
->opaque
;
2083 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2084 CloseHandle(stdio
->hInputReadyEvent
);
2086 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2087 CloseHandle(stdio
->hInputDoneEvent
);
2089 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2090 TerminateThread(stdio
->hInputThread
, 0);
2093 g_free(chr
->opaque
);
2097 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2099 CharDriverState
*chr
;
2100 WinStdioCharState
*stdio
;
2104 chr
= g_malloc0(sizeof(CharDriverState
));
2105 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2107 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2108 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2109 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2113 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2115 chr
->opaque
= stdio
;
2116 chr
->chr_write
= win_stdio_write
;
2117 chr
->chr_close
= win_stdio_close
;
2120 if (qemu_add_wait_object(stdio
->hStdIn
,
2121 win_stdio_wait_func
, chr
)) {
2122 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2127 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2128 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2129 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2132 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2133 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2134 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2135 fprintf(stderr
, "cannot create stdio thread or event\n");
2138 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2139 win_stdio_thread_wait_func
, chr
)) {
2140 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2144 dwMode
|= ENABLE_LINE_INPUT
;
2147 /* set the terminal in raw mode */
2148 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2149 dwMode
|= ENABLE_PROCESSED_INPUT
;
2152 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2154 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2155 qemu_chr_fe_set_echo(chr
, false);
2159 #endif /* !_WIN32 */
2162 /***********************************************************/
2163 /* UDP Net console */
2169 uint8_t buf
[READ_BUF_LEN
];
2175 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2177 NetCharDriver
*s
= chr
->opaque
;
2178 gsize bytes_written
;
2181 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2182 if (status
== G_IO_STATUS_EOF
) {
2184 } else if (status
!= G_IO_STATUS_NORMAL
) {
2188 return bytes_written
;
2191 static int udp_chr_read_poll(void *opaque
)
2193 CharDriverState
*chr
= opaque
;
2194 NetCharDriver
*s
= chr
->opaque
;
2196 s
->max_size
= qemu_chr_be_can_write(chr
);
2198 /* If there were any stray characters in the queue process them
2201 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2202 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2204 s
->max_size
= qemu_chr_be_can_write(chr
);
2209 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2211 CharDriverState
*chr
= opaque
;
2212 NetCharDriver
*s
= chr
->opaque
;
2213 gsize bytes_read
= 0;
2216 if (s
->max_size
== 0) {
2219 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2221 s
->bufcnt
= bytes_read
;
2222 s
->bufptr
= s
->bufcnt
;
2223 if (status
!= G_IO_STATUS_NORMAL
) {
2225 io_remove_watch_poll(s
->tag
);
2232 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2233 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2235 s
->max_size
= qemu_chr_be_can_write(chr
);
2241 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2243 NetCharDriver
*s
= chr
->opaque
;
2246 io_remove_watch_poll(s
->tag
);
2251 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2255 static void udp_chr_close(CharDriverState
*chr
)
2257 NetCharDriver
*s
= chr
->opaque
;
2259 io_remove_watch_poll(s
->tag
);
2263 g_io_channel_unref(s
->chan
);
2267 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2270 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2272 CharDriverState
*chr
= NULL
;
2273 NetCharDriver
*s
= NULL
;
2275 chr
= g_malloc0(sizeof(CharDriverState
));
2276 s
= g_malloc0(sizeof(NetCharDriver
));
2279 s
->chan
= io_channel_from_socket(s
->fd
);
2283 chr
->chr_write
= udp_chr_write
;
2284 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2285 chr
->chr_close
= udp_chr_close
;
2286 /* be isn't opened until we get a connection */
2287 chr
->explicit_be_open
= true;
2291 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2293 Error
*local_err
= NULL
;
2296 fd
= inet_dgram_opts(opts
, &local_err
);
2298 qerror_report_err(local_err
);
2299 error_free(local_err
);
2302 return qemu_chr_open_udp_fd(fd
);
2305 /***********************************************************/
2306 /* TCP Net console */
2310 GIOChannel
*chan
, *listen_chan
;
2311 guint tag
, listen_tag
;
2321 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2323 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2325 TCPCharDriver
*s
= chr
->opaque
;
2327 return io_channel_send(s
->chan
, buf
, len
);
2329 /* XXX: indicate an error ? */
2334 static int tcp_chr_read_poll(void *opaque
)
2336 CharDriverState
*chr
= opaque
;
2337 TCPCharDriver
*s
= chr
->opaque
;
2340 s
->max_size
= qemu_chr_be_can_write(chr
);
2345 #define IAC_BREAK 243
2346 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2348 uint8_t *buf
, int *size
)
2350 /* Handle any telnet client's basic IAC options to satisfy char by
2351 * char mode with no echo. All IAC options will be removed from
2352 * the buf and the do_telnetopt variable will be used to track the
2353 * state of the width of the IAC information.
2355 * IAC commands come in sets of 3 bytes with the exception of the
2356 * "IAC BREAK" command and the double IAC.
2362 for (i
= 0; i
< *size
; i
++) {
2363 if (s
->do_telnetopt
> 1) {
2364 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2365 /* Double IAC means send an IAC */
2369 s
->do_telnetopt
= 1;
2371 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2372 /* Handle IAC break commands by sending a serial break */
2373 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2378 if (s
->do_telnetopt
>= 4) {
2379 s
->do_telnetopt
= 1;
2382 if ((unsigned char)buf
[i
] == IAC
) {
2383 s
->do_telnetopt
= 2;
2394 static int tcp_get_msgfd(CharDriverState
*chr
)
2396 TCPCharDriver
*s
= chr
->opaque
;
2403 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2405 TCPCharDriver
*s
= chr
->opaque
;
2406 struct cmsghdr
*cmsg
;
2408 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2411 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2412 cmsg
->cmsg_level
!= SOL_SOCKET
||
2413 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2416 fd
= *((int *)CMSG_DATA(cmsg
));
2420 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2423 #ifndef MSG_CMSG_CLOEXEC
2424 qemu_set_cloexec(fd
);
2432 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2434 TCPCharDriver
*s
= chr
->opaque
;
2435 struct msghdr msg
= { NULL
, };
2436 struct iovec iov
[1];
2438 struct cmsghdr cmsg
;
2439 char control
[CMSG_SPACE(sizeof(int))];
2444 iov
[0].iov_base
= buf
;
2445 iov
[0].iov_len
= len
;
2449 msg
.msg_control
= &msg_control
;
2450 msg
.msg_controllen
= sizeof(msg_control
);
2452 #ifdef MSG_CMSG_CLOEXEC
2453 flags
|= MSG_CMSG_CLOEXEC
;
2455 ret
= recvmsg(s
->fd
, &msg
, flags
);
2456 if (ret
> 0 && s
->is_unix
) {
2457 unix_process_msgfd(chr
, &msg
);
2463 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2465 TCPCharDriver
*s
= chr
->opaque
;
2466 return qemu_recv(s
->fd
, buf
, len
, 0);
2470 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2472 TCPCharDriver
*s
= chr
->opaque
;
2473 return g_io_create_watch(s
->chan
, cond
);
2476 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2478 CharDriverState
*chr
= opaque
;
2479 TCPCharDriver
*s
= chr
->opaque
;
2480 uint8_t buf
[READ_BUF_LEN
];
2483 if (!s
->connected
|| s
->max_size
<= 0) {
2487 if (len
> s
->max_size
)
2489 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2491 /* connection closed */
2493 if (s
->listen_chan
) {
2494 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2497 io_remove_watch_poll(s
->tag
);
2500 g_io_channel_unref(s
->chan
);
2504 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2505 } else if (size
> 0) {
2506 if (s
->do_telnetopt
)
2507 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2509 qemu_chr_be_write(chr
, buf
, size
);
2516 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2518 return qemu_chr_open_fd(eventfd
, eventfd
);
2522 static void tcp_chr_connect(void *opaque
)
2524 CharDriverState
*chr
= opaque
;
2525 TCPCharDriver
*s
= chr
->opaque
;
2529 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2531 qemu_chr_be_generic_open(chr
);
2534 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2535 static void tcp_chr_telnet_init(int fd
)
2538 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2539 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2540 send(fd
, (char *)buf
, 3, 0);
2541 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2542 send(fd
, (char *)buf
, 3, 0);
2543 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2544 send(fd
, (char *)buf
, 3, 0);
2545 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2546 send(fd
, (char *)buf
, 3, 0);
2549 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2551 TCPCharDriver
*s
= chr
->opaque
;
2555 qemu_set_nonblock(fd
);
2557 socket_set_nodelay(fd
);
2559 s
->chan
= io_channel_from_socket(fd
);
2560 if (s
->listen_tag
) {
2561 g_source_remove(s
->listen_tag
);
2564 tcp_chr_connect(chr
);
2569 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2571 CharDriverState
*chr
= opaque
;
2572 TCPCharDriver
*s
= chr
->opaque
;
2573 struct sockaddr_in saddr
;
2575 struct sockaddr_un uaddr
;
2577 struct sockaddr
*addr
;
2584 len
= sizeof(uaddr
);
2585 addr
= (struct sockaddr
*)&uaddr
;
2589 len
= sizeof(saddr
);
2590 addr
= (struct sockaddr
*)&saddr
;
2592 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2593 if (fd
< 0 && errno
!= EINTR
) {
2596 } else if (fd
>= 0) {
2597 if (s
->do_telnetopt
)
2598 tcp_chr_telnet_init(fd
);
2602 if (tcp_chr_add_client(chr
, fd
) < 0)
2608 static void tcp_chr_close(CharDriverState
*chr
)
2610 TCPCharDriver
*s
= chr
->opaque
;
2613 io_remove_watch_poll(s
->tag
);
2617 g_io_channel_unref(s
->chan
);
2621 if (s
->listen_fd
>= 0) {
2622 if (s
->listen_tag
) {
2623 g_source_remove(s
->listen_tag
);
2626 if (s
->listen_chan
) {
2627 g_io_channel_unref(s
->listen_chan
);
2629 closesocket(s
->listen_fd
);
2632 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2635 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2636 bool is_listen
, bool is_telnet
,
2637 bool is_waitconnect
,
2640 CharDriverState
*chr
= NULL
;
2641 TCPCharDriver
*s
= NULL
;
2642 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2643 const char *left
= "", *right
= "";
2644 struct sockaddr_storage ss
;
2645 socklen_t ss_len
= sizeof(ss
);
2647 memset(&ss
, 0, ss_len
);
2648 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2649 error_setg_errno(errp
, errno
, "getsockname");
2653 chr
= g_malloc0(sizeof(CharDriverState
));
2654 s
= g_malloc0(sizeof(TCPCharDriver
));
2661 chr
->filename
= g_malloc(256);
2662 switch (ss
.ss_family
) {
2666 snprintf(chr
->filename
, 256, "unix:%s%s",
2667 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2668 is_listen
? ",server" : "");
2676 s
->do_nodelay
= do_nodelay
;
2677 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2678 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2679 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2680 is_telnet
? "telnet" : "tcp",
2681 left
, host
, right
, serv
,
2682 is_listen
? ",server" : "");
2687 chr
->chr_write
= tcp_chr_write
;
2688 chr
->chr_close
= tcp_chr_close
;
2689 chr
->get_msgfd
= tcp_get_msgfd
;
2690 chr
->chr_add_client
= tcp_chr_add_client
;
2691 chr
->chr_add_watch
= tcp_chr_add_watch
;
2692 /* be isn't opened until we get a connection */
2693 chr
->explicit_be_open
= true;
2697 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2698 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2700 s
->do_telnetopt
= 1;
2705 socket_set_nodelay(fd
);
2706 s
->chan
= io_channel_from_socket(s
->fd
);
2707 tcp_chr_connect(chr
);
2710 if (is_listen
&& is_waitconnect
) {
2711 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2713 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2714 qemu_set_nonblock(s
->listen_fd
);
2719 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2721 CharDriverState
*chr
= NULL
;
2722 Error
*local_err
= NULL
;
2725 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2726 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2727 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2728 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2729 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2733 fd
= unix_listen_opts(opts
, &local_err
);
2735 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2739 fd
= inet_listen_opts(opts
, 0, &local_err
);
2741 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2748 if (!is_waitconnect
)
2749 qemu_set_nonblock(fd
);
2751 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2752 is_waitconnect
, &local_err
);
2753 if (error_is_set(&local_err
)) {
2761 qerror_report_err(local_err
);
2762 error_free(local_err
);
2768 g_free(chr
->opaque
);
2774 /*********************************************************/
2775 /* Ring buffer chardev */
2782 } RingBufCharDriver
;
2784 static size_t ringbuf_count(const CharDriverState
*chr
)
2786 const RingBufCharDriver
*d
= chr
->opaque
;
2788 return d
->prod
- d
->cons
;
2791 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2793 RingBufCharDriver
*d
= chr
->opaque
;
2796 if (!buf
|| (len
< 0)) {
2800 for (i
= 0; i
< len
; i
++ ) {
2801 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2802 if (d
->prod
- d
->cons
> d
->size
) {
2803 d
->cons
= d
->prod
- d
->size
;
2810 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2812 RingBufCharDriver
*d
= chr
->opaque
;
2815 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2816 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2822 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2824 RingBufCharDriver
*d
= chr
->opaque
;
2831 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2834 CharDriverState
*chr
;
2835 RingBufCharDriver
*d
;
2837 chr
= g_malloc0(sizeof(CharDriverState
));
2838 d
= g_malloc(sizeof(*d
));
2840 d
->size
= opts
->has_size
? opts
->size
: 65536;
2842 /* The size must be power of 2 */
2843 if (d
->size
& (d
->size
- 1)) {
2844 error_setg(errp
, "size of ringbuf chardev must be power of two");
2850 d
->cbuf
= g_malloc0(d
->size
);
2853 chr
->chr_write
= ringbuf_chr_write
;
2854 chr
->chr_close
= ringbuf_chr_close
;
2864 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2866 return chr
->chr_write
== ringbuf_chr_write
;
2869 void qmp_ringbuf_write(const char *device
, const char *data
,
2870 bool has_format
, enum DataFormat format
,
2873 CharDriverState
*chr
;
2874 const uint8_t *write_data
;
2878 chr
= qemu_chr_find(device
);
2880 error_setg(errp
, "Device '%s' not found", device
);
2884 if (!chr_is_ringbuf(chr
)) {
2885 error_setg(errp
,"%s is not a ringbuf device", device
);
2889 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2890 write_data
= g_base64_decode(data
, &write_count
);
2892 write_data
= (uint8_t *)data
;
2893 write_count
= strlen(data
);
2896 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2898 if (write_data
!= (uint8_t *)data
) {
2899 g_free((void *)write_data
);
2903 error_setg(errp
, "Failed to write to device %s", device
);
2908 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2909 bool has_format
, enum DataFormat format
,
2912 CharDriverState
*chr
;
2917 chr
= qemu_chr_find(device
);
2919 error_setg(errp
, "Device '%s' not found", device
);
2923 if (!chr_is_ringbuf(chr
)) {
2924 error_setg(errp
,"%s is not a ringbuf device", device
);
2929 error_setg(errp
, "size must be greater than zero");
2933 count
= ringbuf_count(chr
);
2934 size
= size
> count
? count
: size
;
2935 read_data
= g_malloc(size
+ 1);
2937 ringbuf_chr_read(chr
, read_data
, size
);
2939 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2940 data
= g_base64_encode(read_data
, size
);
2944 * FIXME should read only complete, valid UTF-8 characters up
2945 * to @size bytes. Invalid sequences should be replaced by a
2946 * suitable replacement character. Except when (and only
2947 * when) ring buffer lost characters since last read, initial
2948 * continuation characters should be dropped.
2950 read_data
[size
] = 0;
2951 data
= (char *)read_data
;
2957 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2959 char host
[65], port
[33], width
[8], height
[8];
2963 Error
*local_err
= NULL
;
2965 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2966 if (error_is_set(&local_err
)) {
2967 qerror_report_err(local_err
);
2968 error_free(local_err
);
2972 if (strstart(filename
, "mon:", &p
)) {
2974 qemu_opt_set(opts
, "mux", "on");
2975 if (strcmp(filename
, "stdio") == 0) {
2976 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2977 * but pass it to the guest. Handle this only for compat syntax,
2978 * for -chardev syntax we have special option for this.
2979 * This is what -nographic did, redirecting+muxing serial+monitor
2980 * to stdio causing Ctrl+C to be passed to guest. */
2981 qemu_opt_set(opts
, "signal", "off");
2985 if (strcmp(filename
, "null") == 0 ||
2986 strcmp(filename
, "pty") == 0 ||
2987 strcmp(filename
, "msmouse") == 0 ||
2988 strcmp(filename
, "braille") == 0 ||
2989 strcmp(filename
, "stdio") == 0) {
2990 qemu_opt_set(opts
, "backend", filename
);
2993 if (strstart(filename
, "vc", &p
)) {
2994 qemu_opt_set(opts
, "backend", "vc");
2996 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2998 qemu_opt_set(opts
, "width", width
);
2999 qemu_opt_set(opts
, "height", height
);
3000 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3002 qemu_opt_set(opts
, "cols", width
);
3003 qemu_opt_set(opts
, "rows", height
);
3010 if (strcmp(filename
, "con:") == 0) {
3011 qemu_opt_set(opts
, "backend", "console");
3014 if (strstart(filename
, "COM", NULL
)) {
3015 qemu_opt_set(opts
, "backend", "serial");
3016 qemu_opt_set(opts
, "path", filename
);
3019 if (strstart(filename
, "file:", &p
)) {
3020 qemu_opt_set(opts
, "backend", "file");
3021 qemu_opt_set(opts
, "path", p
);
3024 if (strstart(filename
, "pipe:", &p
)) {
3025 qemu_opt_set(opts
, "backend", "pipe");
3026 qemu_opt_set(opts
, "path", p
);
3029 if (strstart(filename
, "tcp:", &p
) ||
3030 strstart(filename
, "telnet:", &p
)) {
3031 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3033 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3036 qemu_opt_set(opts
, "backend", "socket");
3037 qemu_opt_set(opts
, "host", host
);
3038 qemu_opt_set(opts
, "port", port
);
3039 if (p
[pos
] == ',') {
3040 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3043 if (strstart(filename
, "telnet:", &p
))
3044 qemu_opt_set(opts
, "telnet", "on");
3047 if (strstart(filename
, "udp:", &p
)) {
3048 qemu_opt_set(opts
, "backend", "udp");
3049 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3051 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3055 qemu_opt_set(opts
, "host", host
);
3056 qemu_opt_set(opts
, "port", port
);
3057 if (p
[pos
] == '@') {
3059 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3061 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3065 qemu_opt_set(opts
, "localaddr", host
);
3066 qemu_opt_set(opts
, "localport", port
);
3070 if (strstart(filename
, "unix:", &p
)) {
3071 qemu_opt_set(opts
, "backend", "socket");
3072 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3076 if (strstart(filename
, "/dev/parport", NULL
) ||
3077 strstart(filename
, "/dev/ppi", NULL
)) {
3078 qemu_opt_set(opts
, "backend", "parport");
3079 qemu_opt_set(opts
, "path", filename
);
3082 if (strstart(filename
, "/dev/", NULL
)) {
3083 qemu_opt_set(opts
, "backend", "tty");
3084 qemu_opt_set(opts
, "path", filename
);
3089 qemu_opts_del(opts
);
3093 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3096 const char *path
= qemu_opt_get(opts
, "path");
3099 error_setg(errp
, "chardev: file: no filename given");
3102 backend
->file
= g_new0(ChardevFile
, 1);
3103 backend
->file
->out
= g_strdup(path
);
3106 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3109 backend
->stdio
= g_new0(ChardevStdio
, 1);
3110 backend
->stdio
->has_signal
= true;
3111 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3114 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3117 const char *device
= qemu_opt_get(opts
, "path");
3119 if (device
== NULL
) {
3120 error_setg(errp
, "chardev: serial/tty: no device path given");
3123 backend
->serial
= g_new0(ChardevHostdev
, 1);
3124 backend
->serial
->device
= g_strdup(device
);
3127 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3130 const char *device
= qemu_opt_get(opts
, "path");
3132 if (device
== NULL
) {
3133 error_setg(errp
, "chardev: parallel: no device path given");
3136 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3137 backend
->parallel
->device
= g_strdup(device
);
3140 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3143 const char *device
= qemu_opt_get(opts
, "path");
3145 if (device
== NULL
) {
3146 error_setg(errp
, "chardev: pipe: no device path given");
3149 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3150 backend
->pipe
->device
= g_strdup(device
);
3153 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3158 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3160 val
= qemu_opt_get_size(opts
, "size", 0);
3162 backend
->ringbuf
->has_size
= true;
3163 backend
->ringbuf
->size
= val
;
3167 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3170 const char *chardev
= qemu_opt_get(opts
, "chardev");
3172 if (chardev
== NULL
) {
3173 error_setg(errp
, "chardev: mux: no chardev given");
3176 backend
->mux
= g_new0(ChardevMux
, 1);
3177 backend
->mux
->chardev
= g_strdup(chardev
);
3180 typedef struct CharDriver
{
3183 CharDriverState
*(*open
)(QemuOpts
*opts
);
3184 /* new, qapi-based */
3185 ChardevBackendKind kind
;
3186 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3189 static GSList
*backends
;
3191 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3195 s
= g_malloc0(sizeof(*s
));
3196 s
->name
= g_strdup(name
);
3199 backends
= g_slist_append(backends
, s
);
3202 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3203 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3207 s
= g_malloc0(sizeof(*s
));
3208 s
->name
= g_strdup(name
);
3212 backends
= g_slist_append(backends
, s
);
3215 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3216 void (*init
)(struct CharDriverState
*s
),
3220 CharDriverState
*chr
;
3223 if (qemu_opts_id(opts
) == NULL
) {
3224 error_setg(errp
, "chardev: no id specified");
3228 if (qemu_opt_get(opts
, "backend") == NULL
) {
3229 error_setg(errp
, "chardev: \"%s\" missing backend",
3230 qemu_opts_id(opts
));
3233 for (i
= backends
; i
; i
= i
->next
) {
3236 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3241 error_setg(errp
, "chardev: backend \"%s\" not found",
3242 qemu_opt_get(opts
, "backend"));
3247 /* using new, qapi init */
3248 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3249 ChardevReturn
*ret
= NULL
;
3250 const char *id
= qemu_opts_id(opts
);
3253 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3254 bid
= g_strdup_printf("%s-base", id
);
3258 backend
->kind
= cd
->kind
;
3260 cd
->parse(opts
, backend
, errp
);
3261 if (error_is_set(errp
)) {
3265 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3266 if (error_is_set(errp
)) {
3271 qapi_free_ChardevBackend(backend
);
3272 qapi_free_ChardevReturn(ret
);
3273 backend
= g_new0(ChardevBackend
, 1);
3274 backend
->mux
= g_new0(ChardevMux
, 1);
3275 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3276 backend
->mux
->chardev
= g_strdup(bid
);
3277 ret
= qmp_chardev_add(id
, backend
, errp
);
3278 assert(!error_is_set(errp
));
3281 chr
= qemu_chr_find(id
);
3285 qapi_free_ChardevBackend(backend
);
3286 qapi_free_ChardevReturn(ret
);
3291 chr
= cd
->open(opts
);
3293 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3294 qemu_opt_get(opts
, "backend"));
3299 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3301 /* if we didn't create the chardev via qmp_chardev_add, we
3302 * need to send the OPENED event here
3304 if (!chr
->explicit_be_open
) {
3305 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3307 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3309 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3310 CharDriverState
*base
= chr
;
3311 int len
= strlen(qemu_opts_id(opts
)) + 6;
3312 base
->label
= g_malloc(len
);
3313 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3314 chr
= qemu_chr_open_mux(base
);
3315 chr
->filename
= base
->filename
;
3316 chr
->avail_connections
= MAX_MUX
;
3317 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3319 chr
->avail_connections
= 1;
3321 chr
->label
= g_strdup(qemu_opts_id(opts
));
3326 qemu_opts_del(opts
);
3330 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3333 CharDriverState
*chr
;
3337 if (strstart(filename
, "chardev:", &p
)) {
3338 return qemu_chr_find(p
);
3341 opts
= qemu_chr_parse_compat(label
, filename
);
3345 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3346 if (error_is_set(&err
)) {
3347 error_report("%s", error_get_pretty(err
));
3350 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3351 qemu_chr_fe_claim_no_fail(chr
);
3352 monitor_init(chr
, MONITOR_USE_READLINE
);
3357 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3359 if (chr
->chr_set_echo
) {
3360 chr
->chr_set_echo(chr
, echo
);
3364 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3366 if (chr
->fe_open
== fe_open
) {
3369 chr
->fe_open
= fe_open
;
3370 if (chr
->chr_set_fe_open
) {
3371 chr
->chr_set_fe_open(chr
, fe_open
);
3375 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3376 GIOFunc func
, void *user_data
)
3381 if (s
->chr_add_watch
== NULL
) {
3385 src
= s
->chr_add_watch(s
, cond
);
3386 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3387 tag
= g_source_attach(src
, NULL
);
3388 g_source_unref(src
);
3393 int qemu_chr_fe_claim(CharDriverState
*s
)
3395 if (s
->avail_connections
< 1) {
3398 s
->avail_connections
--;
3402 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3404 if (qemu_chr_fe_claim(s
) != 0) {
3405 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3406 __func__
, s
->label
);
3411 void qemu_chr_fe_release(CharDriverState
*s
)
3413 s
->avail_connections
++;
3416 void qemu_chr_delete(CharDriverState
*chr
)
3418 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3419 if (chr
->chr_close
) {
3420 chr
->chr_close(chr
);
3422 g_free(chr
->filename
);
3425 qemu_opts_del(chr
->opts
);
3430 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3432 ChardevInfoList
*chr_list
= NULL
;
3433 CharDriverState
*chr
;
3435 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3436 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3437 info
->value
= g_malloc0(sizeof(*info
->value
));
3438 info
->value
->label
= g_strdup(chr
->label
);
3439 info
->value
->filename
= g_strdup(chr
->filename
);
3441 info
->next
= chr_list
;
3448 CharDriverState
*qemu_chr_find(const char *name
)
3450 CharDriverState
*chr
;
3452 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3453 if (strcmp(chr
->label
, name
) != 0)
3460 /* Get a character (serial) device interface. */
3461 CharDriverState
*qemu_char_get_next_serial(void)
3463 static int next_serial
;
3464 CharDriverState
*chr
;
3466 /* FIXME: This function needs to go away: use chardev properties! */
3468 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3469 chr
= serial_hds
[next_serial
++];
3470 qemu_chr_fe_claim_no_fail(chr
);
3476 QemuOptsList qemu_chardev_opts
= {
3478 .implied_opt_name
= "backend",
3479 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3483 .type
= QEMU_OPT_STRING
,
3486 .type
= QEMU_OPT_STRING
,
3489 .type
= QEMU_OPT_STRING
,
3492 .type
= QEMU_OPT_STRING
,
3494 .name
= "localaddr",
3495 .type
= QEMU_OPT_STRING
,
3497 .name
= "localport",
3498 .type
= QEMU_OPT_STRING
,
3501 .type
= QEMU_OPT_NUMBER
,
3504 .type
= QEMU_OPT_BOOL
,
3507 .type
= QEMU_OPT_BOOL
,
3510 .type
= QEMU_OPT_BOOL
,
3513 .type
= QEMU_OPT_BOOL
,
3516 .type
= QEMU_OPT_BOOL
,
3519 .type
= QEMU_OPT_BOOL
,
3522 .type
= QEMU_OPT_NUMBER
,
3525 .type
= QEMU_OPT_NUMBER
,
3528 .type
= QEMU_OPT_NUMBER
,
3531 .type
= QEMU_OPT_NUMBER
,
3534 .type
= QEMU_OPT_BOOL
,
3537 .type
= QEMU_OPT_BOOL
,
3540 .type
= QEMU_OPT_STRING
,
3543 .type
= QEMU_OPT_NUMBER
,
3546 .type
= QEMU_OPT_SIZE
,
3549 .type
= QEMU_OPT_STRING
,
3551 { /* end of list */ }
3557 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3562 error_setg(errp
, "input file not supported");
3566 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3567 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3568 if (out
== INVALID_HANDLE_VALUE
) {
3569 error_setg(errp
, "open %s failed", file
->out
);
3572 return qemu_chr_open_win_file(out
);
3575 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3578 return qemu_chr_open_win_path(serial
->device
);
3581 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3584 error_setg(errp
, "character device backend type 'parallel' not supported");
3590 static int qmp_chardev_open_file_source(char *src
, int flags
,
3595 TFR(fd
= qemu_open(src
, flags
, 0666));
3597 error_setg_file_open(errp
, errno
, src
);
3602 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3604 int flags
, in
= -1, out
= -1;
3606 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3607 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3608 if (error_is_set(errp
)) {
3614 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3615 if (error_is_set(errp
)) {
3621 return qemu_chr_open_fd(in
, out
);
3624 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3627 #ifdef HAVE_CHARDEV_TTY
3630 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3631 if (error_is_set(errp
)) {
3634 qemu_set_nonblock(fd
);
3635 return qemu_chr_open_tty_fd(fd
);
3637 error_setg(errp
, "character device backend type 'serial' not supported");
3642 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3645 #ifdef HAVE_CHARDEV_PARPORT
3648 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3649 if (error_is_set(errp
)) {
3652 return qemu_chr_open_pp_fd(fd
);
3654 error_setg(errp
, "character device backend type 'parallel' not supported");
3661 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3664 SocketAddress
*addr
= sock
->addr
;
3665 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3666 bool is_listen
= sock
->has_server
? sock
->server
: true;
3667 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3668 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3672 fd
= socket_listen(addr
, errp
);
3674 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3676 if (error_is_set(errp
)) {
3679 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3680 is_telnet
, is_waitconnect
, errp
);
3683 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3688 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3689 if (error_is_set(errp
)) {
3692 return qemu_chr_open_udp_fd(fd
);
3695 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3698 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3699 CharDriverState
*base
, *chr
= NULL
;
3701 chr
= qemu_chr_find(id
);
3703 error_setg(errp
, "Chardev '%s' already exists", id
);
3708 switch (backend
->kind
) {
3709 case CHARDEV_BACKEND_KIND_FILE
:
3710 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3712 case CHARDEV_BACKEND_KIND_SERIAL
:
3713 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3715 case CHARDEV_BACKEND_KIND_PARALLEL
:
3716 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3718 case CHARDEV_BACKEND_KIND_PIPE
:
3719 chr
= qemu_chr_open_pipe(backend
->pipe
);
3721 case CHARDEV_BACKEND_KIND_SOCKET
:
3722 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3724 case CHARDEV_BACKEND_KIND_UDP
:
3725 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
3727 #ifdef HAVE_CHARDEV_TTY
3728 case CHARDEV_BACKEND_KIND_PTY
:
3729 chr
= qemu_chr_open_pty(id
, ret
);
3732 case CHARDEV_BACKEND_KIND_NULL
:
3733 chr
= qemu_chr_open_null();
3735 case CHARDEV_BACKEND_KIND_MUX
:
3736 base
= qemu_chr_find(backend
->mux
->chardev
);
3738 error_setg(errp
, "mux: base chardev %s not found",
3739 backend
->mux
->chardev
);
3742 chr
= qemu_chr_open_mux(base
);
3744 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3745 chr
= qemu_chr_open_msmouse();
3747 #ifdef CONFIG_BRLAPI
3748 case CHARDEV_BACKEND_KIND_BRAILLE
:
3749 chr
= chr_baum_init();
3752 case CHARDEV_BACKEND_KIND_STDIO
:
3753 chr
= qemu_chr_open_stdio(backend
->stdio
);
3756 case CHARDEV_BACKEND_KIND_CONSOLE
:
3757 chr
= qemu_chr_open_win_con();
3761 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3762 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3764 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3765 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3768 case CHARDEV_BACKEND_KIND_VC
:
3769 chr
= vc_init(backend
->vc
);
3771 case CHARDEV_BACKEND_KIND_RINGBUF
:
3772 case CHARDEV_BACKEND_KIND_MEMORY
:
3773 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
3776 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3780 if (chr
== NULL
&& !error_is_set(errp
)) {
3781 error_setg(errp
, "Failed to create chardev");
3784 chr
->label
= g_strdup(id
);
3785 chr
->avail_connections
=
3786 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3787 if (!chr
->filename
) {
3788 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
3790 if (!chr
->explicit_be_open
) {
3791 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3793 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3801 void qmp_chardev_remove(const char *id
, Error
**errp
)
3803 CharDriverState
*chr
;
3805 chr
= qemu_chr_find(id
);
3807 error_setg(errp
, "Chardev '%s' not found", id
);
3810 if (chr
->chr_can_read
|| chr
->chr_read
||
3811 chr
->chr_event
|| chr
->handler_opaque
) {
3812 error_setg(errp
, "Chardev '%s' is busy", id
);
3815 qemu_chr_delete(chr
);
3818 static void register_types(void)
3820 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3821 register_char_driver("socket", qemu_chr_open_socket
);
3822 register_char_driver("udp", qemu_chr_open_udp
);
3823 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
3824 qemu_chr_parse_ringbuf
);
3825 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3826 qemu_chr_parse_file_out
);
3827 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3828 qemu_chr_parse_stdio
);
3829 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3830 qemu_chr_parse_serial
);
3831 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3832 qemu_chr_parse_serial
);
3833 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3834 qemu_chr_parse_parallel
);
3835 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3836 qemu_chr_parse_parallel
);
3837 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3838 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3839 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3840 qemu_chr_parse_pipe
);
3841 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
3842 qemu_chr_parse_mux
);
3843 /* Bug-compatibility: */
3844 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3845 qemu_chr_parse_ringbuf
);
3846 /* this must be done after machine init, since we register FEs with muxes
3847 * as part of realize functions like serial_isa_realizefn when -nographic
3850 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
3853 type_init(register_types
);