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
;
1030 if (!s
->connected
) {
1032 pty_chr_update_read_handler(chr
);
1037 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1039 PtyCharDriver
*s
= chr
->opaque
;
1042 g_source_remove(s
->timer_tag
);
1047 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1049 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1053 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1055 PtyCharDriver
*s
= chr
->opaque
;
1057 if (!s
->connected
) {
1058 /* guest sends data, check for (re-)connect */
1059 pty_chr_update_read_handler(chr
);
1062 return io_channel_send(s
->fd
, buf
, len
);
1065 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1067 PtyCharDriver
*s
= chr
->opaque
;
1068 return g_io_create_watch(s
->fd
, cond
);
1071 static int pty_chr_read_poll(void *opaque
)
1073 CharDriverState
*chr
= opaque
;
1074 PtyCharDriver
*s
= chr
->opaque
;
1076 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1077 return s
->read_bytes
;
1080 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1082 CharDriverState
*chr
= opaque
;
1083 PtyCharDriver
*s
= chr
->opaque
;
1085 uint8_t buf
[READ_BUF_LEN
];
1089 if (len
> s
->read_bytes
)
1090 len
= s
->read_bytes
;
1094 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1095 if (status
!= G_IO_STATUS_NORMAL
) {
1096 pty_chr_state(chr
, 0);
1099 pty_chr_state(chr
, 1);
1100 qemu_chr_be_write(chr
, buf
, size
);
1105 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1107 PtyCharDriver
*s
= chr
->opaque
;
1110 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1111 pfd
.events
= G_IO_OUT
;
1114 if (pfd
.revents
& G_IO_HUP
) {
1115 pty_chr_state(chr
, 0);
1117 pty_chr_state(chr
, 1);
1121 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1123 PtyCharDriver
*s
= chr
->opaque
;
1127 io_remove_watch_poll(s
->fd_tag
);
1131 /* (re-)connect poll interval for idle guests: once per second.
1132 * We check more frequently in case the guests sends data to
1133 * the virtual device linked to our pty. */
1134 pty_chr_rearm_timer(chr
, 1000);
1137 g_source_remove(s
->timer_tag
);
1140 if (!s
->connected
) {
1142 qemu_chr_be_generic_open(chr
);
1143 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1149 static void pty_chr_close(struct CharDriverState
*chr
)
1151 PtyCharDriver
*s
= chr
->opaque
;
1155 io_remove_watch_poll(s
->fd_tag
);
1158 fd
= g_io_channel_unix_get_fd(s
->fd
);
1159 g_io_channel_unref(s
->fd
);
1162 g_source_remove(s
->timer_tag
);
1166 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1169 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1172 CharDriverState
*chr
;
1174 int master_fd
, slave_fd
;
1175 char pty_name
[PATH_MAX
];
1177 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1178 if (master_fd
< 0) {
1184 chr
= g_malloc0(sizeof(CharDriverState
));
1186 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1187 ret
->pty
= g_strdup(pty_name
);
1188 ret
->has_pty
= true;
1190 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1193 s
= g_malloc0(sizeof(PtyCharDriver
));
1195 chr
->chr_write
= pty_chr_write
;
1196 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1197 chr
->chr_close
= pty_chr_close
;
1198 chr
->chr_add_watch
= pty_chr_add_watch
;
1199 chr
->explicit_be_open
= true;
1201 s
->fd
= io_channel_from_fd(master_fd
);
1207 static void tty_serial_init(int fd
, int speed
,
1208 int parity
, int data_bits
, int stop_bits
)
1214 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1215 speed
, parity
, data_bits
, stop_bits
);
1217 tcgetattr (fd
, &tty
);
1219 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1220 speed
= speed
* 10 / 11;
1237 /* Non-Posix values follow. They may be unsupported on some systems. */
1239 check_speed(115200);
1241 check_speed(230400);
1244 check_speed(460800);
1247 check_speed(500000);
1250 check_speed(576000);
1253 check_speed(921600);
1256 check_speed(1000000);
1259 check_speed(1152000);
1262 check_speed(1500000);
1265 check_speed(2000000);
1268 check_speed(2500000);
1271 check_speed(3000000);
1274 check_speed(3500000);
1277 check_speed(4000000);
1282 cfsetispeed(&tty
, spd
);
1283 cfsetospeed(&tty
, spd
);
1285 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1286 |INLCR
|IGNCR
|ICRNL
|IXON
);
1287 tty
.c_oflag
|= OPOST
;
1288 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1289 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1310 tty
.c_cflag
|= PARENB
;
1313 tty
.c_cflag
|= PARENB
| PARODD
;
1317 tty
.c_cflag
|= CSTOPB
;
1319 tcsetattr (fd
, TCSANOW
, &tty
);
1322 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1324 FDCharDriver
*s
= chr
->opaque
;
1327 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1329 QEMUSerialSetParams
*ssp
= arg
;
1330 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1331 ssp
->speed
, ssp
->parity
,
1332 ssp
->data_bits
, ssp
->stop_bits
);
1335 case CHR_IOCTL_SERIAL_SET_BREAK
:
1337 int enable
= *(int *)arg
;
1339 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1343 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1346 int *targ
= (int *)arg
;
1347 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1349 if (sarg
& TIOCM_CTS
)
1350 *targ
|= CHR_TIOCM_CTS
;
1351 if (sarg
& TIOCM_CAR
)
1352 *targ
|= CHR_TIOCM_CAR
;
1353 if (sarg
& TIOCM_DSR
)
1354 *targ
|= CHR_TIOCM_DSR
;
1355 if (sarg
& TIOCM_RI
)
1356 *targ
|= CHR_TIOCM_RI
;
1357 if (sarg
& TIOCM_DTR
)
1358 *targ
|= CHR_TIOCM_DTR
;
1359 if (sarg
& TIOCM_RTS
)
1360 *targ
|= CHR_TIOCM_RTS
;
1363 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1365 int sarg
= *(int *)arg
;
1367 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1368 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1369 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1370 if (sarg
& CHR_TIOCM_CTS
)
1372 if (sarg
& CHR_TIOCM_CAR
)
1374 if (sarg
& CHR_TIOCM_DSR
)
1376 if (sarg
& CHR_TIOCM_RI
)
1378 if (sarg
& CHR_TIOCM_DTR
)
1380 if (sarg
& CHR_TIOCM_RTS
)
1382 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1391 static void qemu_chr_close_tty(CharDriverState
*chr
)
1393 FDCharDriver
*s
= chr
->opaque
;
1397 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1407 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1409 CharDriverState
*chr
;
1411 tty_serial_init(fd
, 115200, 'N', 8, 1);
1412 chr
= qemu_chr_open_fd(fd
, fd
);
1413 chr
->chr_ioctl
= tty_serial_ioctl
;
1414 chr
->chr_close
= qemu_chr_close_tty
;
1417 #endif /* __linux__ || __sun__ */
1419 #if defined(__linux__)
1421 #define HAVE_CHARDEV_PARPORT 1
1426 } ParallelCharDriver
;
1428 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1430 if (s
->mode
!= mode
) {
1432 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1439 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1441 ParallelCharDriver
*drv
= chr
->opaque
;
1446 case CHR_IOCTL_PP_READ_DATA
:
1447 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1449 *(uint8_t *)arg
= b
;
1451 case CHR_IOCTL_PP_WRITE_DATA
:
1452 b
= *(uint8_t *)arg
;
1453 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1456 case CHR_IOCTL_PP_READ_CONTROL
:
1457 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1459 /* Linux gives only the lowest bits, and no way to know data
1460 direction! For better compatibility set the fixed upper
1462 *(uint8_t *)arg
= b
| 0xc0;
1464 case CHR_IOCTL_PP_WRITE_CONTROL
:
1465 b
= *(uint8_t *)arg
;
1466 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1469 case CHR_IOCTL_PP_READ_STATUS
:
1470 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1472 *(uint8_t *)arg
= b
;
1474 case CHR_IOCTL_PP_DATA_DIR
:
1475 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1478 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1479 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1480 struct ParallelIOArg
*parg
= arg
;
1481 int n
= read(fd
, parg
->buffer
, parg
->count
);
1482 if (n
!= parg
->count
) {
1487 case CHR_IOCTL_PP_EPP_READ
:
1488 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1489 struct ParallelIOArg
*parg
= arg
;
1490 int n
= read(fd
, parg
->buffer
, parg
->count
);
1491 if (n
!= parg
->count
) {
1496 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1497 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1498 struct ParallelIOArg
*parg
= arg
;
1499 int n
= write(fd
, parg
->buffer
, parg
->count
);
1500 if (n
!= parg
->count
) {
1505 case CHR_IOCTL_PP_EPP_WRITE
:
1506 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1507 struct ParallelIOArg
*parg
= arg
;
1508 int n
= write(fd
, parg
->buffer
, parg
->count
);
1509 if (n
!= parg
->count
) {
1520 static void pp_close(CharDriverState
*chr
)
1522 ParallelCharDriver
*drv
= chr
->opaque
;
1525 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1526 ioctl(fd
, PPRELEASE
);
1529 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1532 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1534 CharDriverState
*chr
;
1535 ParallelCharDriver
*drv
;
1537 if (ioctl(fd
, PPCLAIM
) < 0) {
1542 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1544 drv
->mode
= IEEE1284_MODE_COMPAT
;
1546 chr
= g_malloc0(sizeof(CharDriverState
));
1547 chr
->chr_write
= null_chr_write
;
1548 chr
->chr_ioctl
= pp_ioctl
;
1549 chr
->chr_close
= pp_close
;
1554 #endif /* __linux__ */
1556 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1558 #define HAVE_CHARDEV_PARPORT 1
1560 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1562 int fd
= (int)(intptr_t)chr
->opaque
;
1566 case CHR_IOCTL_PP_READ_DATA
:
1567 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1569 *(uint8_t *)arg
= b
;
1571 case CHR_IOCTL_PP_WRITE_DATA
:
1572 b
= *(uint8_t *)arg
;
1573 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1576 case CHR_IOCTL_PP_READ_CONTROL
:
1577 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1579 *(uint8_t *)arg
= b
;
1581 case CHR_IOCTL_PP_WRITE_CONTROL
:
1582 b
= *(uint8_t *)arg
;
1583 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1586 case CHR_IOCTL_PP_READ_STATUS
:
1587 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1589 *(uint8_t *)arg
= b
;
1597 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1599 CharDriverState
*chr
;
1601 chr
= g_malloc0(sizeof(CharDriverState
));
1602 chr
->opaque
= (void *)(intptr_t)fd
;
1603 chr
->chr_write
= null_chr_write
;
1604 chr
->chr_ioctl
= pp_ioctl
;
1605 chr
->explicit_be_open
= true;
1614 HANDLE hcom
, hrecv
, hsend
;
1615 OVERLAPPED orecv
, osend
;
1622 HANDLE hInputReadyEvent
;
1623 HANDLE hInputDoneEvent
;
1624 HANDLE hInputThread
;
1625 uint8_t win_stdio_buf
;
1626 } WinStdioCharState
;
1628 #define NSENDBUF 2048
1629 #define NRECVBUF 2048
1630 #define MAXCONNECT 1
1631 #define NTIMEOUT 5000
1633 static int win_chr_poll(void *opaque
);
1634 static int win_chr_pipe_poll(void *opaque
);
1636 static void win_chr_close(CharDriverState
*chr
)
1638 WinCharState
*s
= chr
->opaque
;
1641 CloseHandle(s
->hsend
);
1645 CloseHandle(s
->hrecv
);
1649 CloseHandle(s
->hcom
);
1653 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1655 qemu_del_polling_cb(win_chr_poll
, chr
);
1657 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1660 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1662 WinCharState
*s
= chr
->opaque
;
1664 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1669 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1671 fprintf(stderr
, "Failed CreateEvent\n");
1674 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1676 fprintf(stderr
, "Failed CreateEvent\n");
1680 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1681 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1682 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1683 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1688 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1689 fprintf(stderr
, "Failed SetupComm\n");
1693 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1694 size
= sizeof(COMMCONFIG
);
1695 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1696 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1697 CommConfigDialog(filename
, NULL
, &comcfg
);
1699 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1700 fprintf(stderr
, "Failed SetCommState\n");
1704 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1705 fprintf(stderr
, "Failed SetCommMask\n");
1709 cto
.ReadIntervalTimeout
= MAXDWORD
;
1710 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1711 fprintf(stderr
, "Failed SetCommTimeouts\n");
1715 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1716 fprintf(stderr
, "Failed ClearCommError\n");
1719 qemu_add_polling_cb(win_chr_poll
, chr
);
1727 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1729 WinCharState
*s
= chr
->opaque
;
1730 DWORD len
, ret
, size
, err
;
1733 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1734 s
->osend
.hEvent
= s
->hsend
;
1737 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1739 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1741 err
= GetLastError();
1742 if (err
== ERROR_IO_PENDING
) {
1743 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1761 static int win_chr_read_poll(CharDriverState
*chr
)
1763 WinCharState
*s
= chr
->opaque
;
1765 s
->max_size
= qemu_chr_be_can_write(chr
);
1769 static void win_chr_readfile(CharDriverState
*chr
)
1771 WinCharState
*s
= chr
->opaque
;
1773 uint8_t buf
[READ_BUF_LEN
];
1776 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1777 s
->orecv
.hEvent
= s
->hrecv
;
1778 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1780 err
= GetLastError();
1781 if (err
== ERROR_IO_PENDING
) {
1782 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1787 qemu_chr_be_write(chr
, buf
, size
);
1791 static void win_chr_read(CharDriverState
*chr
)
1793 WinCharState
*s
= chr
->opaque
;
1795 if (s
->len
> s
->max_size
)
1796 s
->len
= s
->max_size
;
1800 win_chr_readfile(chr
);
1803 static int win_chr_poll(void *opaque
)
1805 CharDriverState
*chr
= opaque
;
1806 WinCharState
*s
= chr
->opaque
;
1810 ClearCommError(s
->hcom
, &comerr
, &status
);
1811 if (status
.cbInQue
> 0) {
1812 s
->len
= status
.cbInQue
;
1813 win_chr_read_poll(chr
);
1820 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1822 CharDriverState
*chr
;
1825 chr
= g_malloc0(sizeof(CharDriverState
));
1826 s
= g_malloc0(sizeof(WinCharState
));
1828 chr
->chr_write
= win_chr_write
;
1829 chr
->chr_close
= win_chr_close
;
1831 if (win_chr_init(chr
, filename
) < 0) {
1839 static int win_chr_pipe_poll(void *opaque
)
1841 CharDriverState
*chr
= opaque
;
1842 WinCharState
*s
= chr
->opaque
;
1845 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1848 win_chr_read_poll(chr
);
1855 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1857 WinCharState
*s
= chr
->opaque
;
1865 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1867 fprintf(stderr
, "Failed CreateEvent\n");
1870 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1872 fprintf(stderr
, "Failed CreateEvent\n");
1876 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1877 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1878 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1880 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1881 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1882 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1887 ZeroMemory(&ov
, sizeof(ov
));
1888 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1889 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1891 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1895 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1897 fprintf(stderr
, "Failed GetOverlappedResult\n");
1899 CloseHandle(ov
.hEvent
);
1906 CloseHandle(ov
.hEvent
);
1909 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1918 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1920 const char *filename
= opts
->device
;
1921 CharDriverState
*chr
;
1924 chr
= g_malloc0(sizeof(CharDriverState
));
1925 s
= g_malloc0(sizeof(WinCharState
));
1927 chr
->chr_write
= win_chr_write
;
1928 chr
->chr_close
= win_chr_close
;
1930 if (win_chr_pipe_init(chr
, filename
) < 0) {
1938 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1940 CharDriverState
*chr
;
1943 chr
= g_malloc0(sizeof(CharDriverState
));
1944 s
= g_malloc0(sizeof(WinCharState
));
1947 chr
->chr_write
= win_chr_write
;
1951 static CharDriverState
*qemu_chr_open_win_con(void)
1953 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1956 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1958 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1965 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1975 static void win_stdio_wait_func(void *opaque
)
1977 CharDriverState
*chr
= opaque
;
1978 WinStdioCharState
*stdio
= chr
->opaque
;
1979 INPUT_RECORD buf
[4];
1984 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1988 /* Avoid error storm */
1989 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1993 for (i
= 0; i
< dwSize
; i
++) {
1994 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1996 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1998 if (kev
->uChar
.AsciiChar
!= 0) {
1999 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2000 if (qemu_chr_be_can_write(chr
)) {
2001 uint8_t c
= kev
->uChar
.AsciiChar
;
2002 qemu_chr_be_write(chr
, &c
, 1);
2010 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2012 CharDriverState
*chr
= param
;
2013 WinStdioCharState
*stdio
= chr
->opaque
;
2019 /* Wait for one byte */
2020 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2022 /* Exit in case of error, continue if nothing read */
2030 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2031 if (stdio
->win_stdio_buf
== '\r') {
2035 /* Signal the main thread and wait until the byte was eaten */
2036 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2039 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2045 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2049 static void win_stdio_thread_wait_func(void *opaque
)
2051 CharDriverState
*chr
= opaque
;
2052 WinStdioCharState
*stdio
= chr
->opaque
;
2054 if (qemu_chr_be_can_write(chr
)) {
2055 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2058 SetEvent(stdio
->hInputDoneEvent
);
2061 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2063 WinStdioCharState
*stdio
= chr
->opaque
;
2066 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2069 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2071 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2075 static void win_stdio_close(CharDriverState
*chr
)
2077 WinStdioCharState
*stdio
= chr
->opaque
;
2079 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2080 CloseHandle(stdio
->hInputReadyEvent
);
2082 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2083 CloseHandle(stdio
->hInputDoneEvent
);
2085 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2086 TerminateThread(stdio
->hInputThread
, 0);
2089 g_free(chr
->opaque
);
2093 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2095 CharDriverState
*chr
;
2096 WinStdioCharState
*stdio
;
2100 chr
= g_malloc0(sizeof(CharDriverState
));
2101 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2103 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2104 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2105 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2109 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2111 chr
->opaque
= stdio
;
2112 chr
->chr_write
= win_stdio_write
;
2113 chr
->chr_close
= win_stdio_close
;
2116 if (qemu_add_wait_object(stdio
->hStdIn
,
2117 win_stdio_wait_func
, chr
)) {
2118 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2123 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2124 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2125 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2128 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2129 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2130 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2131 fprintf(stderr
, "cannot create stdio thread or event\n");
2134 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2135 win_stdio_thread_wait_func
, chr
)) {
2136 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2140 dwMode
|= ENABLE_LINE_INPUT
;
2143 /* set the terminal in raw mode */
2144 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2145 dwMode
|= ENABLE_PROCESSED_INPUT
;
2148 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2150 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2151 qemu_chr_fe_set_echo(chr
, false);
2155 #endif /* !_WIN32 */
2158 /***********************************************************/
2159 /* UDP Net console */
2165 uint8_t buf
[READ_BUF_LEN
];
2171 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2173 NetCharDriver
*s
= chr
->opaque
;
2174 gsize bytes_written
;
2177 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2178 if (status
== G_IO_STATUS_EOF
) {
2180 } else if (status
!= G_IO_STATUS_NORMAL
) {
2184 return bytes_written
;
2187 static int udp_chr_read_poll(void *opaque
)
2189 CharDriverState
*chr
= opaque
;
2190 NetCharDriver
*s
= chr
->opaque
;
2192 s
->max_size
= qemu_chr_be_can_write(chr
);
2194 /* If there were any stray characters in the queue process them
2197 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2198 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2200 s
->max_size
= qemu_chr_be_can_write(chr
);
2205 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2207 CharDriverState
*chr
= opaque
;
2208 NetCharDriver
*s
= chr
->opaque
;
2209 gsize bytes_read
= 0;
2212 if (s
->max_size
== 0) {
2215 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2217 s
->bufcnt
= bytes_read
;
2218 s
->bufptr
= s
->bufcnt
;
2219 if (status
!= G_IO_STATUS_NORMAL
) {
2221 io_remove_watch_poll(s
->tag
);
2228 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2229 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2231 s
->max_size
= qemu_chr_be_can_write(chr
);
2237 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2239 NetCharDriver
*s
= chr
->opaque
;
2242 io_remove_watch_poll(s
->tag
);
2247 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2251 static void udp_chr_close(CharDriverState
*chr
)
2253 NetCharDriver
*s
= chr
->opaque
;
2255 io_remove_watch_poll(s
->tag
);
2259 g_io_channel_unref(s
->chan
);
2263 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2266 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2268 CharDriverState
*chr
= NULL
;
2269 NetCharDriver
*s
= NULL
;
2271 chr
= g_malloc0(sizeof(CharDriverState
));
2272 s
= g_malloc0(sizeof(NetCharDriver
));
2275 s
->chan
= io_channel_from_socket(s
->fd
);
2279 chr
->chr_write
= udp_chr_write
;
2280 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2281 chr
->chr_close
= udp_chr_close
;
2282 /* be isn't opened until we get a connection */
2283 chr
->explicit_be_open
= true;
2287 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2289 Error
*local_err
= NULL
;
2292 fd
= inet_dgram_opts(opts
, &local_err
);
2294 qerror_report_err(local_err
);
2295 error_free(local_err
);
2298 return qemu_chr_open_udp_fd(fd
);
2301 /***********************************************************/
2302 /* TCP Net console */
2306 GIOChannel
*chan
, *listen_chan
;
2307 guint tag
, listen_tag
;
2317 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2319 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2321 TCPCharDriver
*s
= chr
->opaque
;
2323 return io_channel_send(s
->chan
, buf
, len
);
2325 /* XXX: indicate an error ? */
2330 static int tcp_chr_read_poll(void *opaque
)
2332 CharDriverState
*chr
= opaque
;
2333 TCPCharDriver
*s
= chr
->opaque
;
2336 s
->max_size
= qemu_chr_be_can_write(chr
);
2341 #define IAC_BREAK 243
2342 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2344 uint8_t *buf
, int *size
)
2346 /* Handle any telnet client's basic IAC options to satisfy char by
2347 * char mode with no echo. All IAC options will be removed from
2348 * the buf and the do_telnetopt variable will be used to track the
2349 * state of the width of the IAC information.
2351 * IAC commands come in sets of 3 bytes with the exception of the
2352 * "IAC BREAK" command and the double IAC.
2358 for (i
= 0; i
< *size
; i
++) {
2359 if (s
->do_telnetopt
> 1) {
2360 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2361 /* Double IAC means send an IAC */
2365 s
->do_telnetopt
= 1;
2367 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2368 /* Handle IAC break commands by sending a serial break */
2369 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2374 if (s
->do_telnetopt
>= 4) {
2375 s
->do_telnetopt
= 1;
2378 if ((unsigned char)buf
[i
] == IAC
) {
2379 s
->do_telnetopt
= 2;
2390 static int tcp_get_msgfd(CharDriverState
*chr
)
2392 TCPCharDriver
*s
= chr
->opaque
;
2399 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2401 TCPCharDriver
*s
= chr
->opaque
;
2402 struct cmsghdr
*cmsg
;
2404 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2407 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2408 cmsg
->cmsg_level
!= SOL_SOCKET
||
2409 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2412 fd
= *((int *)CMSG_DATA(cmsg
));
2416 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2419 #ifndef MSG_CMSG_CLOEXEC
2420 qemu_set_cloexec(fd
);
2428 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2430 TCPCharDriver
*s
= chr
->opaque
;
2431 struct msghdr msg
= { NULL
, };
2432 struct iovec iov
[1];
2434 struct cmsghdr cmsg
;
2435 char control
[CMSG_SPACE(sizeof(int))];
2440 iov
[0].iov_base
= buf
;
2441 iov
[0].iov_len
= len
;
2445 msg
.msg_control
= &msg_control
;
2446 msg
.msg_controllen
= sizeof(msg_control
);
2448 #ifdef MSG_CMSG_CLOEXEC
2449 flags
|= MSG_CMSG_CLOEXEC
;
2451 ret
= recvmsg(s
->fd
, &msg
, flags
);
2452 if (ret
> 0 && s
->is_unix
) {
2453 unix_process_msgfd(chr
, &msg
);
2459 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2461 TCPCharDriver
*s
= chr
->opaque
;
2462 return qemu_recv(s
->fd
, buf
, len
, 0);
2466 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2468 TCPCharDriver
*s
= chr
->opaque
;
2469 return g_io_create_watch(s
->chan
, cond
);
2472 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2474 CharDriverState
*chr
= opaque
;
2475 TCPCharDriver
*s
= chr
->opaque
;
2476 uint8_t buf
[READ_BUF_LEN
];
2479 if (!s
->connected
|| s
->max_size
<= 0) {
2483 if (len
> s
->max_size
)
2485 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2487 /* connection closed */
2489 if (s
->listen_chan
) {
2490 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2493 io_remove_watch_poll(s
->tag
);
2496 g_io_channel_unref(s
->chan
);
2500 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2501 } else if (size
> 0) {
2502 if (s
->do_telnetopt
)
2503 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2505 qemu_chr_be_write(chr
, buf
, size
);
2512 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2514 return qemu_chr_open_fd(eventfd
, eventfd
);
2518 static void tcp_chr_connect(void *opaque
)
2520 CharDriverState
*chr
= opaque
;
2521 TCPCharDriver
*s
= chr
->opaque
;
2525 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2527 qemu_chr_be_generic_open(chr
);
2530 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2531 static void tcp_chr_telnet_init(int fd
)
2534 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2535 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2536 send(fd
, (char *)buf
, 3, 0);
2537 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2538 send(fd
, (char *)buf
, 3, 0);
2539 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2540 send(fd
, (char *)buf
, 3, 0);
2541 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2542 send(fd
, (char *)buf
, 3, 0);
2545 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2547 TCPCharDriver
*s
= chr
->opaque
;
2551 qemu_set_nonblock(fd
);
2553 socket_set_nodelay(fd
);
2555 s
->chan
= io_channel_from_socket(fd
);
2556 if (s
->listen_tag
) {
2557 g_source_remove(s
->listen_tag
);
2560 tcp_chr_connect(chr
);
2565 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2567 CharDriverState
*chr
= opaque
;
2568 TCPCharDriver
*s
= chr
->opaque
;
2569 struct sockaddr_in saddr
;
2571 struct sockaddr_un uaddr
;
2573 struct sockaddr
*addr
;
2580 len
= sizeof(uaddr
);
2581 addr
= (struct sockaddr
*)&uaddr
;
2585 len
= sizeof(saddr
);
2586 addr
= (struct sockaddr
*)&saddr
;
2588 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2589 if (fd
< 0 && errno
!= EINTR
) {
2592 } else if (fd
>= 0) {
2593 if (s
->do_telnetopt
)
2594 tcp_chr_telnet_init(fd
);
2598 if (tcp_chr_add_client(chr
, fd
) < 0)
2604 static void tcp_chr_close(CharDriverState
*chr
)
2606 TCPCharDriver
*s
= chr
->opaque
;
2609 io_remove_watch_poll(s
->tag
);
2613 g_io_channel_unref(s
->chan
);
2617 if (s
->listen_fd
>= 0) {
2618 if (s
->listen_tag
) {
2619 g_source_remove(s
->listen_tag
);
2622 if (s
->listen_chan
) {
2623 g_io_channel_unref(s
->listen_chan
);
2625 closesocket(s
->listen_fd
);
2628 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2631 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2632 bool is_listen
, bool is_telnet
,
2633 bool is_waitconnect
,
2636 CharDriverState
*chr
= NULL
;
2637 TCPCharDriver
*s
= NULL
;
2638 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2639 const char *left
= "", *right
= "";
2640 struct sockaddr_storage ss
;
2641 socklen_t ss_len
= sizeof(ss
);
2643 memset(&ss
, 0, ss_len
);
2644 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2645 error_setg_errno(errp
, errno
, "getsockname");
2649 chr
= g_malloc0(sizeof(CharDriverState
));
2650 s
= g_malloc0(sizeof(TCPCharDriver
));
2657 chr
->filename
= g_malloc(256);
2658 switch (ss
.ss_family
) {
2662 snprintf(chr
->filename
, 256, "unix:%s%s",
2663 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2664 is_listen
? ",server" : "");
2672 s
->do_nodelay
= do_nodelay
;
2673 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2674 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2675 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2676 is_telnet
? "telnet" : "tcp",
2677 left
, host
, right
, serv
,
2678 is_listen
? ",server" : "");
2683 chr
->chr_write
= tcp_chr_write
;
2684 chr
->chr_close
= tcp_chr_close
;
2685 chr
->get_msgfd
= tcp_get_msgfd
;
2686 chr
->chr_add_client
= tcp_chr_add_client
;
2687 chr
->chr_add_watch
= tcp_chr_add_watch
;
2688 /* be isn't opened until we get a connection */
2689 chr
->explicit_be_open
= true;
2693 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2694 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2696 s
->do_telnetopt
= 1;
2701 socket_set_nodelay(fd
);
2702 s
->chan
= io_channel_from_socket(s
->fd
);
2703 tcp_chr_connect(chr
);
2706 if (is_listen
&& is_waitconnect
) {
2707 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2709 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2710 qemu_set_nonblock(s
->listen_fd
);
2715 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2717 CharDriverState
*chr
= NULL
;
2718 Error
*local_err
= NULL
;
2721 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2722 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2723 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2724 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2725 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2729 fd
= unix_listen_opts(opts
, &local_err
);
2731 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2735 fd
= inet_listen_opts(opts
, 0, &local_err
);
2737 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2744 if (!is_waitconnect
)
2745 qemu_set_nonblock(fd
);
2747 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2748 is_waitconnect
, &local_err
);
2749 if (error_is_set(&local_err
)) {
2757 qerror_report_err(local_err
);
2758 error_free(local_err
);
2764 g_free(chr
->opaque
);
2770 /*********************************************************/
2771 /* Ring buffer chardev */
2778 } RingBufCharDriver
;
2780 static size_t ringbuf_count(const CharDriverState
*chr
)
2782 const RingBufCharDriver
*d
= chr
->opaque
;
2784 return d
->prod
- d
->cons
;
2787 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2789 RingBufCharDriver
*d
= chr
->opaque
;
2792 if (!buf
|| (len
< 0)) {
2796 for (i
= 0; i
< len
; i
++ ) {
2797 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2798 if (d
->prod
- d
->cons
> d
->size
) {
2799 d
->cons
= d
->prod
- d
->size
;
2806 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2808 RingBufCharDriver
*d
= chr
->opaque
;
2811 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2812 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2818 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2820 RingBufCharDriver
*d
= chr
->opaque
;
2827 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2830 CharDriverState
*chr
;
2831 RingBufCharDriver
*d
;
2833 chr
= g_malloc0(sizeof(CharDriverState
));
2834 d
= g_malloc(sizeof(*d
));
2836 d
->size
= opts
->has_size
? opts
->size
: 65536;
2838 /* The size must be power of 2 */
2839 if (d
->size
& (d
->size
- 1)) {
2840 error_setg(errp
, "size of ringbuf chardev must be power of two");
2846 d
->cbuf
= g_malloc0(d
->size
);
2849 chr
->chr_write
= ringbuf_chr_write
;
2850 chr
->chr_close
= ringbuf_chr_close
;
2860 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2862 return chr
->chr_write
== ringbuf_chr_write
;
2865 void qmp_ringbuf_write(const char *device
, const char *data
,
2866 bool has_format
, enum DataFormat format
,
2869 CharDriverState
*chr
;
2870 const uint8_t *write_data
;
2874 chr
= qemu_chr_find(device
);
2876 error_setg(errp
, "Device '%s' not found", device
);
2880 if (!chr_is_ringbuf(chr
)) {
2881 error_setg(errp
,"%s is not a ringbuf device", device
);
2885 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2886 write_data
= g_base64_decode(data
, &write_count
);
2888 write_data
= (uint8_t *)data
;
2889 write_count
= strlen(data
);
2892 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2894 if (write_data
!= (uint8_t *)data
) {
2895 g_free((void *)write_data
);
2899 error_setg(errp
, "Failed to write to device %s", device
);
2904 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2905 bool has_format
, enum DataFormat format
,
2908 CharDriverState
*chr
;
2913 chr
= qemu_chr_find(device
);
2915 error_setg(errp
, "Device '%s' not found", device
);
2919 if (!chr_is_ringbuf(chr
)) {
2920 error_setg(errp
,"%s is not a ringbuf device", device
);
2925 error_setg(errp
, "size must be greater than zero");
2929 count
= ringbuf_count(chr
);
2930 size
= size
> count
? count
: size
;
2931 read_data
= g_malloc(size
+ 1);
2933 ringbuf_chr_read(chr
, read_data
, size
);
2935 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2936 data
= g_base64_encode(read_data
, size
);
2940 * FIXME should read only complete, valid UTF-8 characters up
2941 * to @size bytes. Invalid sequences should be replaced by a
2942 * suitable replacement character. Except when (and only
2943 * when) ring buffer lost characters since last read, initial
2944 * continuation characters should be dropped.
2946 read_data
[size
] = 0;
2947 data
= (char *)read_data
;
2953 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2955 char host
[65], port
[33], width
[8], height
[8];
2959 Error
*local_err
= NULL
;
2961 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2962 if (error_is_set(&local_err
)) {
2963 qerror_report_err(local_err
);
2964 error_free(local_err
);
2968 if (strstart(filename
, "mon:", &p
)) {
2970 qemu_opt_set(opts
, "mux", "on");
2971 if (strcmp(filename
, "stdio") == 0) {
2972 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2973 * but pass it to the guest. Handle this only for compat syntax,
2974 * for -chardev syntax we have special option for this.
2975 * This is what -nographic did, redirecting+muxing serial+monitor
2976 * to stdio causing Ctrl+C to be passed to guest. */
2977 qemu_opt_set(opts
, "signal", "off");
2981 if (strcmp(filename
, "null") == 0 ||
2982 strcmp(filename
, "pty") == 0 ||
2983 strcmp(filename
, "msmouse") == 0 ||
2984 strcmp(filename
, "braille") == 0 ||
2985 strcmp(filename
, "stdio") == 0) {
2986 qemu_opt_set(opts
, "backend", filename
);
2989 if (strstart(filename
, "vc", &p
)) {
2990 qemu_opt_set(opts
, "backend", "vc");
2992 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
2994 qemu_opt_set(opts
, "width", width
);
2995 qemu_opt_set(opts
, "height", height
);
2996 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
2998 qemu_opt_set(opts
, "cols", width
);
2999 qemu_opt_set(opts
, "rows", height
);
3006 if (strcmp(filename
, "con:") == 0) {
3007 qemu_opt_set(opts
, "backend", "console");
3010 if (strstart(filename
, "COM", NULL
)) {
3011 qemu_opt_set(opts
, "backend", "serial");
3012 qemu_opt_set(opts
, "path", filename
);
3015 if (strstart(filename
, "file:", &p
)) {
3016 qemu_opt_set(opts
, "backend", "file");
3017 qemu_opt_set(opts
, "path", p
);
3020 if (strstart(filename
, "pipe:", &p
)) {
3021 qemu_opt_set(opts
, "backend", "pipe");
3022 qemu_opt_set(opts
, "path", p
);
3025 if (strstart(filename
, "tcp:", &p
) ||
3026 strstart(filename
, "telnet:", &p
)) {
3027 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3029 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3032 qemu_opt_set(opts
, "backend", "socket");
3033 qemu_opt_set(opts
, "host", host
);
3034 qemu_opt_set(opts
, "port", port
);
3035 if (p
[pos
] == ',') {
3036 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3039 if (strstart(filename
, "telnet:", &p
))
3040 qemu_opt_set(opts
, "telnet", "on");
3043 if (strstart(filename
, "udp:", &p
)) {
3044 qemu_opt_set(opts
, "backend", "udp");
3045 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3047 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3051 qemu_opt_set(opts
, "host", host
);
3052 qemu_opt_set(opts
, "port", port
);
3053 if (p
[pos
] == '@') {
3055 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3057 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3061 qemu_opt_set(opts
, "localaddr", host
);
3062 qemu_opt_set(opts
, "localport", port
);
3066 if (strstart(filename
, "unix:", &p
)) {
3067 qemu_opt_set(opts
, "backend", "socket");
3068 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3072 if (strstart(filename
, "/dev/parport", NULL
) ||
3073 strstart(filename
, "/dev/ppi", NULL
)) {
3074 qemu_opt_set(opts
, "backend", "parport");
3075 qemu_opt_set(opts
, "path", filename
);
3078 if (strstart(filename
, "/dev/", NULL
)) {
3079 qemu_opt_set(opts
, "backend", "tty");
3080 qemu_opt_set(opts
, "path", filename
);
3085 qemu_opts_del(opts
);
3089 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3092 const char *path
= qemu_opt_get(opts
, "path");
3095 error_setg(errp
, "chardev: file: no filename given");
3098 backend
->file
= g_new0(ChardevFile
, 1);
3099 backend
->file
->out
= g_strdup(path
);
3102 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3105 backend
->stdio
= g_new0(ChardevStdio
, 1);
3106 backend
->stdio
->has_signal
= true;
3107 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3110 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3113 const char *device
= qemu_opt_get(opts
, "path");
3115 if (device
== NULL
) {
3116 error_setg(errp
, "chardev: serial/tty: no device path given");
3119 backend
->serial
= g_new0(ChardevHostdev
, 1);
3120 backend
->serial
->device
= g_strdup(device
);
3123 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3126 const char *device
= qemu_opt_get(opts
, "path");
3128 if (device
== NULL
) {
3129 error_setg(errp
, "chardev: parallel: no device path given");
3132 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3133 backend
->parallel
->device
= g_strdup(device
);
3136 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3139 const char *device
= qemu_opt_get(opts
, "path");
3141 if (device
== NULL
) {
3142 error_setg(errp
, "chardev: pipe: no device path given");
3145 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3146 backend
->pipe
->device
= g_strdup(device
);
3149 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3154 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3156 val
= qemu_opt_get_size(opts
, "size", 0);
3158 backend
->ringbuf
->has_size
= true;
3159 backend
->ringbuf
->size
= val
;
3163 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3166 const char *chardev
= qemu_opt_get(opts
, "chardev");
3168 if (chardev
== NULL
) {
3169 error_setg(errp
, "chardev: mux: no chardev given");
3172 backend
->mux
= g_new0(ChardevMux
, 1);
3173 backend
->mux
->chardev
= g_strdup(chardev
);
3176 typedef struct CharDriver
{
3179 CharDriverState
*(*open
)(QemuOpts
*opts
);
3180 /* new, qapi-based */
3181 ChardevBackendKind kind
;
3182 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3185 static GSList
*backends
;
3187 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3191 s
= g_malloc0(sizeof(*s
));
3192 s
->name
= g_strdup(name
);
3195 backends
= g_slist_append(backends
, s
);
3198 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3199 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3203 s
= g_malloc0(sizeof(*s
));
3204 s
->name
= g_strdup(name
);
3208 backends
= g_slist_append(backends
, s
);
3211 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3212 void (*init
)(struct CharDriverState
*s
),
3216 CharDriverState
*chr
;
3219 if (qemu_opts_id(opts
) == NULL
) {
3220 error_setg(errp
, "chardev: no id specified");
3224 if (qemu_opt_get(opts
, "backend") == NULL
) {
3225 error_setg(errp
, "chardev: \"%s\" missing backend",
3226 qemu_opts_id(opts
));
3229 for (i
= backends
; i
; i
= i
->next
) {
3232 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3237 error_setg(errp
, "chardev: backend \"%s\" not found",
3238 qemu_opt_get(opts
, "backend"));
3243 /* using new, qapi init */
3244 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3245 ChardevReturn
*ret
= NULL
;
3246 const char *id
= qemu_opts_id(opts
);
3249 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3250 bid
= g_strdup_printf("%s-base", id
);
3254 backend
->kind
= cd
->kind
;
3256 cd
->parse(opts
, backend
, errp
);
3257 if (error_is_set(errp
)) {
3261 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3262 if (error_is_set(errp
)) {
3267 qapi_free_ChardevBackend(backend
);
3268 qapi_free_ChardevReturn(ret
);
3269 backend
= g_new0(ChardevBackend
, 1);
3270 backend
->mux
= g_new0(ChardevMux
, 1);
3271 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3272 backend
->mux
->chardev
= g_strdup(bid
);
3273 ret
= qmp_chardev_add(id
, backend
, errp
);
3274 assert(!error_is_set(errp
));
3277 chr
= qemu_chr_find(id
);
3281 qapi_free_ChardevBackend(backend
);
3282 qapi_free_ChardevReturn(ret
);
3287 chr
= cd
->open(opts
);
3289 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3290 qemu_opt_get(opts
, "backend"));
3295 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3297 /* if we didn't create the chardev via qmp_chardev_add, we
3298 * need to send the OPENED event here
3300 if (!chr
->explicit_be_open
) {
3301 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3303 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3305 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3306 CharDriverState
*base
= chr
;
3307 int len
= strlen(qemu_opts_id(opts
)) + 6;
3308 base
->label
= g_malloc(len
);
3309 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3310 chr
= qemu_chr_open_mux(base
);
3311 chr
->filename
= base
->filename
;
3312 chr
->avail_connections
= MAX_MUX
;
3313 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3315 chr
->avail_connections
= 1;
3317 chr
->label
= g_strdup(qemu_opts_id(opts
));
3322 qemu_opts_del(opts
);
3326 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3329 CharDriverState
*chr
;
3333 if (strstart(filename
, "chardev:", &p
)) {
3334 return qemu_chr_find(p
);
3337 opts
= qemu_chr_parse_compat(label
, filename
);
3341 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3342 if (error_is_set(&err
)) {
3343 error_report("%s", error_get_pretty(err
));
3346 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3347 qemu_chr_fe_claim_no_fail(chr
);
3348 monitor_init(chr
, MONITOR_USE_READLINE
);
3353 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3355 if (chr
->chr_set_echo
) {
3356 chr
->chr_set_echo(chr
, echo
);
3360 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3362 if (chr
->fe_open
== fe_open
) {
3365 chr
->fe_open
= fe_open
;
3366 if (chr
->chr_set_fe_open
) {
3367 chr
->chr_set_fe_open(chr
, fe_open
);
3371 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3372 GIOFunc func
, void *user_data
)
3377 if (s
->chr_add_watch
== NULL
) {
3381 src
= s
->chr_add_watch(s
, cond
);
3382 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3383 tag
= g_source_attach(src
, NULL
);
3384 g_source_unref(src
);
3389 int qemu_chr_fe_claim(CharDriverState
*s
)
3391 if (s
->avail_connections
< 1) {
3394 s
->avail_connections
--;
3398 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3400 if (qemu_chr_fe_claim(s
) != 0) {
3401 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3402 __func__
, s
->label
);
3407 void qemu_chr_fe_release(CharDriverState
*s
)
3409 s
->avail_connections
++;
3412 void qemu_chr_delete(CharDriverState
*chr
)
3414 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3415 if (chr
->chr_close
) {
3416 chr
->chr_close(chr
);
3418 g_free(chr
->filename
);
3421 qemu_opts_del(chr
->opts
);
3426 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3428 ChardevInfoList
*chr_list
= NULL
;
3429 CharDriverState
*chr
;
3431 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3432 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3433 info
->value
= g_malloc0(sizeof(*info
->value
));
3434 info
->value
->label
= g_strdup(chr
->label
);
3435 info
->value
->filename
= g_strdup(chr
->filename
);
3437 info
->next
= chr_list
;
3444 CharDriverState
*qemu_chr_find(const char *name
)
3446 CharDriverState
*chr
;
3448 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3449 if (strcmp(chr
->label
, name
) != 0)
3456 /* Get a character (serial) device interface. */
3457 CharDriverState
*qemu_char_get_next_serial(void)
3459 static int next_serial
;
3460 CharDriverState
*chr
;
3462 /* FIXME: This function needs to go away: use chardev properties! */
3464 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3465 chr
= serial_hds
[next_serial
++];
3466 qemu_chr_fe_claim_no_fail(chr
);
3472 QemuOptsList qemu_chardev_opts
= {
3474 .implied_opt_name
= "backend",
3475 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3479 .type
= QEMU_OPT_STRING
,
3482 .type
= QEMU_OPT_STRING
,
3485 .type
= QEMU_OPT_STRING
,
3488 .type
= QEMU_OPT_STRING
,
3490 .name
= "localaddr",
3491 .type
= QEMU_OPT_STRING
,
3493 .name
= "localport",
3494 .type
= QEMU_OPT_STRING
,
3497 .type
= QEMU_OPT_NUMBER
,
3500 .type
= QEMU_OPT_BOOL
,
3503 .type
= QEMU_OPT_BOOL
,
3506 .type
= QEMU_OPT_BOOL
,
3509 .type
= QEMU_OPT_BOOL
,
3512 .type
= QEMU_OPT_BOOL
,
3515 .type
= QEMU_OPT_BOOL
,
3518 .type
= QEMU_OPT_NUMBER
,
3521 .type
= QEMU_OPT_NUMBER
,
3524 .type
= QEMU_OPT_NUMBER
,
3527 .type
= QEMU_OPT_NUMBER
,
3530 .type
= QEMU_OPT_BOOL
,
3533 .type
= QEMU_OPT_BOOL
,
3536 .type
= QEMU_OPT_STRING
,
3539 .type
= QEMU_OPT_NUMBER
,
3542 .type
= QEMU_OPT_SIZE
,
3545 .type
= QEMU_OPT_STRING
,
3547 { /* end of list */ }
3553 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3558 error_setg(errp
, "input file not supported");
3562 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3563 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3564 if (out
== INVALID_HANDLE_VALUE
) {
3565 error_setg(errp
, "open %s failed", file
->out
);
3568 return qemu_chr_open_win_file(out
);
3571 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3574 return qemu_chr_open_win_path(serial
->device
);
3577 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3580 error_setg(errp
, "character device backend type 'parallel' not supported");
3586 static int qmp_chardev_open_file_source(char *src
, int flags
,
3591 TFR(fd
= qemu_open(src
, flags
, 0666));
3593 error_setg_file_open(errp
, errno
, src
);
3598 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3600 int flags
, in
= -1, out
= -1;
3602 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3603 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3604 if (error_is_set(errp
)) {
3610 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3611 if (error_is_set(errp
)) {
3617 return qemu_chr_open_fd(in
, out
);
3620 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3623 #ifdef HAVE_CHARDEV_TTY
3626 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3627 if (error_is_set(errp
)) {
3630 qemu_set_nonblock(fd
);
3631 return qemu_chr_open_tty_fd(fd
);
3633 error_setg(errp
, "character device backend type 'serial' not supported");
3638 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3641 #ifdef HAVE_CHARDEV_PARPORT
3644 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3645 if (error_is_set(errp
)) {
3648 return qemu_chr_open_pp_fd(fd
);
3650 error_setg(errp
, "character device backend type 'parallel' not supported");
3657 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3660 SocketAddress
*addr
= sock
->addr
;
3661 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3662 bool is_listen
= sock
->has_server
? sock
->server
: true;
3663 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3664 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3668 fd
= socket_listen(addr
, errp
);
3670 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3672 if (error_is_set(errp
)) {
3675 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3676 is_telnet
, is_waitconnect
, errp
);
3679 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3684 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3685 if (error_is_set(errp
)) {
3688 return qemu_chr_open_udp_fd(fd
);
3691 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3694 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3695 CharDriverState
*base
, *chr
= NULL
;
3697 chr
= qemu_chr_find(id
);
3699 error_setg(errp
, "Chardev '%s' already exists", id
);
3704 switch (backend
->kind
) {
3705 case CHARDEV_BACKEND_KIND_FILE
:
3706 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3708 case CHARDEV_BACKEND_KIND_SERIAL
:
3709 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3711 case CHARDEV_BACKEND_KIND_PARALLEL
:
3712 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3714 case CHARDEV_BACKEND_KIND_PIPE
:
3715 chr
= qemu_chr_open_pipe(backend
->pipe
);
3717 case CHARDEV_BACKEND_KIND_SOCKET
:
3718 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3720 case CHARDEV_BACKEND_KIND_UDP
:
3721 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
3723 #ifdef HAVE_CHARDEV_TTY
3724 case CHARDEV_BACKEND_KIND_PTY
:
3725 chr
= qemu_chr_open_pty(id
, ret
);
3728 case CHARDEV_BACKEND_KIND_NULL
:
3729 chr
= qemu_chr_open_null();
3731 case CHARDEV_BACKEND_KIND_MUX
:
3732 base
= qemu_chr_find(backend
->mux
->chardev
);
3734 error_setg(errp
, "mux: base chardev %s not found",
3735 backend
->mux
->chardev
);
3738 chr
= qemu_chr_open_mux(base
);
3740 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3741 chr
= qemu_chr_open_msmouse();
3743 #ifdef CONFIG_BRLAPI
3744 case CHARDEV_BACKEND_KIND_BRAILLE
:
3745 chr
= chr_baum_init();
3748 case CHARDEV_BACKEND_KIND_STDIO
:
3749 chr
= qemu_chr_open_stdio(backend
->stdio
);
3752 case CHARDEV_BACKEND_KIND_CONSOLE
:
3753 chr
= qemu_chr_open_win_con();
3757 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3758 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3760 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3761 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3764 case CHARDEV_BACKEND_KIND_VC
:
3765 chr
= vc_init(backend
->vc
);
3767 case CHARDEV_BACKEND_KIND_RINGBUF
:
3768 case CHARDEV_BACKEND_KIND_MEMORY
:
3769 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
3772 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3776 if (chr
== NULL
&& !error_is_set(errp
)) {
3777 error_setg(errp
, "Failed to create chardev");
3780 chr
->label
= g_strdup(id
);
3781 chr
->avail_connections
=
3782 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3783 if (!chr
->filename
) {
3784 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
3786 if (!chr
->explicit_be_open
) {
3787 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3789 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3797 void qmp_chardev_remove(const char *id
, Error
**errp
)
3799 CharDriverState
*chr
;
3801 chr
= qemu_chr_find(id
);
3803 error_setg(errp
, "Chardev '%s' not found", id
);
3806 if (chr
->chr_can_read
|| chr
->chr_read
||
3807 chr
->chr_event
|| chr
->handler_opaque
) {
3808 error_setg(errp
, "Chardev '%s' is busy", id
);
3811 qemu_chr_delete(chr
);
3814 static void register_types(void)
3816 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3817 register_char_driver("socket", qemu_chr_open_socket
);
3818 register_char_driver("udp", qemu_chr_open_udp
);
3819 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
3820 qemu_chr_parse_ringbuf
);
3821 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3822 qemu_chr_parse_file_out
);
3823 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3824 qemu_chr_parse_stdio
);
3825 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3826 qemu_chr_parse_serial
);
3827 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3828 qemu_chr_parse_serial
);
3829 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3830 qemu_chr_parse_parallel
);
3831 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3832 qemu_chr_parse_parallel
);
3833 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3834 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3835 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3836 qemu_chr_parse_pipe
);
3837 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
3838 qemu_chr_parse_mux
);
3839 /* Bug-compatibility: */
3840 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3841 qemu_chr_parse_ringbuf
);
3842 /* this must be done after machine init, since we register FEs with muxes
3843 * as part of realize functions like serial_isa_realizefn when -nographic
3846 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
3849 type_init(register_types
);