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 "sysemu/sysemu.h"
27 #include "qemu/error-report.h"
28 #include "qemu/timer.h"
29 #include "sysemu/char.h"
31 #include "qmp-commands.h"
32 #include "qapi/qmp-input-visitor.h"
33 #include "qapi/qmp-output-visitor.h"
34 #include "qapi-visit.h"
44 #include <sys/times.h>
48 #include <sys/ioctl.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
53 #include <arpa/inet.h>
55 #include <sys/select.h>
58 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
59 #include <dev/ppbus/ppi.h>
60 #include <dev/ppbus/ppbconf.h>
61 #elif defined(__DragonFly__)
62 #include <dev/misc/ppi/ppi.h>
63 #include <bus/ppbus/ppbconf.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
85 #include "qemu/sockets.h"
86 #include "ui/qemu-spice.h"
88 #define READ_BUF_LEN 4096
89 #define READ_RETRIES 10
90 #define CHR_MAX_FILENAME_SIZE 256
91 #define TCP_MAX_FDS 16
93 /***********************************************************/
94 /* Socket address helpers */
96 static int SocketAddress_to_str(char *dest
, int max_len
,
97 const char *prefix
, SocketAddress
*addr
,
98 bool is_listen
, bool is_telnet
)
100 switch (addr
->type
) {
101 case SOCKET_ADDRESS_KIND_INET
:
102 return snprintf(dest
, max_len
, "%s%s:%s:%s%s", prefix
,
103 is_telnet
? "telnet" : "tcp", addr
->u
.inet
->host
,
104 addr
->u
.inet
->port
, is_listen
? ",server" : "");
106 case SOCKET_ADDRESS_KIND_UNIX
:
107 return snprintf(dest
, max_len
, "%sunix:%s%s", prefix
,
108 addr
->u
.q_unix
->path
, is_listen
? ",server" : "");
110 case SOCKET_ADDRESS_KIND_FD
:
111 return snprintf(dest
, max_len
, "%sfd:%s%s", prefix
, addr
->u
.fd
->str
,
112 is_listen
? ",server" : "");
119 static int sockaddr_to_str(char *dest
, int max_len
,
120 struct sockaddr_storage
*ss
, socklen_t ss_len
,
121 struct sockaddr_storage
*ps
, socklen_t ps_len
,
122 bool is_listen
, bool is_telnet
)
124 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
125 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
126 const char *left
= "", *right
= "";
128 switch (ss
->ss_family
) {
131 return snprintf(dest
, max_len
, "unix:%s%s",
132 ((struct sockaddr_un
*)(ss
))->sun_path
,
133 is_listen
? ",server" : "");
140 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
141 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
142 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
143 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
144 return snprintf(dest
, max_len
, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
145 is_telnet
? "telnet" : "tcp",
146 left
, shost
, right
, sserv
,
147 is_listen
? ",server" : "",
148 left
, phost
, right
, pserv
);
151 return snprintf(dest
, max_len
, "unknown");
155 /***********************************************************/
156 /* character device */
158 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
159 QTAILQ_HEAD_INITIALIZER(chardevs
);
161 CharDriverState
*qemu_chr_alloc(void)
163 CharDriverState
*chr
= g_malloc0(sizeof(CharDriverState
));
164 qemu_mutex_init(&chr
->chr_write_lock
);
168 void qemu_chr_be_event(CharDriverState
*s
, int event
)
170 /* Keep track if the char device is open */
172 case CHR_EVENT_OPENED
:
175 case CHR_EVENT_CLOSED
:
182 s
->chr_event(s
->handler_opaque
, event
);
185 void qemu_chr_be_generic_open(CharDriverState
*s
)
187 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
190 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
194 qemu_mutex_lock(&s
->chr_write_lock
);
195 ret
= s
->chr_write(s
, buf
, len
);
196 qemu_mutex_unlock(&s
->chr_write_lock
);
200 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
205 qemu_mutex_lock(&s
->chr_write_lock
);
206 while (offset
< len
) {
208 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
209 if (res
== -1 && errno
== EAGAIN
) {
212 } while (res
== -1 && errno
== EAGAIN
);
220 qemu_mutex_unlock(&s
->chr_write_lock
);
228 int qemu_chr_fe_read_all(CharDriverState
*s
, uint8_t *buf
, int len
)
230 int offset
= 0, counter
= 10;
233 if (!s
->chr_sync_read
) {
237 while (offset
< len
) {
239 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
240 if (res
== -1 && errno
== EAGAIN
) {
243 } while (res
== -1 && errno
== EAGAIN
);
263 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
267 return s
->chr_ioctl(s
, cmd
, arg
);
270 int qemu_chr_be_can_write(CharDriverState
*s
)
272 if (!s
->chr_can_read
)
274 return s
->chr_can_read(s
->handler_opaque
);
277 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
280 s
->chr_read(s
->handler_opaque
, buf
, len
);
284 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
287 return (qemu_chr_fe_get_msgfds(s
, &fd
, 1) == 1) ? fd
: -1;
290 int qemu_chr_fe_get_msgfds(CharDriverState
*s
, int *fds
, int len
)
292 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
295 int qemu_chr_fe_set_msgfds(CharDriverState
*s
, int *fds
, int num
)
297 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
300 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
302 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
305 void qemu_chr_accept_input(CharDriverState
*s
)
307 if (s
->chr_accept_input
)
308 s
->chr_accept_input(s
);
312 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
314 char buf
[READ_BUF_LEN
];
317 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
318 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
322 static void remove_fd_in_watch(CharDriverState
*chr
);
324 void qemu_chr_add_handlers(CharDriverState
*s
,
325 IOCanReadHandler
*fd_can_read
,
326 IOReadHandler
*fd_read
,
327 IOEventHandler
*fd_event
,
332 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
334 remove_fd_in_watch(s
);
338 s
->chr_can_read
= fd_can_read
;
339 s
->chr_read
= fd_read
;
340 s
->chr_event
= fd_event
;
341 s
->handler_opaque
= opaque
;
342 if (fe_open
&& s
->chr_update_read_handler
)
343 s
->chr_update_read_handler(s
);
345 if (!s
->explicit_fe_open
) {
346 qemu_chr_fe_set_open(s
, fe_open
);
349 /* We're connecting to an already opened device, so let's make sure we
350 also get the open event */
351 if (fe_open
&& s
->be_open
) {
352 qemu_chr_be_generic_open(s
);
356 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
361 static CharDriverState
*qemu_chr_open_null(const char *id
,
362 ChardevBackend
*backend
,
366 CharDriverState
*chr
;
368 chr
= qemu_chr_alloc();
369 chr
->chr_write
= null_chr_write
;
370 chr
->explicit_be_open
= true;
374 /* MUX driver for serial I/O splitting */
376 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
377 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
379 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
380 IOReadHandler
*chr_read
[MAX_MUX
];
381 IOEventHandler
*chr_event
[MAX_MUX
];
382 void *ext_opaque
[MAX_MUX
];
383 CharDriverState
*drv
;
388 /* Intermediate input buffer allows to catch escape sequences even if the
389 currently active device is not accepting any input - but only until it
391 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
396 /* Protected by the CharDriverState chr_write_lock. */
398 int64_t timestamps_start
;
402 /* Called with chr_write_lock held. */
403 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
405 MuxDriver
*d
= chr
->opaque
;
407 if (!d
->timestamps
) {
408 ret
= qemu_chr_fe_write(d
->drv
, buf
, len
);
413 for (i
= 0; i
< len
; i
++) {
419 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
420 if (d
->timestamps_start
== -1)
421 d
->timestamps_start
= ti
;
422 ti
-= d
->timestamps_start
;
424 snprintf(buf1
, sizeof(buf1
),
425 "[%02d:%02d:%02d.%03d] ",
430 qemu_chr_fe_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
433 ret
+= qemu_chr_fe_write(d
->drv
, buf
+i
, 1);
434 if (buf
[i
] == '\n') {
442 static const char * const mux_help
[] = {
443 "% h print this help\n\r",
444 "% x exit emulator\n\r",
445 "% s save disk data back to file (if -snapshot)\n\r",
446 "% t toggle console timestamps\n\r",
447 "% b send break (magic sysrq)\n\r",
448 "% c switch between console and monitor\n\r",
453 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
454 static void mux_print_help(CharDriverState
*chr
)
457 char ebuf
[15] = "Escape-Char";
458 char cbuf
[50] = "\n\r";
460 if (term_escape_char
> 0 && term_escape_char
< 26) {
461 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
462 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
464 snprintf(cbuf
, sizeof(cbuf
),
465 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
468 qemu_chr_fe_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
469 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
470 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
471 if (mux_help
[i
][j
] == '%')
472 qemu_chr_fe_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
474 qemu_chr_fe_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
479 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
481 if (d
->chr_event
[mux_nr
])
482 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
485 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
487 if (d
->term_got_escape
) {
488 d
->term_got_escape
= 0;
489 if (ch
== term_escape_char
)
498 const char *term
= "QEMU: Terminated\n\r";
499 qemu_chr_fe_write(chr
, (uint8_t *)term
, strlen(term
));
507 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
510 /* Switch to the next registered device */
511 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
513 if (d
->focus
>= d
->mux_cnt
)
515 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
518 d
->timestamps
= !d
->timestamps
;
519 d
->timestamps_start
= -1;
523 } else if (ch
== term_escape_char
) {
524 d
->term_got_escape
= 1;
532 static void mux_chr_accept_input(CharDriverState
*chr
)
534 MuxDriver
*d
= chr
->opaque
;
537 while (d
->prod
[m
] != d
->cons
[m
] &&
538 d
->chr_can_read
[m
] &&
539 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
540 d
->chr_read
[m
](d
->ext_opaque
[m
],
541 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
545 static int mux_chr_can_read(void *opaque
)
547 CharDriverState
*chr
= opaque
;
548 MuxDriver
*d
= chr
->opaque
;
551 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
553 if (d
->chr_can_read
[m
])
554 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
558 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
560 CharDriverState
*chr
= opaque
;
561 MuxDriver
*d
= chr
->opaque
;
565 mux_chr_accept_input (opaque
);
567 for(i
= 0; i
< size
; i
++)
568 if (mux_proc_byte(chr
, d
, buf
[i
])) {
569 if (d
->prod
[m
] == d
->cons
[m
] &&
570 d
->chr_can_read
[m
] &&
571 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
572 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
574 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
578 static void mux_chr_event(void *opaque
, int event
)
580 CharDriverState
*chr
= opaque
;
581 MuxDriver
*d
= chr
->opaque
;
584 /* Send the event to all registered listeners */
585 for (i
= 0; i
< d
->mux_cnt
; i
++)
586 mux_chr_send_event(d
, i
, event
);
589 static void mux_chr_update_read_handler(CharDriverState
*chr
)
591 MuxDriver
*d
= chr
->opaque
;
593 if (d
->mux_cnt
>= MAX_MUX
) {
594 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
597 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
598 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
599 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
600 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
601 /* Fix up the real driver with mux routines */
602 if (d
->mux_cnt
== 0) {
603 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
606 if (d
->focus
!= -1) {
607 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
609 d
->focus
= d
->mux_cnt
;
611 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
614 static bool muxes_realized
;
617 * Called after processing of default and command-line-specified
618 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
619 * to a mux chardev. This is done here to ensure that
620 * output/prompts/banners are only displayed for the FE that has
621 * focus when initial command-line processing/machine init is
624 * After this point, any new FE attached to any new or existing
625 * mux will receive CHR_EVENT_OPENED notifications for the BE
628 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
630 CharDriverState
*chr
;
632 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
634 MuxDriver
*d
= chr
->opaque
;
637 /* send OPENED to all already-attached FEs */
638 for (i
= 0; i
< d
->mux_cnt
; i
++) {
639 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
641 /* mark mux as OPENED so any new FEs will immediately receive
644 qemu_chr_be_generic_open(chr
);
647 muxes_realized
= true;
650 static Notifier muxes_realize_notify
= {
651 .notify
= muxes_realize_done
,
654 static GSource
*mux_chr_add_watch(CharDriverState
*s
, GIOCondition cond
)
656 MuxDriver
*d
= s
->opaque
;
657 return d
->drv
->chr_add_watch(d
->drv
, cond
);
660 static CharDriverState
*qemu_chr_open_mux(const char *id
,
661 ChardevBackend
*backend
,
662 ChardevReturn
*ret
, Error
**errp
)
664 ChardevMux
*mux
= backend
->u
.mux
;
665 CharDriverState
*chr
, *drv
;
668 drv
= qemu_chr_find(mux
->chardev
);
670 error_setg(errp
, "mux: base chardev %s not found", mux
->chardev
);
674 chr
= qemu_chr_alloc();
675 d
= g_new0(MuxDriver
, 1);
680 chr
->chr_write
= mux_chr_write
;
681 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
682 chr
->chr_accept_input
= mux_chr_accept_input
;
683 /* Frontend guest-open / -close notification is not support with muxes */
684 chr
->chr_set_fe_open
= NULL
;
685 if (drv
->chr_add_watch
) {
686 chr
->chr_add_watch
= mux_chr_add_watch
;
688 /* only default to opened state if we've realized the initial
691 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
699 int send_all(int fd
, const void *buf
, int len1
)
705 ret
= send(fd
, buf
, len
, 0);
707 errno
= WSAGetLastError();
708 if (errno
!= WSAEWOULDBLOCK
) {
711 } else if (ret
== 0) {
723 int send_all(int fd
, const void *_buf
, int len1
)
726 const uint8_t *buf
= _buf
;
730 ret
= write(fd
, buf
, len
);
732 if (errno
!= EINTR
&& errno
!= EAGAIN
)
734 } else if (ret
== 0) {
744 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
750 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
752 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
769 typedef struct IOWatchPoll
776 IOCanReadHandler
*fd_can_read
;
781 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
783 return container_of(source
, IOWatchPoll
, parent
);
786 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
788 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
789 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
790 bool was_active
= iwp
->src
!= NULL
;
791 if (was_active
== now_active
) {
796 iwp
->src
= g_io_create_watch(iwp
->channel
,
797 G_IO_IN
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
);
798 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
799 g_source_attach(iwp
->src
, NULL
);
801 g_source_destroy(iwp
->src
);
802 g_source_unref(iwp
->src
);
808 static gboolean
io_watch_poll_check(GSource
*source
)
813 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
819 static void io_watch_poll_finalize(GSource
*source
)
821 /* Due to a glib bug, removing the last reference to a source
822 * inside a finalize callback causes recursive locking (and a
823 * deadlock). This is not a problem inside other callbacks,
824 * including dispatch callbacks, so we call io_remove_watch_poll
825 * to remove this source. At this point, iwp->src must
826 * be NULL, or we would leak it.
828 * This would be solved much more elegantly by child sources,
829 * but we support older glib versions that do not have them.
831 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
832 assert(iwp
->src
== NULL
);
835 static GSourceFuncs io_watch_poll_funcs
= {
836 .prepare
= io_watch_poll_prepare
,
837 .check
= io_watch_poll_check
,
838 .dispatch
= io_watch_poll_dispatch
,
839 .finalize
= io_watch_poll_finalize
,
842 /* Can only be used for read */
843 static guint
io_add_watch_poll(GIOChannel
*channel
,
844 IOCanReadHandler
*fd_can_read
,
851 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
852 iwp
->fd_can_read
= fd_can_read
;
853 iwp
->opaque
= user_data
;
854 iwp
->channel
= channel
;
855 iwp
->fd_read
= (GSourceFunc
) fd_read
;
858 tag
= g_source_attach(&iwp
->parent
, NULL
);
859 g_source_unref(&iwp
->parent
);
863 static void io_remove_watch_poll(guint tag
)
868 g_return_if_fail (tag
> 0);
870 source
= g_main_context_find_source_by_id(NULL
, tag
);
871 g_return_if_fail (source
!= NULL
);
873 iwp
= io_watch_poll_from_source(source
);
875 g_source_destroy(iwp
->src
);
876 g_source_unref(iwp
->src
);
879 g_source_destroy(&iwp
->parent
);
882 static void remove_fd_in_watch(CharDriverState
*chr
)
884 if (chr
->fd_in_tag
) {
885 io_remove_watch_poll(chr
->fd_in_tag
);
891 static GIOChannel
*io_channel_from_fd(int fd
)
899 chan
= g_io_channel_unix_new(fd
);
901 g_io_channel_set_encoding(chan
, NULL
, NULL
);
902 g_io_channel_set_buffered(chan
, FALSE
);
908 static GIOChannel
*io_channel_from_socket(int fd
)
917 chan
= g_io_channel_win32_new_socket(fd
);
919 chan
= g_io_channel_unix_new(fd
);
922 g_io_channel_set_encoding(chan
, NULL
, NULL
);
923 g_io_channel_set_buffered(chan
, FALSE
);
928 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
931 GIOStatus status
= G_IO_STATUS_NORMAL
;
933 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
934 gsize bytes_written
= 0;
936 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
937 &bytes_written
, NULL
);
938 offset
+= bytes_written
;
945 case G_IO_STATUS_NORMAL
:
948 case G_IO_STATUS_AGAIN
:
960 typedef struct FDCharDriver
{
961 CharDriverState
*chr
;
962 GIOChannel
*fd_in
, *fd_out
;
966 /* Called with chr_write_lock held. */
967 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
969 FDCharDriver
*s
= chr
->opaque
;
971 return io_channel_send(s
->fd_out
, buf
, len
);
974 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
976 CharDriverState
*chr
= opaque
;
977 FDCharDriver
*s
= chr
->opaque
;
979 uint8_t buf
[READ_BUF_LEN
];
984 if (len
> s
->max_size
) {
991 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
992 len
, &bytes_read
, NULL
);
993 if (status
== G_IO_STATUS_EOF
) {
994 remove_fd_in_watch(chr
);
995 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
998 if (status
== G_IO_STATUS_NORMAL
) {
999 qemu_chr_be_write(chr
, buf
, bytes_read
);
1005 static int fd_chr_read_poll(void *opaque
)
1007 CharDriverState
*chr
= opaque
;
1008 FDCharDriver
*s
= chr
->opaque
;
1010 s
->max_size
= qemu_chr_be_can_write(chr
);
1014 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1016 FDCharDriver
*s
= chr
->opaque
;
1017 return g_io_create_watch(s
->fd_out
, cond
);
1020 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1022 FDCharDriver
*s
= chr
->opaque
;
1024 remove_fd_in_watch(chr
);
1026 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
1031 static void fd_chr_close(struct CharDriverState
*chr
)
1033 FDCharDriver
*s
= chr
->opaque
;
1035 remove_fd_in_watch(chr
);
1037 g_io_channel_unref(s
->fd_in
);
1040 g_io_channel_unref(s
->fd_out
);
1044 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1047 /* open a character device to a unix fd */
1048 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1050 CharDriverState
*chr
;
1053 chr
= qemu_chr_alloc();
1054 s
= g_new0(FDCharDriver
, 1);
1055 s
->fd_in
= io_channel_from_fd(fd_in
);
1056 s
->fd_out
= io_channel_from_fd(fd_out
);
1057 qemu_set_nonblock(fd_out
);
1060 chr
->chr_add_watch
= fd_chr_add_watch
;
1061 chr
->chr_write
= fd_chr_write
;
1062 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1063 chr
->chr_close
= fd_chr_close
;
1068 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
1069 ChardevBackend
*backend
,
1073 ChardevHostdev
*opts
= backend
->u
.pipe
;
1075 char filename_in
[CHR_MAX_FILENAME_SIZE
];
1076 char filename_out
[CHR_MAX_FILENAME_SIZE
];
1077 const char *filename
= opts
->device
;
1079 snprintf(filename_in
, CHR_MAX_FILENAME_SIZE
, "%s.in", filename
);
1080 snprintf(filename_out
, CHR_MAX_FILENAME_SIZE
, "%s.out", filename
);
1081 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1082 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1083 if (fd_in
< 0 || fd_out
< 0) {
1088 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1090 error_setg_file_open(errp
, errno
, filename
);
1094 return qemu_chr_open_fd(fd_in
, fd_out
);
1097 /* init terminal so that we can grab keys */
1098 static struct termios oldtty
;
1099 static int old_fd0_flags
;
1100 static bool stdio_in_use
;
1101 static bool stdio_allow_signal
;
1102 static bool stdio_echo_state
;
1104 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
);
1106 static void term_exit(void)
1108 tcsetattr (0, TCSANOW
, &oldtty
);
1109 fcntl(0, F_SETFL
, old_fd0_flags
);
1112 static void term_stdio_handler(int sig
)
1114 /* restore echo after resume from suspend. */
1115 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
1118 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1122 stdio_echo_state
= echo
;
1125 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1126 |INLCR
|IGNCR
|ICRNL
|IXON
);
1127 tty
.c_oflag
|= OPOST
;
1128 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1129 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1132 tty
.c_cc
[VTIME
] = 0;
1134 if (!stdio_allow_signal
)
1135 tty
.c_lflag
&= ~ISIG
;
1137 tcsetattr (0, TCSANOW
, &tty
);
1140 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1146 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
1147 ChardevBackend
*backend
,
1151 ChardevStdio
*opts
= backend
->u
.stdio
;
1152 CharDriverState
*chr
;
1153 struct sigaction act
;
1155 if (is_daemonized()) {
1156 error_setg(errp
, "cannot use stdio with -daemonize");
1161 error_setg(errp
, "cannot use stdio by multiple character devices");
1165 stdio_in_use
= true;
1166 old_fd0_flags
= fcntl(0, F_GETFL
);
1167 tcgetattr(0, &oldtty
);
1168 qemu_set_nonblock(0);
1171 memset(&act
, 0, sizeof(act
));
1172 act
.sa_handler
= term_stdio_handler
;
1173 sigaction(SIGCONT
, &act
, NULL
);
1175 chr
= qemu_chr_open_fd(0, 1);
1176 chr
->chr_close
= qemu_chr_close_stdio
;
1177 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1178 if (opts
->has_signal
) {
1179 stdio_allow_signal
= opts
->signal
;
1181 qemu_chr_fe_set_echo(chr
, false);
1186 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1187 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1188 || defined(__GLIBC__)
1190 #define HAVE_CHARDEV_SERIAL 1
1191 #define HAVE_CHARDEV_PTY 1
1197 /* Protected by the CharDriverState chr_write_lock. */
1203 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1204 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1206 static gboolean
pty_chr_timer(gpointer opaque
)
1208 struct CharDriverState
*chr
= opaque
;
1209 PtyCharDriver
*s
= chr
->opaque
;
1211 qemu_mutex_lock(&chr
->chr_write_lock
);
1214 if (!s
->connected
) {
1216 pty_chr_update_read_handler_locked(chr
);
1218 qemu_mutex_unlock(&chr
->chr_write_lock
);
1222 /* Called with chr_write_lock held. */
1223 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1225 PtyCharDriver
*s
= chr
->opaque
;
1228 g_source_remove(s
->timer_tag
);
1233 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1235 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1239 /* Called with chr_write_lock held. */
1240 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1242 PtyCharDriver
*s
= chr
->opaque
;
1245 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1246 pfd
.events
= G_IO_OUT
;
1249 if (pfd
.revents
& G_IO_HUP
) {
1250 pty_chr_state(chr
, 0);
1252 pty_chr_state(chr
, 1);
1256 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1258 qemu_mutex_lock(&chr
->chr_write_lock
);
1259 pty_chr_update_read_handler_locked(chr
);
1260 qemu_mutex_unlock(&chr
->chr_write_lock
);
1263 /* Called with chr_write_lock held. */
1264 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1266 PtyCharDriver
*s
= chr
->opaque
;
1268 if (!s
->connected
) {
1269 /* guest sends data, check for (re-)connect */
1270 pty_chr_update_read_handler_locked(chr
);
1271 if (!s
->connected
) {
1275 return io_channel_send(s
->fd
, buf
, len
);
1278 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1280 PtyCharDriver
*s
= chr
->opaque
;
1281 if (!s
->connected
) {
1284 return g_io_create_watch(s
->fd
, cond
);
1287 static int pty_chr_read_poll(void *opaque
)
1289 CharDriverState
*chr
= opaque
;
1290 PtyCharDriver
*s
= chr
->opaque
;
1292 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1293 return s
->read_bytes
;
1296 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1298 CharDriverState
*chr
= opaque
;
1299 PtyCharDriver
*s
= chr
->opaque
;
1301 uint8_t buf
[READ_BUF_LEN
];
1305 if (len
> s
->read_bytes
)
1306 len
= s
->read_bytes
;
1310 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1311 if (status
!= G_IO_STATUS_NORMAL
) {
1312 pty_chr_state(chr
, 0);
1315 pty_chr_state(chr
, 1);
1316 qemu_chr_be_write(chr
, buf
, size
);
1321 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1323 CharDriverState
*chr
= opaque
;
1324 PtyCharDriver
*s
= chr
->opaque
;
1327 qemu_chr_be_generic_open(chr
);
1331 /* Called with chr_write_lock held. */
1332 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1334 PtyCharDriver
*s
= chr
->opaque
;
1338 g_source_remove(s
->open_tag
);
1341 remove_fd_in_watch(chr
);
1343 /* (re-)connect poll interval for idle guests: once per second.
1344 * We check more frequently in case the guests sends data to
1345 * the virtual device linked to our pty. */
1346 pty_chr_rearm_timer(chr
, 1000);
1349 g_source_remove(s
->timer_tag
);
1352 if (!s
->connected
) {
1353 g_assert(s
->open_tag
== 0);
1355 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1357 if (!chr
->fd_in_tag
) {
1358 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1364 static void pty_chr_close(struct CharDriverState
*chr
)
1366 PtyCharDriver
*s
= chr
->opaque
;
1369 qemu_mutex_lock(&chr
->chr_write_lock
);
1370 pty_chr_state(chr
, 0);
1371 fd
= g_io_channel_unix_get_fd(s
->fd
);
1372 g_io_channel_unref(s
->fd
);
1375 g_source_remove(s
->timer_tag
);
1378 qemu_mutex_unlock(&chr
->chr_write_lock
);
1380 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1383 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1384 ChardevBackend
*backend
,
1388 CharDriverState
*chr
;
1390 int master_fd
, slave_fd
;
1391 char pty_name
[PATH_MAX
];
1393 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1394 if (master_fd
< 0) {
1395 error_setg_errno(errp
, errno
, "Failed to create PTY");
1400 qemu_set_nonblock(master_fd
);
1402 chr
= qemu_chr_alloc();
1404 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1405 ret
->pty
= g_strdup(pty_name
);
1406 ret
->has_pty
= true;
1408 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1411 s
= g_new0(PtyCharDriver
, 1);
1413 chr
->chr_write
= pty_chr_write
;
1414 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1415 chr
->chr_close
= pty_chr_close
;
1416 chr
->chr_add_watch
= pty_chr_add_watch
;
1417 chr
->explicit_be_open
= true;
1419 s
->fd
= io_channel_from_fd(master_fd
);
1425 static void tty_serial_init(int fd
, int speed
,
1426 int parity
, int data_bits
, int stop_bits
)
1432 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1433 speed
, parity
, data_bits
, stop_bits
);
1435 tcgetattr (fd
, &tty
);
1437 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1438 speed
= speed
* 10 / 11;
1455 /* Non-Posix values follow. They may be unsupported on some systems. */
1457 check_speed(115200);
1459 check_speed(230400);
1462 check_speed(460800);
1465 check_speed(500000);
1468 check_speed(576000);
1471 check_speed(921600);
1474 check_speed(1000000);
1477 check_speed(1152000);
1480 check_speed(1500000);
1483 check_speed(2000000);
1486 check_speed(2500000);
1489 check_speed(3000000);
1492 check_speed(3500000);
1495 check_speed(4000000);
1500 cfsetispeed(&tty
, spd
);
1501 cfsetospeed(&tty
, spd
);
1503 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1504 |INLCR
|IGNCR
|ICRNL
|IXON
);
1505 tty
.c_oflag
|= OPOST
;
1506 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1507 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1528 tty
.c_cflag
|= PARENB
;
1531 tty
.c_cflag
|= PARENB
| PARODD
;
1535 tty
.c_cflag
|= CSTOPB
;
1537 tcsetattr (fd
, TCSANOW
, &tty
);
1540 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1542 FDCharDriver
*s
= chr
->opaque
;
1545 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1547 QEMUSerialSetParams
*ssp
= arg
;
1548 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1549 ssp
->speed
, ssp
->parity
,
1550 ssp
->data_bits
, ssp
->stop_bits
);
1553 case CHR_IOCTL_SERIAL_SET_BREAK
:
1555 int enable
= *(int *)arg
;
1557 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1561 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1564 int *targ
= (int *)arg
;
1565 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1567 if (sarg
& TIOCM_CTS
)
1568 *targ
|= CHR_TIOCM_CTS
;
1569 if (sarg
& TIOCM_CAR
)
1570 *targ
|= CHR_TIOCM_CAR
;
1571 if (sarg
& TIOCM_DSR
)
1572 *targ
|= CHR_TIOCM_DSR
;
1573 if (sarg
& TIOCM_RI
)
1574 *targ
|= CHR_TIOCM_RI
;
1575 if (sarg
& TIOCM_DTR
)
1576 *targ
|= CHR_TIOCM_DTR
;
1577 if (sarg
& TIOCM_RTS
)
1578 *targ
|= CHR_TIOCM_RTS
;
1581 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1583 int sarg
= *(int *)arg
;
1585 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1586 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1587 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1588 if (sarg
& CHR_TIOCM_CTS
)
1590 if (sarg
& CHR_TIOCM_CAR
)
1592 if (sarg
& CHR_TIOCM_DSR
)
1594 if (sarg
& CHR_TIOCM_RI
)
1596 if (sarg
& CHR_TIOCM_DTR
)
1598 if (sarg
& CHR_TIOCM_RTS
)
1600 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1609 static void qemu_chr_close_tty(CharDriverState
*chr
)
1611 FDCharDriver
*s
= chr
->opaque
;
1615 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1625 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1627 CharDriverState
*chr
;
1629 tty_serial_init(fd
, 115200, 'N', 8, 1);
1630 chr
= qemu_chr_open_fd(fd
, fd
);
1631 chr
->chr_ioctl
= tty_serial_ioctl
;
1632 chr
->chr_close
= qemu_chr_close_tty
;
1635 #endif /* __linux__ || __sun__ */
1637 #if defined(__linux__)
1639 #define HAVE_CHARDEV_PARPORT 1
1644 } ParallelCharDriver
;
1646 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1648 if (s
->mode
!= mode
) {
1650 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1657 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1659 ParallelCharDriver
*drv
= chr
->opaque
;
1664 case CHR_IOCTL_PP_READ_DATA
:
1665 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1667 *(uint8_t *)arg
= b
;
1669 case CHR_IOCTL_PP_WRITE_DATA
:
1670 b
= *(uint8_t *)arg
;
1671 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1674 case CHR_IOCTL_PP_READ_CONTROL
:
1675 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1677 /* Linux gives only the lowest bits, and no way to know data
1678 direction! For better compatibility set the fixed upper
1680 *(uint8_t *)arg
= b
| 0xc0;
1682 case CHR_IOCTL_PP_WRITE_CONTROL
:
1683 b
= *(uint8_t *)arg
;
1684 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1687 case CHR_IOCTL_PP_READ_STATUS
:
1688 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1690 *(uint8_t *)arg
= b
;
1692 case CHR_IOCTL_PP_DATA_DIR
:
1693 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1696 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1697 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1698 struct ParallelIOArg
*parg
= arg
;
1699 int n
= read(fd
, parg
->buffer
, parg
->count
);
1700 if (n
!= parg
->count
) {
1705 case CHR_IOCTL_PP_EPP_READ
:
1706 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1707 struct ParallelIOArg
*parg
= arg
;
1708 int n
= read(fd
, parg
->buffer
, parg
->count
);
1709 if (n
!= parg
->count
) {
1714 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1715 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1716 struct ParallelIOArg
*parg
= arg
;
1717 int n
= write(fd
, parg
->buffer
, parg
->count
);
1718 if (n
!= parg
->count
) {
1723 case CHR_IOCTL_PP_EPP_WRITE
:
1724 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1725 struct ParallelIOArg
*parg
= arg
;
1726 int n
= write(fd
, parg
->buffer
, parg
->count
);
1727 if (n
!= parg
->count
) {
1738 static void pp_close(CharDriverState
*chr
)
1740 ParallelCharDriver
*drv
= chr
->opaque
;
1743 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1744 ioctl(fd
, PPRELEASE
);
1747 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1750 static CharDriverState
*qemu_chr_open_pp_fd(int fd
, Error
**errp
)
1752 CharDriverState
*chr
;
1753 ParallelCharDriver
*drv
;
1755 if (ioctl(fd
, PPCLAIM
) < 0) {
1756 error_setg_errno(errp
, errno
, "not a parallel port");
1761 drv
= g_new0(ParallelCharDriver
, 1);
1763 drv
->mode
= IEEE1284_MODE_COMPAT
;
1765 chr
= qemu_chr_alloc();
1766 chr
->chr_write
= null_chr_write
;
1767 chr
->chr_ioctl
= pp_ioctl
;
1768 chr
->chr_close
= pp_close
;
1773 #endif /* __linux__ */
1775 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1777 #define HAVE_CHARDEV_PARPORT 1
1779 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1781 int fd
= (int)(intptr_t)chr
->opaque
;
1785 case CHR_IOCTL_PP_READ_DATA
:
1786 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1788 *(uint8_t *)arg
= b
;
1790 case CHR_IOCTL_PP_WRITE_DATA
:
1791 b
= *(uint8_t *)arg
;
1792 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1795 case CHR_IOCTL_PP_READ_CONTROL
:
1796 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1798 *(uint8_t *)arg
= b
;
1800 case CHR_IOCTL_PP_WRITE_CONTROL
:
1801 b
= *(uint8_t *)arg
;
1802 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1805 case CHR_IOCTL_PP_READ_STATUS
:
1806 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1808 *(uint8_t *)arg
= b
;
1816 static CharDriverState
*qemu_chr_open_pp_fd(int fd
, Error
**errp
)
1818 CharDriverState
*chr
;
1820 chr
= qemu_chr_alloc();
1821 chr
->opaque
= (void *)(intptr_t)fd
;
1822 chr
->chr_write
= null_chr_write
;
1823 chr
->chr_ioctl
= pp_ioctl
;
1824 chr
->explicit_be_open
= true;
1831 #define HAVE_CHARDEV_SERIAL 1
1835 HANDLE hcom
, hrecv
, hsend
;
1840 /* Protected by the CharDriverState chr_write_lock. */
1846 HANDLE hInputReadyEvent
;
1847 HANDLE hInputDoneEvent
;
1848 HANDLE hInputThread
;
1849 uint8_t win_stdio_buf
;
1850 } WinStdioCharState
;
1852 #define NSENDBUF 2048
1853 #define NRECVBUF 2048
1854 #define MAXCONNECT 1
1855 #define NTIMEOUT 5000
1857 static int win_chr_poll(void *opaque
);
1858 static int win_chr_pipe_poll(void *opaque
);
1860 static void win_chr_close(CharDriverState
*chr
)
1862 WinCharState
*s
= chr
->opaque
;
1865 CloseHandle(s
->hsend
);
1869 CloseHandle(s
->hrecv
);
1873 CloseHandle(s
->hcom
);
1877 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1879 qemu_del_polling_cb(win_chr_poll
, chr
);
1881 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1884 static int win_chr_init(CharDriverState
*chr
, const char *filename
, Error
**errp
)
1886 WinCharState
*s
= chr
->opaque
;
1888 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1893 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1895 error_setg(errp
, "Failed CreateEvent");
1898 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1900 error_setg(errp
, "Failed CreateEvent");
1904 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1905 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1906 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1907 error_setg(errp
, "Failed CreateFile (%lu)", GetLastError());
1912 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1913 error_setg(errp
, "Failed SetupComm");
1917 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1918 size
= sizeof(COMMCONFIG
);
1919 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1920 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1921 CommConfigDialog(filename
, NULL
, &comcfg
);
1923 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1924 error_setg(errp
, "Failed SetCommState");
1928 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1929 error_setg(errp
, "Failed SetCommMask");
1933 cto
.ReadIntervalTimeout
= MAXDWORD
;
1934 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1935 error_setg(errp
, "Failed SetCommTimeouts");
1939 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1940 error_setg(errp
, "Failed ClearCommError");
1943 qemu_add_polling_cb(win_chr_poll
, chr
);
1951 /* Called with chr_write_lock held. */
1952 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1954 WinCharState
*s
= chr
->opaque
;
1955 DWORD len
, ret
, size
, err
;
1958 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1959 s
->osend
.hEvent
= s
->hsend
;
1962 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1964 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1966 err
= GetLastError();
1967 if (err
== ERROR_IO_PENDING
) {
1968 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1986 static int win_chr_read_poll(CharDriverState
*chr
)
1988 WinCharState
*s
= chr
->opaque
;
1990 s
->max_size
= qemu_chr_be_can_write(chr
);
1994 static void win_chr_readfile(CharDriverState
*chr
)
1996 WinCharState
*s
= chr
->opaque
;
1998 uint8_t buf
[READ_BUF_LEN
];
2001 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2002 s
->orecv
.hEvent
= s
->hrecv
;
2003 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2005 err
= GetLastError();
2006 if (err
== ERROR_IO_PENDING
) {
2007 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2012 qemu_chr_be_write(chr
, buf
, size
);
2016 static void win_chr_read(CharDriverState
*chr
)
2018 WinCharState
*s
= chr
->opaque
;
2020 if (s
->len
> s
->max_size
)
2021 s
->len
= s
->max_size
;
2025 win_chr_readfile(chr
);
2028 static int win_chr_poll(void *opaque
)
2030 CharDriverState
*chr
= opaque
;
2031 WinCharState
*s
= chr
->opaque
;
2035 ClearCommError(s
->hcom
, &comerr
, &status
);
2036 if (status
.cbInQue
> 0) {
2037 s
->len
= status
.cbInQue
;
2038 win_chr_read_poll(chr
);
2045 static CharDriverState
*qemu_chr_open_win_path(const char *filename
,
2048 CharDriverState
*chr
;
2051 chr
= qemu_chr_alloc();
2052 s
= g_new0(WinCharState
, 1);
2054 chr
->chr_write
= win_chr_write
;
2055 chr
->chr_close
= win_chr_close
;
2057 if (win_chr_init(chr
, filename
, errp
) < 0) {
2065 static int win_chr_pipe_poll(void *opaque
)
2067 CharDriverState
*chr
= opaque
;
2068 WinCharState
*s
= chr
->opaque
;
2071 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2074 win_chr_read_poll(chr
);
2081 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
,
2084 WinCharState
*s
= chr
->opaque
;
2088 char openname
[CHR_MAX_FILENAME_SIZE
];
2092 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2094 error_setg(errp
, "Failed CreateEvent");
2097 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2099 error_setg(errp
, "Failed CreateEvent");
2103 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2104 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2105 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2107 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2108 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2109 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
2114 ZeroMemory(&ov
, sizeof(ov
));
2115 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2116 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2118 error_setg(errp
, "Failed ConnectNamedPipe");
2122 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2124 error_setg(errp
, "Failed GetOverlappedResult");
2126 CloseHandle(ov
.hEvent
);
2133 CloseHandle(ov
.hEvent
);
2136 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2145 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
2146 ChardevBackend
*backend
,
2150 ChardevHostdev
*opts
= backend
->u
.pipe
;
2151 const char *filename
= opts
->device
;
2152 CharDriverState
*chr
;
2155 chr
= qemu_chr_alloc();
2156 s
= g_new0(WinCharState
, 1);
2158 chr
->chr_write
= win_chr_write
;
2159 chr
->chr_close
= win_chr_close
;
2161 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
2169 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2171 CharDriverState
*chr
;
2174 chr
= qemu_chr_alloc();
2175 s
= g_new0(WinCharState
, 1);
2178 chr
->chr_write
= win_chr_write
;
2182 static CharDriverState
*qemu_chr_open_win_con(const char *id
,
2183 ChardevBackend
*backend
,
2187 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2190 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2192 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2199 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2209 static void win_stdio_wait_func(void *opaque
)
2211 CharDriverState
*chr
= opaque
;
2212 WinStdioCharState
*stdio
= chr
->opaque
;
2213 INPUT_RECORD buf
[4];
2218 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2221 /* Avoid error storm */
2222 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2226 for (i
= 0; i
< dwSize
; i
++) {
2227 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2229 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2231 if (kev
->uChar
.AsciiChar
!= 0) {
2232 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2233 if (qemu_chr_be_can_write(chr
)) {
2234 uint8_t c
= kev
->uChar
.AsciiChar
;
2235 qemu_chr_be_write(chr
, &c
, 1);
2243 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2245 CharDriverState
*chr
= param
;
2246 WinStdioCharState
*stdio
= chr
->opaque
;
2252 /* Wait for one byte */
2253 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2255 /* Exit in case of error, continue if nothing read */
2263 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2264 if (stdio
->win_stdio_buf
== '\r') {
2268 /* Signal the main thread and wait until the byte was eaten */
2269 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2272 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2278 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2282 static void win_stdio_thread_wait_func(void *opaque
)
2284 CharDriverState
*chr
= opaque
;
2285 WinStdioCharState
*stdio
= chr
->opaque
;
2287 if (qemu_chr_be_can_write(chr
)) {
2288 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2291 SetEvent(stdio
->hInputDoneEvent
);
2294 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2296 WinStdioCharState
*stdio
= chr
->opaque
;
2299 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2302 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2304 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2308 static void win_stdio_close(CharDriverState
*chr
)
2310 WinStdioCharState
*stdio
= chr
->opaque
;
2312 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2313 CloseHandle(stdio
->hInputReadyEvent
);
2315 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2316 CloseHandle(stdio
->hInputDoneEvent
);
2318 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2319 TerminateThread(stdio
->hInputThread
, 0);
2322 g_free(chr
->opaque
);
2326 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
2327 ChardevBackend
*backend
,
2331 CharDriverState
*chr
;
2332 WinStdioCharState
*stdio
;
2336 chr
= qemu_chr_alloc();
2337 stdio
= g_new0(WinStdioCharState
, 1);
2339 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2340 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2341 error_setg(errp
, "cannot open stdio: invalid handle");
2345 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2347 chr
->opaque
= stdio
;
2348 chr
->chr_write
= win_stdio_write
;
2349 chr
->chr_close
= win_stdio_close
;
2352 if (qemu_add_wait_object(stdio
->hStdIn
,
2353 win_stdio_wait_func
, chr
)) {
2354 error_setg(errp
, "qemu_add_wait_object: failed");
2360 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2361 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2362 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2363 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2364 error_setg(errp
, "cannot create event");
2367 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2368 win_stdio_thread_wait_func
, chr
)) {
2369 error_setg(errp
, "qemu_add_wait_object: failed");
2372 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2375 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
2376 error_setg(errp
, "cannot create stdio thread");
2381 dwMode
|= ENABLE_LINE_INPUT
;
2384 /* set the terminal in raw mode */
2385 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2386 dwMode
|= ENABLE_PROCESSED_INPUT
;
2389 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2391 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2392 qemu_chr_fe_set_echo(chr
, false);
2397 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2399 CloseHandle(stdio
->hInputReadyEvent
);
2400 CloseHandle(stdio
->hInputDoneEvent
);
2402 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2405 #endif /* !_WIN32 */
2408 /***********************************************************/
2409 /* UDP Net console */
2414 uint8_t buf
[READ_BUF_LEN
];
2420 /* Called with chr_write_lock held. */
2421 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2423 NetCharDriver
*s
= chr
->opaque
;
2424 gsize bytes_written
;
2427 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2428 if (status
== G_IO_STATUS_EOF
) {
2430 } else if (status
!= G_IO_STATUS_NORMAL
) {
2434 return bytes_written
;
2437 static int udp_chr_read_poll(void *opaque
)
2439 CharDriverState
*chr
= opaque
;
2440 NetCharDriver
*s
= chr
->opaque
;
2442 s
->max_size
= qemu_chr_be_can_write(chr
);
2444 /* If there were any stray characters in the queue process them
2447 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2448 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2450 s
->max_size
= qemu_chr_be_can_write(chr
);
2455 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2457 CharDriverState
*chr
= opaque
;
2458 NetCharDriver
*s
= chr
->opaque
;
2459 gsize bytes_read
= 0;
2462 if (s
->max_size
== 0) {
2465 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2467 s
->bufcnt
= bytes_read
;
2468 s
->bufptr
= s
->bufcnt
;
2469 if (status
!= G_IO_STATUS_NORMAL
) {
2470 remove_fd_in_watch(chr
);
2475 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2476 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2478 s
->max_size
= qemu_chr_be_can_write(chr
);
2484 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2486 NetCharDriver
*s
= chr
->opaque
;
2488 remove_fd_in_watch(chr
);
2490 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2495 static void udp_chr_close(CharDriverState
*chr
)
2497 NetCharDriver
*s
= chr
->opaque
;
2499 remove_fd_in_watch(chr
);
2501 g_io_channel_unref(s
->chan
);
2505 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2508 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2510 CharDriverState
*chr
= NULL
;
2511 NetCharDriver
*s
= NULL
;
2513 chr
= qemu_chr_alloc();
2514 s
= g_new0(NetCharDriver
, 1);
2517 s
->chan
= io_channel_from_socket(s
->fd
);
2521 chr
->chr_write
= udp_chr_write
;
2522 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2523 chr
->chr_close
= udp_chr_close
;
2524 /* be isn't opened until we get a connection */
2525 chr
->explicit_be_open
= true;
2529 /***********************************************************/
2530 /* TCP Net console */
2534 GIOChannel
*chan
, *listen_chan
;
2543 int read_msgfds_num
;
2545 int write_msgfds_num
;
2547 SocketAddress
*addr
;
2551 guint reconnect_timer
;
2552 int64_t reconnect_time
;
2553 bool connect_err_reported
;
2556 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2558 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2560 TCPCharDriver
*s
= chr
->opaque
;
2561 assert(s
->connected
== 0);
2562 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2563 socket_reconnect_timeout
, chr
);
2566 static void check_report_connect_error(CharDriverState
*chr
,
2569 TCPCharDriver
*s
= chr
->opaque
;
2571 if (!s
->connect_err_reported
) {
2572 error_report("Unable to connect character device %s: %s",
2573 chr
->label
, error_get_pretty(err
));
2574 s
->connect_err_reported
= true;
2576 qemu_chr_socket_restart_timer(chr
);
2579 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2582 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2584 TCPCharDriver
*s
= chr
->opaque
;
2589 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2590 char control
[CMSG_SPACE(fd_size
)];
2591 struct cmsghdr
*cmsg
;
2593 memset(&msgh
, 0, sizeof(msgh
));
2594 memset(control
, 0, sizeof(control
));
2596 /* set the payload */
2597 iov
.iov_base
= (uint8_t *) buf
;
2600 msgh
.msg_iov
= &iov
;
2601 msgh
.msg_iovlen
= 1;
2603 msgh
.msg_control
= control
;
2604 msgh
.msg_controllen
= sizeof(control
);
2606 cmsg
= CMSG_FIRSTHDR(&msgh
);
2608 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2609 cmsg
->cmsg_level
= SOL_SOCKET
;
2610 cmsg
->cmsg_type
= SCM_RIGHTS
;
2611 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2614 r
= sendmsg(s
->fd
, &msgh
, 0);
2615 } while (r
< 0 && errno
== EINTR
);
2617 /* free the written msgfds, no matter what */
2618 if (s
->write_msgfds_num
) {
2619 g_free(s
->write_msgfds
);
2620 s
->write_msgfds
= 0;
2621 s
->write_msgfds_num
= 0;
2628 /* Called with chr_write_lock held. */
2629 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2631 TCPCharDriver
*s
= chr
->opaque
;
2634 if (s
->is_unix
&& s
->write_msgfds_num
) {
2635 return unix_send_msgfds(chr
, buf
, len
);
2639 return io_channel_send(s
->chan
, buf
, len
);
2642 /* XXX: indicate an error ? */
2647 static int tcp_chr_read_poll(void *opaque
)
2649 CharDriverState
*chr
= opaque
;
2650 TCPCharDriver
*s
= chr
->opaque
;
2653 s
->max_size
= qemu_chr_be_can_write(chr
);
2658 #define IAC_BREAK 243
2659 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2661 uint8_t *buf
, int *size
)
2663 /* Handle any telnet client's basic IAC options to satisfy char by
2664 * char mode with no echo. All IAC options will be removed from
2665 * the buf and the do_telnetopt variable will be used to track the
2666 * state of the width of the IAC information.
2668 * IAC commands come in sets of 3 bytes with the exception of the
2669 * "IAC BREAK" command and the double IAC.
2675 for (i
= 0; i
< *size
; i
++) {
2676 if (s
->do_telnetopt
> 1) {
2677 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2678 /* Double IAC means send an IAC */
2682 s
->do_telnetopt
= 1;
2684 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2685 /* Handle IAC break commands by sending a serial break */
2686 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2691 if (s
->do_telnetopt
>= 4) {
2692 s
->do_telnetopt
= 1;
2695 if ((unsigned char)buf
[i
] == IAC
) {
2696 s
->do_telnetopt
= 2;
2707 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2709 TCPCharDriver
*s
= chr
->opaque
;
2710 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2712 assert(num
<= TCP_MAX_FDS
);
2717 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2719 /* Close unused fds */
2720 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2721 close(s
->read_msgfds
[i
]);
2724 g_free(s
->read_msgfds
);
2726 s
->read_msgfds_num
= 0;
2732 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2734 TCPCharDriver
*s
= chr
->opaque
;
2736 /* clear old pending fd array */
2737 g_free(s
->write_msgfds
);
2740 s
->write_msgfds
= g_new(int, num
);
2741 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2744 s
->write_msgfds_num
= num
;
2750 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2752 TCPCharDriver
*s
= chr
->opaque
;
2753 struct cmsghdr
*cmsg
;
2755 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2758 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2759 cmsg
->cmsg_level
!= SOL_SOCKET
||
2760 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2764 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2770 /* close and clean read_msgfds */
2771 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2772 close(s
->read_msgfds
[i
]);
2775 if (s
->read_msgfds_num
) {
2776 g_free(s
->read_msgfds
);
2779 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2780 s
->read_msgfds
= g_malloc(fd_size
);
2781 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2783 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2784 int fd
= s
->read_msgfds
[i
];
2789 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2792 #ifndef MSG_CMSG_CLOEXEC
2793 qemu_set_cloexec(fd
);
2799 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2801 TCPCharDriver
*s
= chr
->opaque
;
2802 struct msghdr msg
= { NULL
, };
2803 struct iovec iov
[1];
2805 struct cmsghdr cmsg
;
2806 char control
[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS
)];
2811 iov
[0].iov_base
= buf
;
2812 iov
[0].iov_len
= len
;
2816 msg
.msg_control
= &msg_control
;
2817 msg
.msg_controllen
= sizeof(msg_control
);
2819 #ifdef MSG_CMSG_CLOEXEC
2820 flags
|= MSG_CMSG_CLOEXEC
;
2823 ret
= recvmsg(s
->fd
, &msg
, flags
);
2824 } while (ret
== -1 && errno
== EINTR
);
2826 if (ret
> 0 && s
->is_unix
) {
2827 unix_process_msgfd(chr
, &msg
);
2833 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2835 TCPCharDriver
*s
= chr
->opaque
;
2839 ret
= qemu_recv(s
->fd
, buf
, len
, 0);
2840 } while (ret
== -1 && socket_error() == EINTR
);
2846 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2848 TCPCharDriver
*s
= chr
->opaque
;
2849 return g_io_create_watch(s
->chan
, cond
);
2852 static void tcp_chr_disconnect(CharDriverState
*chr
)
2854 TCPCharDriver
*s
= chr
->opaque
;
2857 if (s
->listen_chan
) {
2858 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2859 tcp_chr_accept
, chr
);
2861 remove_fd_in_watch(chr
);
2862 g_io_channel_unref(s
->chan
);
2866 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2867 "disconnected:", s
->addr
, s
->is_listen
, s
->is_telnet
);
2868 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2869 if (s
->reconnect_time
) {
2870 qemu_chr_socket_restart_timer(chr
);
2874 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2876 CharDriverState
*chr
= opaque
;
2877 TCPCharDriver
*s
= chr
->opaque
;
2878 uint8_t buf
[READ_BUF_LEN
];
2881 if (!s
->connected
|| s
->max_size
<= 0) {
2885 if (len
> s
->max_size
)
2887 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2890 socket_error() != EAGAIN
&& socket_error() != EWOULDBLOCK
)) {
2891 /* connection closed */
2892 tcp_chr_disconnect(chr
);
2893 } else if (size
> 0) {
2894 if (s
->do_telnetopt
)
2895 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2897 qemu_chr_be_write(chr
, buf
, size
);
2903 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2905 TCPCharDriver
*s
= chr
->opaque
;
2908 if (!s
->connected
) {
2912 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2914 /* connection closed */
2915 tcp_chr_disconnect(chr
);
2922 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2924 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2927 chr
->avail_connections
= 1;
2934 static void tcp_chr_connect(void *opaque
)
2936 CharDriverState
*chr
= opaque
;
2937 TCPCharDriver
*s
= chr
->opaque
;
2938 struct sockaddr_storage ss
, ps
;
2939 socklen_t ss_len
= sizeof(ss
), ps_len
= sizeof(ps
);
2941 memset(&ss
, 0, ss_len
);
2942 if (getsockname(s
->fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2943 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2944 "Error in getsockname: %s\n", strerror(errno
));
2945 } else if (getpeername(s
->fd
, (struct sockaddr
*) &ps
, &ps_len
) != 0) {
2946 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2947 "Error in getpeername: %s\n", strerror(errno
));
2949 sockaddr_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2950 &ss
, ss_len
, &ps
, ps_len
,
2951 s
->is_listen
, s
->is_telnet
);
2956 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2959 qemu_chr_be_generic_open(chr
);
2962 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2964 TCPCharDriver
*s
= chr
->opaque
;
2966 remove_fd_in_watch(chr
);
2968 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2973 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2974 static void tcp_chr_telnet_init(int fd
)
2977 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2978 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2979 send(fd
, (char *)buf
, 3, 0);
2980 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2981 send(fd
, (char *)buf
, 3, 0);
2982 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2983 send(fd
, (char *)buf
, 3, 0);
2984 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2985 send(fd
, (char *)buf
, 3, 0);
2988 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2990 TCPCharDriver
*s
= chr
->opaque
;
2994 qemu_set_nonblock(fd
);
2996 socket_set_nodelay(fd
);
2998 s
->chan
= io_channel_from_socket(fd
);
2999 if (s
->listen_tag
) {
3000 g_source_remove(s
->listen_tag
);
3003 tcp_chr_connect(chr
);
3008 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
3010 CharDriverState
*chr
= opaque
;
3011 TCPCharDriver
*s
= chr
->opaque
;
3012 struct sockaddr_in saddr
;
3014 struct sockaddr_un uaddr
;
3016 struct sockaddr
*addr
;
3023 len
= sizeof(uaddr
);
3024 addr
= (struct sockaddr
*)&uaddr
;
3028 len
= sizeof(saddr
);
3029 addr
= (struct sockaddr
*)&saddr
;
3031 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
3032 if (fd
< 0 && errno
!= EINTR
) {
3035 } else if (fd
>= 0) {
3036 if (s
->do_telnetopt
)
3037 tcp_chr_telnet_init(fd
);
3041 if (tcp_chr_add_client(chr
, fd
) < 0)
3047 static void tcp_chr_close(CharDriverState
*chr
)
3049 TCPCharDriver
*s
= chr
->opaque
;
3052 if (s
->reconnect_timer
) {
3053 g_source_remove(s
->reconnect_timer
);
3054 s
->reconnect_timer
= 0;
3056 qapi_free_SocketAddress(s
->addr
);
3058 remove_fd_in_watch(chr
);
3060 g_io_channel_unref(s
->chan
);
3064 if (s
->listen_fd
>= 0) {
3065 if (s
->listen_tag
) {
3066 g_source_remove(s
->listen_tag
);
3069 if (s
->listen_chan
) {
3070 g_io_channel_unref(s
->listen_chan
);
3072 closesocket(s
->listen_fd
);
3074 if (s
->read_msgfds_num
) {
3075 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3076 close(s
->read_msgfds
[i
]);
3078 g_free(s
->read_msgfds
);
3080 if (s
->write_msgfds_num
) {
3081 g_free(s
->write_msgfds
);
3084 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3087 static void qemu_chr_finish_socket_connection(CharDriverState
*chr
, int fd
)
3089 TCPCharDriver
*s
= chr
->opaque
;
3093 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
3094 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
3095 tcp_chr_accept
, chr
);
3099 socket_set_nodelay(fd
);
3100 s
->chan
= io_channel_from_socket(s
->fd
);
3101 tcp_chr_connect(chr
);
3105 static void qemu_chr_socket_connected(int fd
, Error
*err
, void *opaque
)
3107 CharDriverState
*chr
= opaque
;
3108 TCPCharDriver
*s
= chr
->opaque
;
3111 check_report_connect_error(chr
, err
);
3115 s
->connect_err_reported
= false;
3116 qemu_chr_finish_socket_connection(chr
, fd
);
3119 static bool qemu_chr_open_socket_fd(CharDriverState
*chr
, Error
**errp
)
3121 TCPCharDriver
*s
= chr
->opaque
;
3125 fd
= socket_listen(s
->addr
, errp
);
3126 } else if (s
->reconnect_time
) {
3127 fd
= socket_connect(s
->addr
, errp
, qemu_chr_socket_connected
, chr
);
3130 fd
= socket_connect(s
->addr
, errp
, NULL
, NULL
);
3136 qemu_chr_finish_socket_connection(chr
, fd
);
3140 /*********************************************************/
3141 /* Ring buffer chardev */
3148 } RingBufCharDriver
;
3150 static size_t ringbuf_count(const CharDriverState
*chr
)
3152 const RingBufCharDriver
*d
= chr
->opaque
;
3154 return d
->prod
- d
->cons
;
3157 /* Called with chr_write_lock held. */
3158 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3160 RingBufCharDriver
*d
= chr
->opaque
;
3163 if (!buf
|| (len
< 0)) {
3167 for (i
= 0; i
< len
; i
++ ) {
3168 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3169 if (d
->prod
- d
->cons
> d
->size
) {
3170 d
->cons
= d
->prod
- d
->size
;
3177 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3179 RingBufCharDriver
*d
= chr
->opaque
;
3182 qemu_mutex_lock(&chr
->chr_write_lock
);
3183 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3184 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3186 qemu_mutex_unlock(&chr
->chr_write_lock
);
3191 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3193 RingBufCharDriver
*d
= chr
->opaque
;
3200 static CharDriverState
*qemu_chr_open_ringbuf(const char *id
,
3201 ChardevBackend
*backend
,
3205 ChardevRingbuf
*opts
= backend
->u
.ringbuf
;
3206 CharDriverState
*chr
;
3207 RingBufCharDriver
*d
;
3209 chr
= qemu_chr_alloc();
3210 d
= g_malloc(sizeof(*d
));
3212 d
->size
= opts
->has_size
? opts
->size
: 65536;
3214 /* The size must be power of 2 */
3215 if (d
->size
& (d
->size
- 1)) {
3216 error_setg(errp
, "size of ringbuf chardev must be power of two");
3222 d
->cbuf
= g_malloc0(d
->size
);
3225 chr
->chr_write
= ringbuf_chr_write
;
3226 chr
->chr_close
= ringbuf_chr_close
;
3236 bool chr_is_ringbuf(const CharDriverState
*chr
)
3238 return chr
->chr_write
== ringbuf_chr_write
;
3241 void qmp_ringbuf_write(const char *device
, const char *data
,
3242 bool has_format
, enum DataFormat format
,
3245 CharDriverState
*chr
;
3246 const uint8_t *write_data
;
3250 chr
= qemu_chr_find(device
);
3252 error_setg(errp
, "Device '%s' not found", device
);
3256 if (!chr_is_ringbuf(chr
)) {
3257 error_setg(errp
,"%s is not a ringbuf device", device
);
3261 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3262 write_data
= g_base64_decode(data
, &write_count
);
3264 write_data
= (uint8_t *)data
;
3265 write_count
= strlen(data
);
3268 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3270 if (write_data
!= (uint8_t *)data
) {
3271 g_free((void *)write_data
);
3275 error_setg(errp
, "Failed to write to device %s", device
);
3280 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3281 bool has_format
, enum DataFormat format
,
3284 CharDriverState
*chr
;
3289 chr
= qemu_chr_find(device
);
3291 error_setg(errp
, "Device '%s' not found", device
);
3295 if (!chr_is_ringbuf(chr
)) {
3296 error_setg(errp
,"%s is not a ringbuf device", device
);
3301 error_setg(errp
, "size must be greater than zero");
3305 count
= ringbuf_count(chr
);
3306 size
= size
> count
? count
: size
;
3307 read_data
= g_malloc(size
+ 1);
3309 ringbuf_chr_read(chr
, read_data
, size
);
3311 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3312 data
= g_base64_encode(read_data
, size
);
3316 * FIXME should read only complete, valid UTF-8 characters up
3317 * to @size bytes. Invalid sequences should be replaced by a
3318 * suitable replacement character. Except when (and only
3319 * when) ring buffer lost characters since last read, initial
3320 * continuation characters should be dropped.
3322 read_data
[size
] = 0;
3323 data
= (char *)read_data
;
3329 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3331 char host
[65], port
[33], width
[8], height
[8];
3335 Error
*local_err
= NULL
;
3337 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3339 error_report_err(local_err
);
3343 if (strstart(filename
, "mon:", &p
)) {
3345 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3346 if (strcmp(filename
, "stdio") == 0) {
3347 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3348 * but pass it to the guest. Handle this only for compat syntax,
3349 * for -chardev syntax we have special option for this.
3350 * This is what -nographic did, redirecting+muxing serial+monitor
3351 * to stdio causing Ctrl+C to be passed to guest. */
3352 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3356 if (strcmp(filename
, "null") == 0 ||
3357 strcmp(filename
, "pty") == 0 ||
3358 strcmp(filename
, "msmouse") == 0 ||
3359 strcmp(filename
, "braille") == 0 ||
3360 strcmp(filename
, "testdev") == 0 ||
3361 strcmp(filename
, "stdio") == 0) {
3362 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3365 if (strstart(filename
, "vc", &p
)) {
3366 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3368 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3370 qemu_opt_set(opts
, "width", width
, &error_abort
);
3371 qemu_opt_set(opts
, "height", height
, &error_abort
);
3372 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3374 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3375 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3382 if (strcmp(filename
, "con:") == 0) {
3383 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3386 if (strstart(filename
, "COM", NULL
)) {
3387 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3388 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3391 if (strstart(filename
, "file:", &p
)) {
3392 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3393 qemu_opt_set(opts
, "path", p
, &error_abort
);
3396 if (strstart(filename
, "pipe:", &p
)) {
3397 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3398 qemu_opt_set(opts
, "path", p
, &error_abort
);
3401 if (strstart(filename
, "tcp:", &p
) ||
3402 strstart(filename
, "telnet:", &p
)) {
3403 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3405 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3408 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3409 qemu_opt_set(opts
, "host", host
, &error_abort
);
3410 qemu_opt_set(opts
, "port", port
, &error_abort
);
3411 if (p
[pos
] == ',') {
3412 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3414 error_report_err(local_err
);
3418 if (strstart(filename
, "telnet:", &p
))
3419 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3422 if (strstart(filename
, "udp:", &p
)) {
3423 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3424 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3426 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3430 qemu_opt_set(opts
, "host", host
, &error_abort
);
3431 qemu_opt_set(opts
, "port", port
, &error_abort
);
3432 if (p
[pos
] == '@') {
3434 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3436 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3440 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3441 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3445 if (strstart(filename
, "unix:", &p
)) {
3446 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3447 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3449 error_report_err(local_err
);
3454 if (strstart(filename
, "/dev/parport", NULL
) ||
3455 strstart(filename
, "/dev/ppi", NULL
)) {
3456 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3457 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3460 if (strstart(filename
, "/dev/", NULL
)) {
3461 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3462 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3467 qemu_opts_del(opts
);
3471 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3474 const char *path
= qemu_opt_get(opts
, "path");
3477 error_setg(errp
, "chardev: file: no filename given");
3480 backend
->u
.file
= g_new0(ChardevFile
, 1);
3481 backend
->u
.file
->out
= g_strdup(path
);
3484 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3487 backend
->u
.stdio
= g_new0(ChardevStdio
, 1);
3488 backend
->u
.stdio
->has_signal
= true;
3489 backend
->u
.stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3492 #ifdef HAVE_CHARDEV_SERIAL
3493 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3496 const char *device
= qemu_opt_get(opts
, "path");
3498 if (device
== NULL
) {
3499 error_setg(errp
, "chardev: serial/tty: no device path given");
3502 backend
->u
.serial
= g_new0(ChardevHostdev
, 1);
3503 backend
->u
.serial
->device
= g_strdup(device
);
3507 #ifdef HAVE_CHARDEV_PARPORT
3508 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3511 const char *device
= qemu_opt_get(opts
, "path");
3513 if (device
== NULL
) {
3514 error_setg(errp
, "chardev: parallel: no device path given");
3517 backend
->u
.parallel
= g_new0(ChardevHostdev
, 1);
3518 backend
->u
.parallel
->device
= g_strdup(device
);
3522 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3525 const char *device
= qemu_opt_get(opts
, "path");
3527 if (device
== NULL
) {
3528 error_setg(errp
, "chardev: pipe: no device path given");
3531 backend
->u
.pipe
= g_new0(ChardevHostdev
, 1);
3532 backend
->u
.pipe
->device
= g_strdup(device
);
3535 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3540 backend
->u
.ringbuf
= g_new0(ChardevRingbuf
, 1);
3542 val
= qemu_opt_get_size(opts
, "size", 0);
3544 backend
->u
.ringbuf
->has_size
= true;
3545 backend
->u
.ringbuf
->size
= val
;
3549 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3552 const char *chardev
= qemu_opt_get(opts
, "chardev");
3554 if (chardev
== NULL
) {
3555 error_setg(errp
, "chardev: mux: no chardev given");
3558 backend
->u
.mux
= g_new0(ChardevMux
, 1);
3559 backend
->u
.mux
->chardev
= g_strdup(chardev
);
3562 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3565 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3566 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3567 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3568 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3569 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3570 const char *path
= qemu_opt_get(opts
, "path");
3571 const char *host
= qemu_opt_get(opts
, "host");
3572 const char *port
= qemu_opt_get(opts
, "port");
3573 SocketAddress
*addr
;
3577 error_setg(errp
, "chardev: socket: no host given");
3581 error_setg(errp
, "chardev: socket: no port given");
3586 backend
->u
.socket
= g_new0(ChardevSocket
, 1);
3588 backend
->u
.socket
->has_nodelay
= true;
3589 backend
->u
.socket
->nodelay
= do_nodelay
;
3590 backend
->u
.socket
->has_server
= true;
3591 backend
->u
.socket
->server
= is_listen
;
3592 backend
->u
.socket
->has_telnet
= true;
3593 backend
->u
.socket
->telnet
= is_telnet
;
3594 backend
->u
.socket
->has_wait
= true;
3595 backend
->u
.socket
->wait
= is_waitconnect
;
3596 backend
->u
.socket
->has_reconnect
= true;
3597 backend
->u
.socket
->reconnect
= reconnect
;
3599 addr
= g_new0(SocketAddress
, 1);
3601 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
3602 addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
3603 addr
->u
.q_unix
->path
= g_strdup(path
);
3605 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3606 addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
3607 addr
->u
.inet
->host
= g_strdup(host
);
3608 addr
->u
.inet
->port
= g_strdup(port
);
3609 addr
->u
.inet
->has_to
= qemu_opt_get(opts
, "to");
3610 addr
->u
.inet
->to
= qemu_opt_get_number(opts
, "to", 0);
3611 addr
->u
.inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3612 addr
->u
.inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3613 addr
->u
.inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3614 addr
->u
.inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3616 backend
->u
.socket
->addr
= addr
;
3619 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
3622 const char *host
= qemu_opt_get(opts
, "host");
3623 const char *port
= qemu_opt_get(opts
, "port");
3624 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
3625 const char *localport
= qemu_opt_get(opts
, "localport");
3626 bool has_local
= false;
3627 SocketAddress
*addr
;
3629 if (host
== NULL
|| strlen(host
) == 0) {
3632 if (port
== NULL
|| strlen(port
) == 0) {
3633 error_setg(errp
, "chardev: udp: remote port not specified");
3636 if (localport
== NULL
|| strlen(localport
) == 0) {
3641 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
3647 backend
->u
.udp
= g_new0(ChardevUdp
, 1);
3649 addr
= g_new0(SocketAddress
, 1);
3650 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3651 addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
3652 addr
->u
.inet
->host
= g_strdup(host
);
3653 addr
->u
.inet
->port
= g_strdup(port
);
3654 addr
->u
.inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3655 addr
->u
.inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3656 addr
->u
.inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3657 addr
->u
.inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3658 backend
->u
.udp
->remote
= addr
;
3661 backend
->u
.udp
->has_local
= true;
3662 addr
= g_new0(SocketAddress
, 1);
3663 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3664 addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
3665 addr
->u
.inet
->host
= g_strdup(localaddr
);
3666 addr
->u
.inet
->port
= g_strdup(localport
);
3667 backend
->u
.udp
->local
= addr
;
3671 typedef struct CharDriver
{
3673 ChardevBackendKind kind
;
3674 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3675 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3676 ChardevReturn
*ret
, Error
**errp
);
3679 static GSList
*backends
;
3681 void register_char_driver(const char *name
, ChardevBackendKind kind
,
3682 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
),
3683 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3684 ChardevReturn
*ret
, Error
**errp
))
3688 s
= g_malloc0(sizeof(*s
));
3689 s
->name
= g_strdup(name
);
3694 backends
= g_slist_append(backends
, s
);
3697 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3698 void (*init
)(struct CharDriverState
*s
),
3701 Error
*local_err
= NULL
;
3703 CharDriverState
*chr
;
3705 ChardevReturn
*ret
= NULL
;
3706 ChardevBackend
*backend
;
3707 const char *id
= qemu_opts_id(opts
);
3711 error_setg(errp
, "chardev: no id specified");
3715 if (qemu_opt_get(opts
, "backend") == NULL
) {
3716 error_setg(errp
, "chardev: \"%s\" missing backend",
3717 qemu_opts_id(opts
));
3720 for (i
= backends
; i
; i
= i
->next
) {
3723 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3728 error_setg(errp
, "chardev: backend \"%s\" not found",
3729 qemu_opt_get(opts
, "backend"));
3733 backend
= g_new0(ChardevBackend
, 1);
3735 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3736 bid
= g_strdup_printf("%s-base", id
);
3740 backend
->type
= cd
->kind
;
3742 cd
->parse(opts
, backend
, &local_err
);
3744 error_propagate(errp
, local_err
);
3748 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3754 qapi_free_ChardevBackend(backend
);
3755 qapi_free_ChardevReturn(ret
);
3756 backend
= g_new0(ChardevBackend
, 1);
3757 backend
->u
.mux
= g_new0(ChardevMux
, 1);
3758 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
3759 backend
->u
.mux
->chardev
= g_strdup(bid
);
3760 ret
= qmp_chardev_add(id
, backend
, errp
);
3762 chr
= qemu_chr_find(bid
);
3763 qemu_chr_delete(chr
);
3769 chr
= qemu_chr_find(id
);
3773 qapi_free_ChardevBackend(backend
);
3774 qapi_free_ChardevReturn(ret
);
3779 qemu_opts_del(opts
);
3783 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3786 CharDriverState
*chr
;
3790 if (strstart(filename
, "chardev:", &p
)) {
3791 return qemu_chr_find(p
);
3794 opts
= qemu_chr_parse_compat(label
, filename
);
3798 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3800 error_report_err(err
);
3802 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3803 qemu_chr_fe_claim_no_fail(chr
);
3804 monitor_init(chr
, MONITOR_USE_READLINE
);
3809 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3811 if (chr
->chr_set_echo
) {
3812 chr
->chr_set_echo(chr
, echo
);
3816 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3818 if (chr
->fe_open
== fe_open
) {
3821 chr
->fe_open
= fe_open
;
3822 if (chr
->chr_set_fe_open
) {
3823 chr
->chr_set_fe_open(chr
, fe_open
);
3827 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3829 if (chr
->chr_fe_event
) {
3830 chr
->chr_fe_event(chr
, event
);
3834 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3835 GIOFunc func
, void *user_data
)
3840 if (s
->chr_add_watch
== NULL
) {
3844 src
= s
->chr_add_watch(s
, cond
);
3849 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3850 tag
= g_source_attach(src
, NULL
);
3851 g_source_unref(src
);
3856 int qemu_chr_fe_claim(CharDriverState
*s
)
3858 if (s
->avail_connections
< 1) {
3861 s
->avail_connections
--;
3865 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3867 if (qemu_chr_fe_claim(s
) != 0) {
3868 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3869 __func__
, s
->label
);
3874 void qemu_chr_fe_release(CharDriverState
*s
)
3876 s
->avail_connections
++;
3879 void qemu_chr_free(CharDriverState
*chr
)
3881 if (chr
->chr_close
) {
3882 chr
->chr_close(chr
);
3884 g_free(chr
->filename
);
3886 qemu_opts_del(chr
->opts
);
3890 void qemu_chr_delete(CharDriverState
*chr
)
3892 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3896 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3898 ChardevInfoList
*chr_list
= NULL
;
3899 CharDriverState
*chr
;
3901 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3902 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3903 info
->value
= g_malloc0(sizeof(*info
->value
));
3904 info
->value
->label
= g_strdup(chr
->label
);
3905 info
->value
->filename
= g_strdup(chr
->filename
);
3906 info
->value
->frontend_open
= chr
->fe_open
;
3908 info
->next
= chr_list
;
3915 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3917 ChardevBackendInfoList
*backend_list
= NULL
;
3918 CharDriver
*c
= NULL
;
3921 for (i
= backends
; i
; i
= i
->next
) {
3922 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3924 info
->value
= g_malloc0(sizeof(*info
->value
));
3925 info
->value
->name
= g_strdup(c
->name
);
3927 info
->next
= backend_list
;
3928 backend_list
= info
;
3931 return backend_list
;
3934 CharDriverState
*qemu_chr_find(const char *name
)
3936 CharDriverState
*chr
;
3938 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3939 if (strcmp(chr
->label
, name
) != 0)
3946 /* Get a character (serial) device interface. */
3947 CharDriverState
*qemu_char_get_next_serial(void)
3949 static int next_serial
;
3950 CharDriverState
*chr
;
3952 /* FIXME: This function needs to go away: use chardev properties! */
3954 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3955 chr
= serial_hds
[next_serial
++];
3956 qemu_chr_fe_claim_no_fail(chr
);
3962 QemuOptsList qemu_chardev_opts
= {
3964 .implied_opt_name
= "backend",
3965 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3969 .type
= QEMU_OPT_STRING
,
3972 .type
= QEMU_OPT_STRING
,
3975 .type
= QEMU_OPT_STRING
,
3978 .type
= QEMU_OPT_STRING
,
3980 .name
= "localaddr",
3981 .type
= QEMU_OPT_STRING
,
3983 .name
= "localport",
3984 .type
= QEMU_OPT_STRING
,
3987 .type
= QEMU_OPT_NUMBER
,
3990 .type
= QEMU_OPT_BOOL
,
3993 .type
= QEMU_OPT_BOOL
,
3996 .type
= QEMU_OPT_BOOL
,
3999 .type
= QEMU_OPT_BOOL
,
4002 .type
= QEMU_OPT_BOOL
,
4004 .name
= "reconnect",
4005 .type
= QEMU_OPT_NUMBER
,
4008 .type
= QEMU_OPT_BOOL
,
4011 .type
= QEMU_OPT_NUMBER
,
4014 .type
= QEMU_OPT_NUMBER
,
4017 .type
= QEMU_OPT_NUMBER
,
4020 .type
= QEMU_OPT_NUMBER
,
4023 .type
= QEMU_OPT_BOOL
,
4026 .type
= QEMU_OPT_BOOL
,
4029 .type
= QEMU_OPT_STRING
,
4032 .type
= QEMU_OPT_NUMBER
,
4035 .type
= QEMU_OPT_SIZE
,
4038 .type
= QEMU_OPT_STRING
,
4040 { /* end of list */ }
4046 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4047 ChardevBackend
*backend
,
4051 ChardevFile
*file
= backend
->u
.file
;
4055 error_setg(errp
, "input file not supported");
4059 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
4060 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4061 if (out
== INVALID_HANDLE_VALUE
) {
4062 error_setg(errp
, "open %s failed", file
->out
);
4065 return qemu_chr_open_win_file(out
);
4068 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4069 ChardevBackend
*backend
,
4073 ChardevHostdev
*serial
= backend
->u
.serial
;
4074 return qemu_chr_open_win_path(serial
->device
, errp
);
4079 static int qmp_chardev_open_file_source(char *src
, int flags
,
4084 TFR(fd
= qemu_open(src
, flags
, 0666));
4086 error_setg_file_open(errp
, errno
, src
);
4091 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4092 ChardevBackend
*backend
,
4096 ChardevFile
*file
= backend
->u
.file
;
4097 int flags
, in
= -1, out
;
4099 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
4100 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4107 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4114 return qemu_chr_open_fd(in
, out
);
4117 #ifdef HAVE_CHARDEV_SERIAL
4118 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4119 ChardevBackend
*backend
,
4123 ChardevHostdev
*serial
= backend
->u
.serial
;
4126 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4130 qemu_set_nonblock(fd
);
4131 return qemu_chr_open_tty_fd(fd
);
4135 #ifdef HAVE_CHARDEV_PARPORT
4136 static CharDriverState
*qmp_chardev_open_parallel(const char *id
,
4137 ChardevBackend
*backend
,
4141 ChardevHostdev
*parallel
= backend
->u
.parallel
;
4144 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4148 return qemu_chr_open_pp_fd(fd
, errp
);
4154 static void socket_try_connect(CharDriverState
*chr
)
4158 if (!qemu_chr_open_socket_fd(chr
, &err
)) {
4159 check_report_connect_error(chr
, err
);
4163 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4165 CharDriverState
*chr
= opaque
;
4166 TCPCharDriver
*s
= chr
->opaque
;
4168 s
->reconnect_timer
= 0;
4174 socket_try_connect(chr
);
4179 static CharDriverState
*qmp_chardev_open_socket(const char *id
,
4180 ChardevBackend
*backend
,
4184 CharDriverState
*chr
;
4186 ChardevSocket
*sock
= backend
->u
.socket
;
4187 SocketAddress
*addr
= sock
->addr
;
4188 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4189 bool is_listen
= sock
->has_server
? sock
->server
: true;
4190 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4191 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4192 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4194 chr
= qemu_chr_alloc();
4195 s
= g_new0(TCPCharDriver
, 1);
4199 s
->is_unix
= addr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
4200 s
->is_listen
= is_listen
;
4201 s
->is_telnet
= is_telnet
;
4202 s
->do_nodelay
= do_nodelay
;
4203 qapi_copy_SocketAddress(&s
->addr
, sock
->addr
);
4206 chr
->chr_write
= tcp_chr_write
;
4207 chr
->chr_sync_read
= tcp_chr_sync_read
;
4208 chr
->chr_close
= tcp_chr_close
;
4209 chr
->get_msgfds
= tcp_get_msgfds
;
4210 chr
->set_msgfds
= tcp_set_msgfds
;
4211 chr
->chr_add_client
= tcp_chr_add_client
;
4212 chr
->chr_add_watch
= tcp_chr_add_watch
;
4213 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4214 /* be isn't opened until we get a connection */
4215 chr
->explicit_be_open
= true;
4217 chr
->filename
= g_malloc(CHR_MAX_FILENAME_SIZE
);
4218 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
, "disconnected:",
4219 addr
, is_listen
, is_telnet
);
4223 s
->do_telnetopt
= 1;
4225 } else if (reconnect
> 0) {
4226 s
->reconnect_time
= reconnect
;
4229 if (s
->reconnect_time
) {
4230 socket_try_connect(chr
);
4231 } else if (!qemu_chr_open_socket_fd(chr
, errp
)) {
4233 g_free(chr
->filename
);
4238 if (is_listen
&& is_waitconnect
) {
4239 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
4241 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
4242 qemu_set_nonblock(s
->listen_fd
);
4248 static CharDriverState
*qmp_chardev_open_udp(const char *id
,
4249 ChardevBackend
*backend
,
4253 ChardevUdp
*udp
= backend
->u
.udp
;
4256 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
4260 return qemu_chr_open_udp_fd(fd
);
4263 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4266 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4267 CharDriverState
*chr
= NULL
;
4268 Error
*local_err
= NULL
;
4272 chr
= qemu_chr_find(id
);
4274 error_setg(errp
, "Chardev '%s' already exists", id
);
4279 for (i
= backends
; i
; i
= i
->next
) {
4282 if (cd
->kind
== backend
->type
) {
4283 chr
= cd
->create(id
, backend
, ret
, &local_err
);
4285 error_propagate(errp
, local_err
);
4294 error_setg(errp
, "chardev backend not available");
4298 chr
->label
= g_strdup(id
);
4299 chr
->avail_connections
=
4300 (backend
->type
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4301 if (!chr
->filename
) {
4302 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->type
]);
4304 if (!chr
->explicit_be_open
) {
4305 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4307 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4315 void qmp_chardev_remove(const char *id
, Error
**errp
)
4317 CharDriverState
*chr
;
4319 chr
= qemu_chr_find(id
);
4321 error_setg(errp
, "Chardev '%s' not found", id
);
4324 if (chr
->chr_can_read
|| chr
->chr_read
||
4325 chr
->chr_event
|| chr
->handler_opaque
) {
4326 error_setg(errp
, "Chardev '%s' is busy", id
);
4329 qemu_chr_delete(chr
);
4332 static void register_types(void)
4334 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
,
4335 qemu_chr_open_null
);
4336 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4337 qemu_chr_parse_socket
, qmp_chardev_open_socket
);
4338 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
,
4339 qmp_chardev_open_udp
);
4340 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4341 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4342 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4343 qemu_chr_parse_file_out
, qmp_chardev_open_file
);
4344 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4345 qemu_chr_parse_stdio
, qemu_chr_open_stdio
);
4346 #if defined HAVE_CHARDEV_SERIAL
4347 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4348 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4349 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4350 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4352 #ifdef HAVE_CHARDEV_PARPORT
4353 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4354 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4355 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4356 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4358 #ifdef HAVE_CHARDEV_PTY
4359 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
,
4363 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
,
4364 qemu_chr_open_win_con
);
4366 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4367 qemu_chr_parse_pipe
, qemu_chr_open_pipe
);
4368 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
,
4370 /* Bug-compatibility: */
4371 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4372 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4373 /* this must be done after machine init, since we register FEs with muxes
4374 * as part of realize functions like serial_isa_realizefn when -nographic
4377 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4380 type_init(register_types
);