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 */
95 static void qapi_copy_SocketAddress(SocketAddress
**p_dest
,
98 QmpOutputVisitor
*qov
;
105 qov
= qmp_output_visitor_new();
106 ov
= qmp_output_get_visitor(qov
);
107 visit_type_SocketAddress(ov
, &src
, NULL
, &error_abort
);
108 obj
= qmp_output_get_qobject(qov
);
109 qmp_output_visitor_cleanup(qov
);
114 qiv
= qmp_input_visitor_new(obj
);
115 iv
= qmp_input_get_visitor(qiv
);
116 visit_type_SocketAddress(iv
, p_dest
, NULL
, &error_abort
);
117 qmp_input_visitor_cleanup(qiv
);
121 static int SocketAddress_to_str(char *dest
, int max_len
,
122 const char *prefix
, SocketAddress
*addr
,
123 bool is_listen
, bool is_telnet
)
125 switch (addr
->kind
) {
126 case SOCKET_ADDRESS_KIND_INET
:
127 return snprintf(dest
, max_len
, "%s%s:%s:%s%s", prefix
,
128 is_telnet
? "telnet" : "tcp", addr
->inet
->host
,
129 addr
->inet
->port
, is_listen
? ",server" : "");
131 case SOCKET_ADDRESS_KIND_UNIX
:
132 return snprintf(dest
, max_len
, "%sunix:%s%s", prefix
,
133 addr
->q_unix
->path
, is_listen
? ",server" : "");
135 case SOCKET_ADDRESS_KIND_FD
:
136 return snprintf(dest
, max_len
, "%sfd:%s%s", prefix
, addr
->fd
->str
,
137 is_listen
? ",server" : "");
144 static int sockaddr_to_str(char *dest
, int max_len
,
145 struct sockaddr_storage
*ss
, socklen_t ss_len
,
146 struct sockaddr_storage
*ps
, socklen_t ps_len
,
147 bool is_listen
, bool is_telnet
)
149 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
150 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
151 const char *left
= "", *right
= "";
153 switch (ss
->ss_family
) {
156 return snprintf(dest
, max_len
, "unix:%s%s",
157 ((struct sockaddr_un
*)(ss
))->sun_path
,
158 is_listen
? ",server" : "");
165 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
166 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
167 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
168 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
169 return snprintf(dest
, max_len
, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
170 is_telnet
? "telnet" : "tcp",
171 left
, shost
, right
, sserv
,
172 is_listen
? ",server" : "",
173 left
, phost
, right
, pserv
);
176 return snprintf(dest
, max_len
, "unknown");
180 /***********************************************************/
181 /* character device */
183 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
184 QTAILQ_HEAD_INITIALIZER(chardevs
);
186 CharDriverState
*qemu_chr_alloc(void)
188 CharDriverState
*chr
= g_malloc0(sizeof(CharDriverState
));
189 qemu_mutex_init(&chr
->chr_write_lock
);
193 void qemu_chr_be_event(CharDriverState
*s
, int event
)
195 /* Keep track if the char device is open */
197 case CHR_EVENT_OPENED
:
200 case CHR_EVENT_CLOSED
:
207 s
->chr_event(s
->handler_opaque
, event
);
210 void qemu_chr_be_generic_open(CharDriverState
*s
)
212 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
215 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
219 qemu_mutex_lock(&s
->chr_write_lock
);
220 ret
= s
->chr_write(s
, buf
, len
);
221 qemu_mutex_unlock(&s
->chr_write_lock
);
225 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
230 qemu_mutex_lock(&s
->chr_write_lock
);
231 while (offset
< len
) {
233 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
234 if (res
== -1 && errno
== EAGAIN
) {
237 } while (res
== -1 && errno
== EAGAIN
);
245 qemu_mutex_unlock(&s
->chr_write_lock
);
253 int qemu_chr_fe_read_all(CharDriverState
*s
, uint8_t *buf
, int len
)
255 int offset
= 0, counter
= 10;
258 if (!s
->chr_sync_read
) {
262 while (offset
< len
) {
264 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
265 if (res
== -1 && errno
== EAGAIN
) {
268 } while (res
== -1 && errno
== EAGAIN
);
288 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
292 return s
->chr_ioctl(s
, cmd
, arg
);
295 int qemu_chr_be_can_write(CharDriverState
*s
)
297 if (!s
->chr_can_read
)
299 return s
->chr_can_read(s
->handler_opaque
);
302 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
305 s
->chr_read(s
->handler_opaque
, buf
, len
);
309 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
312 return (qemu_chr_fe_get_msgfds(s
, &fd
, 1) == 1) ? fd
: -1;
315 int qemu_chr_fe_get_msgfds(CharDriverState
*s
, int *fds
, int len
)
317 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
320 int qemu_chr_fe_set_msgfds(CharDriverState
*s
, int *fds
, int num
)
322 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
325 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
327 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
330 void qemu_chr_accept_input(CharDriverState
*s
)
332 if (s
->chr_accept_input
)
333 s
->chr_accept_input(s
);
337 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
339 char buf
[READ_BUF_LEN
];
342 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
343 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
347 static void remove_fd_in_watch(CharDriverState
*chr
);
349 void qemu_chr_add_handlers(CharDriverState
*s
,
350 IOCanReadHandler
*fd_can_read
,
351 IOReadHandler
*fd_read
,
352 IOEventHandler
*fd_event
,
357 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
359 remove_fd_in_watch(s
);
363 s
->chr_can_read
= fd_can_read
;
364 s
->chr_read
= fd_read
;
365 s
->chr_event
= fd_event
;
366 s
->handler_opaque
= opaque
;
367 if (fe_open
&& s
->chr_update_read_handler
)
368 s
->chr_update_read_handler(s
);
370 if (!s
->explicit_fe_open
) {
371 qemu_chr_fe_set_open(s
, fe_open
);
374 /* We're connecting to an already opened device, so let's make sure we
375 also get the open event */
376 if (fe_open
&& s
->be_open
) {
377 qemu_chr_be_generic_open(s
);
381 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
386 static CharDriverState
*qemu_chr_open_null(void)
388 CharDriverState
*chr
;
390 chr
= qemu_chr_alloc();
391 chr
->chr_write
= null_chr_write
;
392 chr
->explicit_be_open
= true;
396 /* MUX driver for serial I/O splitting */
398 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
399 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
401 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
402 IOReadHandler
*chr_read
[MAX_MUX
];
403 IOEventHandler
*chr_event
[MAX_MUX
];
404 void *ext_opaque
[MAX_MUX
];
405 CharDriverState
*drv
;
410 /* Intermediate input buffer allows to catch escape sequences even if the
411 currently active device is not accepting any input - but only until it
413 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
418 /* Protected by the CharDriverState chr_write_lock. */
420 int64_t timestamps_start
;
424 /* Called with chr_write_lock held. */
425 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
427 MuxDriver
*d
= chr
->opaque
;
429 if (!d
->timestamps
) {
430 ret
= qemu_chr_fe_write(d
->drv
, buf
, len
);
435 for (i
= 0; i
< len
; i
++) {
441 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
442 if (d
->timestamps_start
== -1)
443 d
->timestamps_start
= ti
;
444 ti
-= d
->timestamps_start
;
446 snprintf(buf1
, sizeof(buf1
),
447 "[%02d:%02d:%02d.%03d] ",
452 qemu_chr_fe_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
455 ret
+= qemu_chr_fe_write(d
->drv
, buf
+i
, 1);
456 if (buf
[i
] == '\n') {
464 static const char * const mux_help
[] = {
465 "% h print this help\n\r",
466 "% x exit emulator\n\r",
467 "% s save disk data back to file (if -snapshot)\n\r",
468 "% t toggle console timestamps\n\r",
469 "% b send break (magic sysrq)\n\r",
470 "% c switch between console and monitor\n\r",
475 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
476 static void mux_print_help(CharDriverState
*chr
)
479 char ebuf
[15] = "Escape-Char";
480 char cbuf
[50] = "\n\r";
482 if (term_escape_char
> 0 && term_escape_char
< 26) {
483 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
484 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
486 snprintf(cbuf
, sizeof(cbuf
),
487 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
490 qemu_chr_fe_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
491 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
492 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
493 if (mux_help
[i
][j
] == '%')
494 qemu_chr_fe_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
496 qemu_chr_fe_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
501 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
503 if (d
->chr_event
[mux_nr
])
504 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
507 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
509 if (d
->term_got_escape
) {
510 d
->term_got_escape
= 0;
511 if (ch
== term_escape_char
)
520 const char *term
= "QEMU: Terminated\n\r";
521 qemu_chr_fe_write(chr
, (uint8_t *)term
, strlen(term
));
529 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
532 /* Switch to the next registered device */
533 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
535 if (d
->focus
>= d
->mux_cnt
)
537 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
540 d
->timestamps
= !d
->timestamps
;
541 d
->timestamps_start
= -1;
545 } else if (ch
== term_escape_char
) {
546 d
->term_got_escape
= 1;
554 static void mux_chr_accept_input(CharDriverState
*chr
)
556 MuxDriver
*d
= chr
->opaque
;
559 while (d
->prod
[m
] != d
->cons
[m
] &&
560 d
->chr_can_read
[m
] &&
561 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
562 d
->chr_read
[m
](d
->ext_opaque
[m
],
563 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
567 static int mux_chr_can_read(void *opaque
)
569 CharDriverState
*chr
= opaque
;
570 MuxDriver
*d
= chr
->opaque
;
573 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
575 if (d
->chr_can_read
[m
])
576 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
580 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
582 CharDriverState
*chr
= opaque
;
583 MuxDriver
*d
= chr
->opaque
;
587 mux_chr_accept_input (opaque
);
589 for(i
= 0; i
< size
; i
++)
590 if (mux_proc_byte(chr
, d
, buf
[i
])) {
591 if (d
->prod
[m
] == d
->cons
[m
] &&
592 d
->chr_can_read
[m
] &&
593 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
594 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
596 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
600 static void mux_chr_event(void *opaque
, int event
)
602 CharDriverState
*chr
= opaque
;
603 MuxDriver
*d
= chr
->opaque
;
606 /* Send the event to all registered listeners */
607 for (i
= 0; i
< d
->mux_cnt
; i
++)
608 mux_chr_send_event(d
, i
, event
);
611 static void mux_chr_update_read_handler(CharDriverState
*chr
)
613 MuxDriver
*d
= chr
->opaque
;
615 if (d
->mux_cnt
>= MAX_MUX
) {
616 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
619 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
620 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
621 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
622 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
623 /* Fix up the real driver with mux routines */
624 if (d
->mux_cnt
== 0) {
625 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
628 if (d
->focus
!= -1) {
629 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
631 d
->focus
= d
->mux_cnt
;
633 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
636 static bool muxes_realized
;
639 * Called after processing of default and command-line-specified
640 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
641 * to a mux chardev. This is done here to ensure that
642 * output/prompts/banners are only displayed for the FE that has
643 * focus when initial command-line processing/machine init is
646 * After this point, any new FE attached to any new or existing
647 * mux will receive CHR_EVENT_OPENED notifications for the BE
650 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
652 CharDriverState
*chr
;
654 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
656 MuxDriver
*d
= chr
->opaque
;
659 /* send OPENED to all already-attached FEs */
660 for (i
= 0; i
< d
->mux_cnt
; i
++) {
661 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
663 /* mark mux as OPENED so any new FEs will immediately receive
666 qemu_chr_be_generic_open(chr
);
669 muxes_realized
= true;
672 static Notifier muxes_realize_notify
= {
673 .notify
= muxes_realize_done
,
676 static GSource
*mux_chr_add_watch(CharDriverState
*s
, GIOCondition cond
)
678 MuxDriver
*d
= s
->opaque
;
679 return d
->drv
->chr_add_watch(d
->drv
, cond
);
682 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
684 CharDriverState
*chr
;
687 chr
= qemu_chr_alloc();
688 d
= g_new0(MuxDriver
, 1);
693 chr
->chr_write
= mux_chr_write
;
694 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
695 chr
->chr_accept_input
= mux_chr_accept_input
;
696 /* Frontend guest-open / -close notification is not support with muxes */
697 chr
->chr_set_fe_open
= NULL
;
698 if (drv
->chr_add_watch
) {
699 chr
->chr_add_watch
= mux_chr_add_watch
;
701 /* only default to opened state if we've realized the initial
704 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
712 int send_all(int fd
, const void *buf
, int len1
)
718 ret
= send(fd
, buf
, len
, 0);
720 errno
= WSAGetLastError();
721 if (errno
!= WSAEWOULDBLOCK
) {
724 } else if (ret
== 0) {
736 int send_all(int fd
, const void *_buf
, int len1
)
739 const uint8_t *buf
= _buf
;
743 ret
= write(fd
, buf
, len
);
745 if (errno
!= EINTR
&& errno
!= EAGAIN
)
747 } else if (ret
== 0) {
757 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
763 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
765 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
782 typedef struct IOWatchPoll
789 IOCanReadHandler
*fd_can_read
;
794 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
796 return container_of(source
, IOWatchPoll
, parent
);
799 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
801 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
802 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
803 bool was_active
= iwp
->src
!= NULL
;
804 if (was_active
== now_active
) {
809 iwp
->src
= g_io_create_watch(iwp
->channel
,
810 G_IO_IN
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
);
811 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
812 g_source_attach(iwp
->src
, NULL
);
814 g_source_destroy(iwp
->src
);
815 g_source_unref(iwp
->src
);
821 static gboolean
io_watch_poll_check(GSource
*source
)
826 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
832 static void io_watch_poll_finalize(GSource
*source
)
834 /* Due to a glib bug, removing the last reference to a source
835 * inside a finalize callback causes recursive locking (and a
836 * deadlock). This is not a problem inside other callbacks,
837 * including dispatch callbacks, so we call io_remove_watch_poll
838 * to remove this source. At this point, iwp->src must
839 * be NULL, or we would leak it.
841 * This would be solved much more elegantly by child sources,
842 * but we support older glib versions that do not have them.
844 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
845 assert(iwp
->src
== NULL
);
848 static GSourceFuncs io_watch_poll_funcs
= {
849 .prepare
= io_watch_poll_prepare
,
850 .check
= io_watch_poll_check
,
851 .dispatch
= io_watch_poll_dispatch
,
852 .finalize
= io_watch_poll_finalize
,
855 /* Can only be used for read */
856 static guint
io_add_watch_poll(GIOChannel
*channel
,
857 IOCanReadHandler
*fd_can_read
,
864 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
865 iwp
->fd_can_read
= fd_can_read
;
866 iwp
->opaque
= user_data
;
867 iwp
->channel
= channel
;
868 iwp
->fd_read
= (GSourceFunc
) fd_read
;
871 tag
= g_source_attach(&iwp
->parent
, NULL
);
872 g_source_unref(&iwp
->parent
);
876 static void io_remove_watch_poll(guint tag
)
881 g_return_if_fail (tag
> 0);
883 source
= g_main_context_find_source_by_id(NULL
, tag
);
884 g_return_if_fail (source
!= NULL
);
886 iwp
= io_watch_poll_from_source(source
);
888 g_source_destroy(iwp
->src
);
889 g_source_unref(iwp
->src
);
892 g_source_destroy(&iwp
->parent
);
895 static void remove_fd_in_watch(CharDriverState
*chr
)
897 if (chr
->fd_in_tag
) {
898 io_remove_watch_poll(chr
->fd_in_tag
);
904 static GIOChannel
*io_channel_from_fd(int fd
)
912 chan
= g_io_channel_unix_new(fd
);
914 g_io_channel_set_encoding(chan
, NULL
, NULL
);
915 g_io_channel_set_buffered(chan
, FALSE
);
921 static GIOChannel
*io_channel_from_socket(int fd
)
930 chan
= g_io_channel_win32_new_socket(fd
);
932 chan
= g_io_channel_unix_new(fd
);
935 g_io_channel_set_encoding(chan
, NULL
, NULL
);
936 g_io_channel_set_buffered(chan
, FALSE
);
941 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
944 GIOStatus status
= G_IO_STATUS_NORMAL
;
946 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
947 gsize bytes_written
= 0;
949 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
950 &bytes_written
, NULL
);
951 offset
+= bytes_written
;
958 case G_IO_STATUS_NORMAL
:
961 case G_IO_STATUS_AGAIN
:
973 typedef struct FDCharDriver
{
974 CharDriverState
*chr
;
975 GIOChannel
*fd_in
, *fd_out
;
979 /* Called with chr_write_lock held. */
980 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
982 FDCharDriver
*s
= chr
->opaque
;
984 return io_channel_send(s
->fd_out
, buf
, len
);
987 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
989 CharDriverState
*chr
= opaque
;
990 FDCharDriver
*s
= chr
->opaque
;
992 uint8_t buf
[READ_BUF_LEN
];
997 if (len
> s
->max_size
) {
1004 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
1005 len
, &bytes_read
, NULL
);
1006 if (status
== G_IO_STATUS_EOF
) {
1007 remove_fd_in_watch(chr
);
1008 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1011 if (status
== G_IO_STATUS_NORMAL
) {
1012 qemu_chr_be_write(chr
, buf
, bytes_read
);
1018 static int fd_chr_read_poll(void *opaque
)
1020 CharDriverState
*chr
= opaque
;
1021 FDCharDriver
*s
= chr
->opaque
;
1023 s
->max_size
= qemu_chr_be_can_write(chr
);
1027 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1029 FDCharDriver
*s
= chr
->opaque
;
1030 return g_io_create_watch(s
->fd_out
, cond
);
1033 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1035 FDCharDriver
*s
= chr
->opaque
;
1037 remove_fd_in_watch(chr
);
1039 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
1044 static void fd_chr_close(struct CharDriverState
*chr
)
1046 FDCharDriver
*s
= chr
->opaque
;
1048 remove_fd_in_watch(chr
);
1050 g_io_channel_unref(s
->fd_in
);
1053 g_io_channel_unref(s
->fd_out
);
1057 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1060 /* open a character device to a unix fd */
1061 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1063 CharDriverState
*chr
;
1066 chr
= qemu_chr_alloc();
1067 s
= g_new0(FDCharDriver
, 1);
1068 s
->fd_in
= io_channel_from_fd(fd_in
);
1069 s
->fd_out
= io_channel_from_fd(fd_out
);
1070 qemu_set_nonblock(fd_out
);
1073 chr
->chr_add_watch
= fd_chr_add_watch
;
1074 chr
->chr_write
= fd_chr_write
;
1075 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1076 chr
->chr_close
= fd_chr_close
;
1081 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1084 char filename_in
[CHR_MAX_FILENAME_SIZE
];
1085 char filename_out
[CHR_MAX_FILENAME_SIZE
];
1086 const char *filename
= opts
->device
;
1088 if (filename
== NULL
) {
1089 fprintf(stderr
, "chardev: pipe: no filename given\n");
1093 snprintf(filename_in
, CHR_MAX_FILENAME_SIZE
, "%s.in", filename
);
1094 snprintf(filename_out
, CHR_MAX_FILENAME_SIZE
, "%s.out", filename
);
1095 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1096 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1097 if (fd_in
< 0 || fd_out
< 0) {
1102 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1107 return qemu_chr_open_fd(fd_in
, fd_out
);
1110 /* init terminal so that we can grab keys */
1111 static struct termios oldtty
;
1112 static int old_fd0_flags
;
1113 static bool stdio_in_use
;
1114 static bool stdio_allow_signal
;
1115 static bool stdio_echo_state
;
1117 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
);
1119 static void term_exit(void)
1121 tcsetattr (0, TCSANOW
, &oldtty
);
1122 fcntl(0, F_SETFL
, old_fd0_flags
);
1125 static void term_stdio_handler(int sig
)
1127 /* restore echo after resume from suspend. */
1128 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
1131 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1135 stdio_echo_state
= echo
;
1138 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1139 |INLCR
|IGNCR
|ICRNL
|IXON
);
1140 tty
.c_oflag
|= OPOST
;
1141 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1142 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1145 tty
.c_cc
[VTIME
] = 0;
1147 if (!stdio_allow_signal
)
1148 tty
.c_lflag
&= ~ISIG
;
1150 tcsetattr (0, TCSANOW
, &tty
);
1153 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1159 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
1161 CharDriverState
*chr
;
1162 struct sigaction act
;
1164 if (is_daemonized()) {
1165 error_report("cannot use stdio with -daemonize");
1170 error_report("cannot use stdio by multiple character devices");
1174 stdio_in_use
= true;
1175 old_fd0_flags
= fcntl(0, F_GETFL
);
1176 tcgetattr(0, &oldtty
);
1177 qemu_set_nonblock(0);
1180 memset(&act
, 0, sizeof(act
));
1181 act
.sa_handler
= term_stdio_handler
;
1182 sigaction(SIGCONT
, &act
, NULL
);
1184 chr
= qemu_chr_open_fd(0, 1);
1185 chr
->chr_close
= qemu_chr_close_stdio
;
1186 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1187 if (opts
->has_signal
) {
1188 stdio_allow_signal
= opts
->signal
;
1190 qemu_chr_fe_set_echo(chr
, false);
1195 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1196 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1197 || defined(__GLIBC__)
1199 #define HAVE_CHARDEV_TTY 1
1205 /* Protected by the CharDriverState chr_write_lock. */
1211 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1212 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1214 static gboolean
pty_chr_timer(gpointer opaque
)
1216 struct CharDriverState
*chr
= opaque
;
1217 PtyCharDriver
*s
= chr
->opaque
;
1219 qemu_mutex_lock(&chr
->chr_write_lock
);
1222 if (!s
->connected
) {
1224 pty_chr_update_read_handler_locked(chr
);
1226 qemu_mutex_unlock(&chr
->chr_write_lock
);
1230 /* Called with chr_write_lock held. */
1231 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1233 PtyCharDriver
*s
= chr
->opaque
;
1236 g_source_remove(s
->timer_tag
);
1241 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1243 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1247 /* Called with chr_write_lock held. */
1248 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1250 PtyCharDriver
*s
= chr
->opaque
;
1253 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1254 pfd
.events
= G_IO_OUT
;
1257 if (pfd
.revents
& G_IO_HUP
) {
1258 pty_chr_state(chr
, 0);
1260 pty_chr_state(chr
, 1);
1264 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1266 qemu_mutex_lock(&chr
->chr_write_lock
);
1267 pty_chr_update_read_handler_locked(chr
);
1268 qemu_mutex_unlock(&chr
->chr_write_lock
);
1271 /* Called with chr_write_lock held. */
1272 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1274 PtyCharDriver
*s
= chr
->opaque
;
1276 if (!s
->connected
) {
1277 /* guest sends data, check for (re-)connect */
1278 pty_chr_update_read_handler_locked(chr
);
1279 if (!s
->connected
) {
1283 return io_channel_send(s
->fd
, buf
, len
);
1286 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1288 PtyCharDriver
*s
= chr
->opaque
;
1289 if (!s
->connected
) {
1292 return g_io_create_watch(s
->fd
, cond
);
1295 static int pty_chr_read_poll(void *opaque
)
1297 CharDriverState
*chr
= opaque
;
1298 PtyCharDriver
*s
= chr
->opaque
;
1300 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1301 return s
->read_bytes
;
1304 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1306 CharDriverState
*chr
= opaque
;
1307 PtyCharDriver
*s
= chr
->opaque
;
1309 uint8_t buf
[READ_BUF_LEN
];
1313 if (len
> s
->read_bytes
)
1314 len
= s
->read_bytes
;
1318 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1319 if (status
!= G_IO_STATUS_NORMAL
) {
1320 pty_chr_state(chr
, 0);
1323 pty_chr_state(chr
, 1);
1324 qemu_chr_be_write(chr
, buf
, size
);
1329 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1331 CharDriverState
*chr
= opaque
;
1332 PtyCharDriver
*s
= chr
->opaque
;
1335 qemu_chr_be_generic_open(chr
);
1339 /* Called with chr_write_lock held. */
1340 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1342 PtyCharDriver
*s
= chr
->opaque
;
1346 g_source_remove(s
->open_tag
);
1349 remove_fd_in_watch(chr
);
1351 /* (re-)connect poll interval for idle guests: once per second.
1352 * We check more frequently in case the guests sends data to
1353 * the virtual device linked to our pty. */
1354 pty_chr_rearm_timer(chr
, 1000);
1357 g_source_remove(s
->timer_tag
);
1360 if (!s
->connected
) {
1361 g_assert(s
->open_tag
== 0);
1363 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1365 if (!chr
->fd_in_tag
) {
1366 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1372 static void pty_chr_close(struct CharDriverState
*chr
)
1374 PtyCharDriver
*s
= chr
->opaque
;
1377 qemu_mutex_lock(&chr
->chr_write_lock
);
1378 pty_chr_state(chr
, 0);
1379 fd
= g_io_channel_unix_get_fd(s
->fd
);
1380 g_io_channel_unref(s
->fd
);
1383 g_source_remove(s
->timer_tag
);
1386 qemu_mutex_unlock(&chr
->chr_write_lock
);
1388 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1391 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1394 CharDriverState
*chr
;
1396 int master_fd
, slave_fd
;
1397 char pty_name
[PATH_MAX
];
1399 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1400 if (master_fd
< 0) {
1405 qemu_set_nonblock(master_fd
);
1407 chr
= qemu_chr_alloc();
1409 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1410 ret
->pty
= g_strdup(pty_name
);
1411 ret
->has_pty
= true;
1413 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1416 s
= g_new0(PtyCharDriver
, 1);
1418 chr
->chr_write
= pty_chr_write
;
1419 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1420 chr
->chr_close
= pty_chr_close
;
1421 chr
->chr_add_watch
= pty_chr_add_watch
;
1422 chr
->explicit_be_open
= true;
1424 s
->fd
= io_channel_from_fd(master_fd
);
1430 static void tty_serial_init(int fd
, int speed
,
1431 int parity
, int data_bits
, int stop_bits
)
1437 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1438 speed
, parity
, data_bits
, stop_bits
);
1440 tcgetattr (fd
, &tty
);
1442 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1443 speed
= speed
* 10 / 11;
1460 /* Non-Posix values follow. They may be unsupported on some systems. */
1462 check_speed(115200);
1464 check_speed(230400);
1467 check_speed(460800);
1470 check_speed(500000);
1473 check_speed(576000);
1476 check_speed(921600);
1479 check_speed(1000000);
1482 check_speed(1152000);
1485 check_speed(1500000);
1488 check_speed(2000000);
1491 check_speed(2500000);
1494 check_speed(3000000);
1497 check_speed(3500000);
1500 check_speed(4000000);
1505 cfsetispeed(&tty
, spd
);
1506 cfsetospeed(&tty
, spd
);
1508 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1509 |INLCR
|IGNCR
|ICRNL
|IXON
);
1510 tty
.c_oflag
|= OPOST
;
1511 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1512 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1533 tty
.c_cflag
|= PARENB
;
1536 tty
.c_cflag
|= PARENB
| PARODD
;
1540 tty
.c_cflag
|= CSTOPB
;
1542 tcsetattr (fd
, TCSANOW
, &tty
);
1545 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1547 FDCharDriver
*s
= chr
->opaque
;
1550 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1552 QEMUSerialSetParams
*ssp
= arg
;
1553 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1554 ssp
->speed
, ssp
->parity
,
1555 ssp
->data_bits
, ssp
->stop_bits
);
1558 case CHR_IOCTL_SERIAL_SET_BREAK
:
1560 int enable
= *(int *)arg
;
1562 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1566 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1569 int *targ
= (int *)arg
;
1570 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1572 if (sarg
& TIOCM_CTS
)
1573 *targ
|= CHR_TIOCM_CTS
;
1574 if (sarg
& TIOCM_CAR
)
1575 *targ
|= CHR_TIOCM_CAR
;
1576 if (sarg
& TIOCM_DSR
)
1577 *targ
|= CHR_TIOCM_DSR
;
1578 if (sarg
& TIOCM_RI
)
1579 *targ
|= CHR_TIOCM_RI
;
1580 if (sarg
& TIOCM_DTR
)
1581 *targ
|= CHR_TIOCM_DTR
;
1582 if (sarg
& TIOCM_RTS
)
1583 *targ
|= CHR_TIOCM_RTS
;
1586 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1588 int sarg
= *(int *)arg
;
1590 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1591 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1592 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1593 if (sarg
& CHR_TIOCM_CTS
)
1595 if (sarg
& CHR_TIOCM_CAR
)
1597 if (sarg
& CHR_TIOCM_DSR
)
1599 if (sarg
& CHR_TIOCM_RI
)
1601 if (sarg
& CHR_TIOCM_DTR
)
1603 if (sarg
& CHR_TIOCM_RTS
)
1605 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1614 static void qemu_chr_close_tty(CharDriverState
*chr
)
1616 FDCharDriver
*s
= chr
->opaque
;
1620 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1630 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1632 CharDriverState
*chr
;
1634 tty_serial_init(fd
, 115200, 'N', 8, 1);
1635 chr
= qemu_chr_open_fd(fd
, fd
);
1636 chr
->chr_ioctl
= tty_serial_ioctl
;
1637 chr
->chr_close
= qemu_chr_close_tty
;
1640 #endif /* __linux__ || __sun__ */
1642 #if defined(__linux__)
1644 #define HAVE_CHARDEV_PARPORT 1
1649 } ParallelCharDriver
;
1651 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1653 if (s
->mode
!= mode
) {
1655 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1662 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1664 ParallelCharDriver
*drv
= chr
->opaque
;
1669 case CHR_IOCTL_PP_READ_DATA
:
1670 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1672 *(uint8_t *)arg
= b
;
1674 case CHR_IOCTL_PP_WRITE_DATA
:
1675 b
= *(uint8_t *)arg
;
1676 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1679 case CHR_IOCTL_PP_READ_CONTROL
:
1680 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1682 /* Linux gives only the lowest bits, and no way to know data
1683 direction! For better compatibility set the fixed upper
1685 *(uint8_t *)arg
= b
| 0xc0;
1687 case CHR_IOCTL_PP_WRITE_CONTROL
:
1688 b
= *(uint8_t *)arg
;
1689 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1692 case CHR_IOCTL_PP_READ_STATUS
:
1693 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1695 *(uint8_t *)arg
= b
;
1697 case CHR_IOCTL_PP_DATA_DIR
:
1698 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1701 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1702 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1703 struct ParallelIOArg
*parg
= arg
;
1704 int n
= read(fd
, parg
->buffer
, parg
->count
);
1705 if (n
!= parg
->count
) {
1710 case CHR_IOCTL_PP_EPP_READ
:
1711 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1712 struct ParallelIOArg
*parg
= arg
;
1713 int n
= read(fd
, parg
->buffer
, parg
->count
);
1714 if (n
!= parg
->count
) {
1719 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1720 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1721 struct ParallelIOArg
*parg
= arg
;
1722 int n
= write(fd
, parg
->buffer
, parg
->count
);
1723 if (n
!= parg
->count
) {
1728 case CHR_IOCTL_PP_EPP_WRITE
:
1729 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1730 struct ParallelIOArg
*parg
= arg
;
1731 int n
= write(fd
, parg
->buffer
, parg
->count
);
1732 if (n
!= parg
->count
) {
1743 static void pp_close(CharDriverState
*chr
)
1745 ParallelCharDriver
*drv
= chr
->opaque
;
1748 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1749 ioctl(fd
, PPRELEASE
);
1752 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1755 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1757 CharDriverState
*chr
;
1758 ParallelCharDriver
*drv
;
1760 if (ioctl(fd
, PPCLAIM
) < 0) {
1765 drv
= g_new0(ParallelCharDriver
, 1);
1767 drv
->mode
= IEEE1284_MODE_COMPAT
;
1769 chr
= qemu_chr_alloc();
1770 chr
->chr_write
= null_chr_write
;
1771 chr
->chr_ioctl
= pp_ioctl
;
1772 chr
->chr_close
= pp_close
;
1777 #endif /* __linux__ */
1779 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1781 #define HAVE_CHARDEV_PARPORT 1
1783 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1785 int fd
= (int)(intptr_t)chr
->opaque
;
1789 case CHR_IOCTL_PP_READ_DATA
:
1790 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1792 *(uint8_t *)arg
= b
;
1794 case CHR_IOCTL_PP_WRITE_DATA
:
1795 b
= *(uint8_t *)arg
;
1796 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1799 case CHR_IOCTL_PP_READ_CONTROL
:
1800 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1802 *(uint8_t *)arg
= b
;
1804 case CHR_IOCTL_PP_WRITE_CONTROL
:
1805 b
= *(uint8_t *)arg
;
1806 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1809 case CHR_IOCTL_PP_READ_STATUS
:
1810 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1812 *(uint8_t *)arg
= b
;
1820 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1822 CharDriverState
*chr
;
1824 chr
= qemu_chr_alloc();
1825 chr
->opaque
= (void *)(intptr_t)fd
;
1826 chr
->chr_write
= null_chr_write
;
1827 chr
->chr_ioctl
= pp_ioctl
;
1828 chr
->explicit_be_open
= true;
1837 HANDLE hcom
, hrecv
, hsend
;
1842 /* Protected by the CharDriverState chr_write_lock. */
1848 HANDLE hInputReadyEvent
;
1849 HANDLE hInputDoneEvent
;
1850 HANDLE hInputThread
;
1851 uint8_t win_stdio_buf
;
1852 } WinStdioCharState
;
1854 #define NSENDBUF 2048
1855 #define NRECVBUF 2048
1856 #define MAXCONNECT 1
1857 #define NTIMEOUT 5000
1859 static int win_chr_poll(void *opaque
);
1860 static int win_chr_pipe_poll(void *opaque
);
1862 static void win_chr_close(CharDriverState
*chr
)
1864 WinCharState
*s
= chr
->opaque
;
1867 CloseHandle(s
->hsend
);
1871 CloseHandle(s
->hrecv
);
1875 CloseHandle(s
->hcom
);
1879 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1881 qemu_del_polling_cb(win_chr_poll
, chr
);
1883 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1886 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1888 WinCharState
*s
= chr
->opaque
;
1890 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1895 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1897 fprintf(stderr
, "Failed CreateEvent\n");
1900 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1902 fprintf(stderr
, "Failed CreateEvent\n");
1906 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1907 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1908 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1909 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1914 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1915 fprintf(stderr
, "Failed SetupComm\n");
1919 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1920 size
= sizeof(COMMCONFIG
);
1921 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1922 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1923 CommConfigDialog(filename
, NULL
, &comcfg
);
1925 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1926 fprintf(stderr
, "Failed SetCommState\n");
1930 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1931 fprintf(stderr
, "Failed SetCommMask\n");
1935 cto
.ReadIntervalTimeout
= MAXDWORD
;
1936 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1937 fprintf(stderr
, "Failed SetCommTimeouts\n");
1941 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1942 fprintf(stderr
, "Failed ClearCommError\n");
1945 qemu_add_polling_cb(win_chr_poll
, chr
);
1953 /* Called with chr_write_lock held. */
1954 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1956 WinCharState
*s
= chr
->opaque
;
1957 DWORD len
, ret
, size
, err
;
1960 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1961 s
->osend
.hEvent
= s
->hsend
;
1964 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1966 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1968 err
= GetLastError();
1969 if (err
== ERROR_IO_PENDING
) {
1970 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1988 static int win_chr_read_poll(CharDriverState
*chr
)
1990 WinCharState
*s
= chr
->opaque
;
1992 s
->max_size
= qemu_chr_be_can_write(chr
);
1996 static void win_chr_readfile(CharDriverState
*chr
)
1998 WinCharState
*s
= chr
->opaque
;
2000 uint8_t buf
[READ_BUF_LEN
];
2003 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2004 s
->orecv
.hEvent
= s
->hrecv
;
2005 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2007 err
= GetLastError();
2008 if (err
== ERROR_IO_PENDING
) {
2009 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2014 qemu_chr_be_write(chr
, buf
, size
);
2018 static void win_chr_read(CharDriverState
*chr
)
2020 WinCharState
*s
= chr
->opaque
;
2022 if (s
->len
> s
->max_size
)
2023 s
->len
= s
->max_size
;
2027 win_chr_readfile(chr
);
2030 static int win_chr_poll(void *opaque
)
2032 CharDriverState
*chr
= opaque
;
2033 WinCharState
*s
= chr
->opaque
;
2037 ClearCommError(s
->hcom
, &comerr
, &status
);
2038 if (status
.cbInQue
> 0) {
2039 s
->len
= status
.cbInQue
;
2040 win_chr_read_poll(chr
);
2047 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
2049 CharDriverState
*chr
;
2052 chr
= qemu_chr_alloc();
2053 s
= g_new0(WinCharState
, 1);
2055 chr
->chr_write
= win_chr_write
;
2056 chr
->chr_close
= win_chr_close
;
2058 if (win_chr_init(chr
, filename
) < 0) {
2066 static int win_chr_pipe_poll(void *opaque
)
2068 CharDriverState
*chr
= opaque
;
2069 WinCharState
*s
= chr
->opaque
;
2072 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2075 win_chr_read_poll(chr
);
2082 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 fprintf(stderr
, "Failed CreateEvent\n");
2097 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2099 fprintf(stderr
, "Failed CreateEvent\n");
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 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2114 ZeroMemory(&ov
, sizeof(ov
));
2115 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2116 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2118 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2122 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2124 fprintf(stderr
, "Failed GetOverlappedResult\n");
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(ChardevHostdev
*opts
)
2147 const char *filename
= opts
->device
;
2148 CharDriverState
*chr
;
2151 chr
= qemu_chr_alloc();
2152 s
= g_new0(WinCharState
, 1);
2154 chr
->chr_write
= win_chr_write
;
2155 chr
->chr_close
= win_chr_close
;
2157 if (win_chr_pipe_init(chr
, filename
) < 0) {
2165 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2167 CharDriverState
*chr
;
2170 chr
= qemu_chr_alloc();
2171 s
= g_new0(WinCharState
, 1);
2174 chr
->chr_write
= win_chr_write
;
2178 static CharDriverState
*qemu_chr_open_win_con(void)
2180 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2183 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2185 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2192 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2202 static void win_stdio_wait_func(void *opaque
)
2204 CharDriverState
*chr
= opaque
;
2205 WinStdioCharState
*stdio
= chr
->opaque
;
2206 INPUT_RECORD buf
[4];
2211 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2214 /* Avoid error storm */
2215 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2219 for (i
= 0; i
< dwSize
; i
++) {
2220 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2222 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2224 if (kev
->uChar
.AsciiChar
!= 0) {
2225 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2226 if (qemu_chr_be_can_write(chr
)) {
2227 uint8_t c
= kev
->uChar
.AsciiChar
;
2228 qemu_chr_be_write(chr
, &c
, 1);
2236 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2238 CharDriverState
*chr
= param
;
2239 WinStdioCharState
*stdio
= chr
->opaque
;
2245 /* Wait for one byte */
2246 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2248 /* Exit in case of error, continue if nothing read */
2256 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2257 if (stdio
->win_stdio_buf
== '\r') {
2261 /* Signal the main thread and wait until the byte was eaten */
2262 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2265 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2271 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2275 static void win_stdio_thread_wait_func(void *opaque
)
2277 CharDriverState
*chr
= opaque
;
2278 WinStdioCharState
*stdio
= chr
->opaque
;
2280 if (qemu_chr_be_can_write(chr
)) {
2281 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2284 SetEvent(stdio
->hInputDoneEvent
);
2287 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2289 WinStdioCharState
*stdio
= chr
->opaque
;
2292 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2295 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2297 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2301 static void win_stdio_close(CharDriverState
*chr
)
2303 WinStdioCharState
*stdio
= chr
->opaque
;
2305 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2306 CloseHandle(stdio
->hInputReadyEvent
);
2308 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2309 CloseHandle(stdio
->hInputDoneEvent
);
2311 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2312 TerminateThread(stdio
->hInputThread
, 0);
2315 g_free(chr
->opaque
);
2319 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2321 CharDriverState
*chr
;
2322 WinStdioCharState
*stdio
;
2326 chr
= qemu_chr_alloc();
2327 stdio
= g_new0(WinStdioCharState
, 1);
2329 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2330 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2331 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2335 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2337 chr
->opaque
= stdio
;
2338 chr
->chr_write
= win_stdio_write
;
2339 chr
->chr_close
= win_stdio_close
;
2342 if (qemu_add_wait_object(stdio
->hStdIn
,
2343 win_stdio_wait_func
, chr
)) {
2344 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2349 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2350 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2351 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2354 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2355 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2356 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2357 fprintf(stderr
, "cannot create stdio thread or event\n");
2360 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2361 win_stdio_thread_wait_func
, chr
)) {
2362 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2366 dwMode
|= ENABLE_LINE_INPUT
;
2369 /* set the terminal in raw mode */
2370 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2371 dwMode
|= ENABLE_PROCESSED_INPUT
;
2374 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2376 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2377 qemu_chr_fe_set_echo(chr
, false);
2381 #endif /* !_WIN32 */
2384 /***********************************************************/
2385 /* UDP Net console */
2390 uint8_t buf
[READ_BUF_LEN
];
2396 /* Called with chr_write_lock held. */
2397 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2399 NetCharDriver
*s
= chr
->opaque
;
2400 gsize bytes_written
;
2403 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2404 if (status
== G_IO_STATUS_EOF
) {
2406 } else if (status
!= G_IO_STATUS_NORMAL
) {
2410 return bytes_written
;
2413 static int udp_chr_read_poll(void *opaque
)
2415 CharDriverState
*chr
= opaque
;
2416 NetCharDriver
*s
= chr
->opaque
;
2418 s
->max_size
= qemu_chr_be_can_write(chr
);
2420 /* If there were any stray characters in the queue process them
2423 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2424 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2426 s
->max_size
= qemu_chr_be_can_write(chr
);
2431 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2433 CharDriverState
*chr
= opaque
;
2434 NetCharDriver
*s
= chr
->opaque
;
2435 gsize bytes_read
= 0;
2438 if (s
->max_size
== 0) {
2441 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2443 s
->bufcnt
= bytes_read
;
2444 s
->bufptr
= s
->bufcnt
;
2445 if (status
!= G_IO_STATUS_NORMAL
) {
2446 remove_fd_in_watch(chr
);
2451 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2452 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2454 s
->max_size
= qemu_chr_be_can_write(chr
);
2460 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2462 NetCharDriver
*s
= chr
->opaque
;
2464 remove_fd_in_watch(chr
);
2466 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2471 static void udp_chr_close(CharDriverState
*chr
)
2473 NetCharDriver
*s
= chr
->opaque
;
2475 remove_fd_in_watch(chr
);
2477 g_io_channel_unref(s
->chan
);
2481 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2484 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2486 CharDriverState
*chr
= NULL
;
2487 NetCharDriver
*s
= NULL
;
2489 chr
= qemu_chr_alloc();
2490 s
= g_new0(NetCharDriver
, 1);
2493 s
->chan
= io_channel_from_socket(s
->fd
);
2497 chr
->chr_write
= udp_chr_write
;
2498 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2499 chr
->chr_close
= udp_chr_close
;
2500 /* be isn't opened until we get a connection */
2501 chr
->explicit_be_open
= true;
2505 /***********************************************************/
2506 /* TCP Net console */
2510 GIOChannel
*chan
, *listen_chan
;
2519 int read_msgfds_num
;
2521 int write_msgfds_num
;
2523 SocketAddress
*addr
;
2527 guint reconnect_timer
;
2528 int64_t reconnect_time
;
2529 bool connect_err_reported
;
2532 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2534 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2536 TCPCharDriver
*s
= chr
->opaque
;
2537 assert(s
->connected
== 0);
2538 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2539 socket_reconnect_timeout
, chr
);
2542 static void check_report_connect_error(CharDriverState
*chr
,
2545 TCPCharDriver
*s
= chr
->opaque
;
2547 if (!s
->connect_err_reported
) {
2548 error_report("Unable to connect character device %s: %s",
2549 chr
->label
, error_get_pretty(err
));
2550 s
->connect_err_reported
= true;
2552 qemu_chr_socket_restart_timer(chr
);
2555 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2558 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2560 TCPCharDriver
*s
= chr
->opaque
;
2565 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2566 char control
[CMSG_SPACE(fd_size
)];
2567 struct cmsghdr
*cmsg
;
2569 memset(&msgh
, 0, sizeof(msgh
));
2570 memset(control
, 0, sizeof(control
));
2572 /* set the payload */
2573 iov
.iov_base
= (uint8_t *) buf
;
2576 msgh
.msg_iov
= &iov
;
2577 msgh
.msg_iovlen
= 1;
2579 msgh
.msg_control
= control
;
2580 msgh
.msg_controllen
= sizeof(control
);
2582 cmsg
= CMSG_FIRSTHDR(&msgh
);
2584 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2585 cmsg
->cmsg_level
= SOL_SOCKET
;
2586 cmsg
->cmsg_type
= SCM_RIGHTS
;
2587 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2590 r
= sendmsg(s
->fd
, &msgh
, 0);
2591 } while (r
< 0 && errno
== EINTR
);
2593 /* free the written msgfds, no matter what */
2594 if (s
->write_msgfds_num
) {
2595 g_free(s
->write_msgfds
);
2596 s
->write_msgfds
= 0;
2597 s
->write_msgfds_num
= 0;
2604 /* Called with chr_write_lock held. */
2605 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2607 TCPCharDriver
*s
= chr
->opaque
;
2610 if (s
->is_unix
&& s
->write_msgfds_num
) {
2611 return unix_send_msgfds(chr
, buf
, len
);
2615 return io_channel_send(s
->chan
, buf
, len
);
2618 /* XXX: indicate an error ? */
2623 static int tcp_chr_read_poll(void *opaque
)
2625 CharDriverState
*chr
= opaque
;
2626 TCPCharDriver
*s
= chr
->opaque
;
2629 s
->max_size
= qemu_chr_be_can_write(chr
);
2634 #define IAC_BREAK 243
2635 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2637 uint8_t *buf
, int *size
)
2639 /* Handle any telnet client's basic IAC options to satisfy char by
2640 * char mode with no echo. All IAC options will be removed from
2641 * the buf and the do_telnetopt variable will be used to track the
2642 * state of the width of the IAC information.
2644 * IAC commands come in sets of 3 bytes with the exception of the
2645 * "IAC BREAK" command and the double IAC.
2651 for (i
= 0; i
< *size
; i
++) {
2652 if (s
->do_telnetopt
> 1) {
2653 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2654 /* Double IAC means send an IAC */
2658 s
->do_telnetopt
= 1;
2660 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2661 /* Handle IAC break commands by sending a serial break */
2662 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2667 if (s
->do_telnetopt
>= 4) {
2668 s
->do_telnetopt
= 1;
2671 if ((unsigned char)buf
[i
] == IAC
) {
2672 s
->do_telnetopt
= 2;
2683 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2685 TCPCharDriver
*s
= chr
->opaque
;
2686 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2688 assert(num
<= TCP_MAX_FDS
);
2693 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2695 /* Close unused fds */
2696 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2697 close(s
->read_msgfds
[i
]);
2700 g_free(s
->read_msgfds
);
2702 s
->read_msgfds_num
= 0;
2708 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2710 TCPCharDriver
*s
= chr
->opaque
;
2712 /* clear old pending fd array */
2713 g_free(s
->write_msgfds
);
2716 s
->write_msgfds
= g_new(int, num
);
2717 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2720 s
->write_msgfds_num
= num
;
2726 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2728 TCPCharDriver
*s
= chr
->opaque
;
2729 struct cmsghdr
*cmsg
;
2731 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2734 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2735 cmsg
->cmsg_level
!= SOL_SOCKET
||
2736 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2740 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2746 /* close and clean read_msgfds */
2747 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2748 close(s
->read_msgfds
[i
]);
2751 if (s
->read_msgfds_num
) {
2752 g_free(s
->read_msgfds
);
2755 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2756 s
->read_msgfds
= g_malloc(fd_size
);
2757 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2759 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2760 int fd
= s
->read_msgfds
[i
];
2765 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2768 #ifndef MSG_CMSG_CLOEXEC
2769 qemu_set_cloexec(fd
);
2775 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2777 TCPCharDriver
*s
= chr
->opaque
;
2778 struct msghdr msg
= { NULL
, };
2779 struct iovec iov
[1];
2781 struct cmsghdr cmsg
;
2782 char control
[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS
)];
2787 iov
[0].iov_base
= buf
;
2788 iov
[0].iov_len
= len
;
2792 msg
.msg_control
= &msg_control
;
2793 msg
.msg_controllen
= sizeof(msg_control
);
2795 #ifdef MSG_CMSG_CLOEXEC
2796 flags
|= MSG_CMSG_CLOEXEC
;
2799 ret
= recvmsg(s
->fd
, &msg
, flags
);
2800 } while (ret
== -1 && errno
== EINTR
);
2802 if (ret
> 0 && s
->is_unix
) {
2803 unix_process_msgfd(chr
, &msg
);
2809 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2811 TCPCharDriver
*s
= chr
->opaque
;
2815 ret
= qemu_recv(s
->fd
, buf
, len
, 0);
2816 } while (ret
== -1 && socket_error() == EINTR
);
2822 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2824 TCPCharDriver
*s
= chr
->opaque
;
2825 return g_io_create_watch(s
->chan
, cond
);
2828 static void tcp_chr_disconnect(CharDriverState
*chr
)
2830 TCPCharDriver
*s
= chr
->opaque
;
2833 if (s
->listen_chan
) {
2834 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2835 tcp_chr_accept
, chr
);
2837 remove_fd_in_watch(chr
);
2838 g_io_channel_unref(s
->chan
);
2842 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2843 "disconnected:", s
->addr
, s
->is_listen
, s
->is_telnet
);
2844 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2845 if (s
->reconnect_time
) {
2846 qemu_chr_socket_restart_timer(chr
);
2850 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2852 CharDriverState
*chr
= opaque
;
2853 TCPCharDriver
*s
= chr
->opaque
;
2854 uint8_t buf
[READ_BUF_LEN
];
2857 if (!s
->connected
|| s
->max_size
<= 0) {
2861 if (len
> s
->max_size
)
2863 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2866 socket_error() != EAGAIN
&& socket_error() != EWOULDBLOCK
)) {
2867 /* connection closed */
2868 tcp_chr_disconnect(chr
);
2869 } else if (size
> 0) {
2870 if (s
->do_telnetopt
)
2871 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2873 qemu_chr_be_write(chr
, buf
, size
);
2879 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2881 TCPCharDriver
*s
= chr
->opaque
;
2884 if (!s
->connected
) {
2888 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2890 /* connection closed */
2891 tcp_chr_disconnect(chr
);
2898 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2900 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2903 chr
->avail_connections
= 1;
2910 static void tcp_chr_connect(void *opaque
)
2912 CharDriverState
*chr
= opaque
;
2913 TCPCharDriver
*s
= chr
->opaque
;
2914 struct sockaddr_storage ss
, ps
;
2915 socklen_t ss_len
= sizeof(ss
), ps_len
= sizeof(ps
);
2917 memset(&ss
, 0, ss_len
);
2918 if (getsockname(s
->fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2919 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2920 "Error in getsockname: %s\n", strerror(errno
));
2921 } else if (getpeername(s
->fd
, (struct sockaddr
*) &ps
, &ps_len
) != 0) {
2922 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2923 "Error in getpeername: %s\n", strerror(errno
));
2925 sockaddr_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2926 &ss
, ss_len
, &ps
, ps_len
,
2927 s
->is_listen
, s
->is_telnet
);
2932 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2935 qemu_chr_be_generic_open(chr
);
2938 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2940 TCPCharDriver
*s
= chr
->opaque
;
2942 remove_fd_in_watch(chr
);
2944 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2949 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2950 static void tcp_chr_telnet_init(int fd
)
2953 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2954 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2955 send(fd
, (char *)buf
, 3, 0);
2956 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2957 send(fd
, (char *)buf
, 3, 0);
2958 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2959 send(fd
, (char *)buf
, 3, 0);
2960 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2961 send(fd
, (char *)buf
, 3, 0);
2964 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2966 TCPCharDriver
*s
= chr
->opaque
;
2970 qemu_set_nonblock(fd
);
2972 socket_set_nodelay(fd
);
2974 s
->chan
= io_channel_from_socket(fd
);
2975 if (s
->listen_tag
) {
2976 g_source_remove(s
->listen_tag
);
2979 tcp_chr_connect(chr
);
2984 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2986 CharDriverState
*chr
= opaque
;
2987 TCPCharDriver
*s
= chr
->opaque
;
2988 struct sockaddr_in saddr
;
2990 struct sockaddr_un uaddr
;
2992 struct sockaddr
*addr
;
2999 len
= sizeof(uaddr
);
3000 addr
= (struct sockaddr
*)&uaddr
;
3004 len
= sizeof(saddr
);
3005 addr
= (struct sockaddr
*)&saddr
;
3007 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
3008 if (fd
< 0 && errno
!= EINTR
) {
3011 } else if (fd
>= 0) {
3012 if (s
->do_telnetopt
)
3013 tcp_chr_telnet_init(fd
);
3017 if (tcp_chr_add_client(chr
, fd
) < 0)
3023 static void tcp_chr_close(CharDriverState
*chr
)
3025 TCPCharDriver
*s
= chr
->opaque
;
3028 if (s
->reconnect_timer
) {
3029 g_source_remove(s
->reconnect_timer
);
3030 s
->reconnect_timer
= 0;
3032 qapi_free_SocketAddress(s
->addr
);
3034 remove_fd_in_watch(chr
);
3036 g_io_channel_unref(s
->chan
);
3040 if (s
->listen_fd
>= 0) {
3041 if (s
->listen_tag
) {
3042 g_source_remove(s
->listen_tag
);
3045 if (s
->listen_chan
) {
3046 g_io_channel_unref(s
->listen_chan
);
3048 closesocket(s
->listen_fd
);
3050 if (s
->read_msgfds_num
) {
3051 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3052 close(s
->read_msgfds
[i
]);
3054 g_free(s
->read_msgfds
);
3056 if (s
->write_msgfds_num
) {
3057 g_free(s
->write_msgfds
);
3060 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3063 static void qemu_chr_finish_socket_connection(CharDriverState
*chr
, int fd
)
3065 TCPCharDriver
*s
= chr
->opaque
;
3069 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
3070 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
3071 tcp_chr_accept
, chr
);
3075 socket_set_nodelay(fd
);
3076 s
->chan
= io_channel_from_socket(s
->fd
);
3077 tcp_chr_connect(chr
);
3081 static void qemu_chr_socket_connected(int fd
, Error
*err
, void *opaque
)
3083 CharDriverState
*chr
= opaque
;
3084 TCPCharDriver
*s
= chr
->opaque
;
3087 check_report_connect_error(chr
, err
);
3091 s
->connect_err_reported
= false;
3092 qemu_chr_finish_socket_connection(chr
, fd
);
3095 static bool qemu_chr_open_socket_fd(CharDriverState
*chr
, Error
**errp
)
3097 TCPCharDriver
*s
= chr
->opaque
;
3101 fd
= socket_listen(s
->addr
, errp
);
3102 } else if (s
->reconnect_time
) {
3103 fd
= socket_connect(s
->addr
, errp
, qemu_chr_socket_connected
, chr
);
3106 fd
= socket_connect(s
->addr
, errp
, NULL
, NULL
);
3112 qemu_chr_finish_socket_connection(chr
, fd
);
3116 /*********************************************************/
3117 /* Ring buffer chardev */
3124 } RingBufCharDriver
;
3126 static size_t ringbuf_count(const CharDriverState
*chr
)
3128 const RingBufCharDriver
*d
= chr
->opaque
;
3130 return d
->prod
- d
->cons
;
3133 /* Called with chr_write_lock held. */
3134 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3136 RingBufCharDriver
*d
= chr
->opaque
;
3139 if (!buf
|| (len
< 0)) {
3143 for (i
= 0; i
< len
; i
++ ) {
3144 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3145 if (d
->prod
- d
->cons
> d
->size
) {
3146 d
->cons
= d
->prod
- d
->size
;
3153 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3155 RingBufCharDriver
*d
= chr
->opaque
;
3158 qemu_mutex_lock(&chr
->chr_write_lock
);
3159 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3160 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3162 qemu_mutex_unlock(&chr
->chr_write_lock
);
3167 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3169 RingBufCharDriver
*d
= chr
->opaque
;
3176 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3179 CharDriverState
*chr
;
3180 RingBufCharDriver
*d
;
3182 chr
= qemu_chr_alloc();
3183 d
= g_malloc(sizeof(*d
));
3185 d
->size
= opts
->has_size
? opts
->size
: 65536;
3187 /* The size must be power of 2 */
3188 if (d
->size
& (d
->size
- 1)) {
3189 error_setg(errp
, "size of ringbuf chardev must be power of two");
3195 d
->cbuf
= g_malloc0(d
->size
);
3198 chr
->chr_write
= ringbuf_chr_write
;
3199 chr
->chr_close
= ringbuf_chr_close
;
3209 bool chr_is_ringbuf(const CharDriverState
*chr
)
3211 return chr
->chr_write
== ringbuf_chr_write
;
3214 void qmp_ringbuf_write(const char *device
, const char *data
,
3215 bool has_format
, enum DataFormat format
,
3218 CharDriverState
*chr
;
3219 const uint8_t *write_data
;
3223 chr
= qemu_chr_find(device
);
3225 error_setg(errp
, "Device '%s' not found", device
);
3229 if (!chr_is_ringbuf(chr
)) {
3230 error_setg(errp
,"%s is not a ringbuf device", device
);
3234 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3235 write_data
= g_base64_decode(data
, &write_count
);
3237 write_data
= (uint8_t *)data
;
3238 write_count
= strlen(data
);
3241 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3243 if (write_data
!= (uint8_t *)data
) {
3244 g_free((void *)write_data
);
3248 error_setg(errp
, "Failed to write to device %s", device
);
3253 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3254 bool has_format
, enum DataFormat format
,
3257 CharDriverState
*chr
;
3262 chr
= qemu_chr_find(device
);
3264 error_setg(errp
, "Device '%s' not found", device
);
3268 if (!chr_is_ringbuf(chr
)) {
3269 error_setg(errp
,"%s is not a ringbuf device", device
);
3274 error_setg(errp
, "size must be greater than zero");
3278 count
= ringbuf_count(chr
);
3279 size
= size
> count
? count
: size
;
3280 read_data
= g_malloc(size
+ 1);
3282 ringbuf_chr_read(chr
, read_data
, size
);
3284 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3285 data
= g_base64_encode(read_data
, size
);
3289 * FIXME should read only complete, valid UTF-8 characters up
3290 * to @size bytes. Invalid sequences should be replaced by a
3291 * suitable replacement character. Except when (and only
3292 * when) ring buffer lost characters since last read, initial
3293 * continuation characters should be dropped.
3295 read_data
[size
] = 0;
3296 data
= (char *)read_data
;
3302 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3304 char host
[65], port
[33], width
[8], height
[8];
3308 Error
*local_err
= NULL
;
3310 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3312 error_report_err(local_err
);
3316 if (strstart(filename
, "mon:", &p
)) {
3318 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3319 if (strcmp(filename
, "stdio") == 0) {
3320 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3321 * but pass it to the guest. Handle this only for compat syntax,
3322 * for -chardev syntax we have special option for this.
3323 * This is what -nographic did, redirecting+muxing serial+monitor
3324 * to stdio causing Ctrl+C to be passed to guest. */
3325 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3329 if (strcmp(filename
, "null") == 0 ||
3330 strcmp(filename
, "pty") == 0 ||
3331 strcmp(filename
, "msmouse") == 0 ||
3332 strcmp(filename
, "braille") == 0 ||
3333 strcmp(filename
, "testdev") == 0 ||
3334 strcmp(filename
, "stdio") == 0) {
3335 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3338 if (strstart(filename
, "vc", &p
)) {
3339 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3341 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3343 qemu_opt_set(opts
, "width", width
, &error_abort
);
3344 qemu_opt_set(opts
, "height", height
, &error_abort
);
3345 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3347 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3348 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3355 if (strcmp(filename
, "con:") == 0) {
3356 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3359 if (strstart(filename
, "COM", NULL
)) {
3360 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3361 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3364 if (strstart(filename
, "file:", &p
)) {
3365 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3366 qemu_opt_set(opts
, "path", p
, &error_abort
);
3369 if (strstart(filename
, "pipe:", &p
)) {
3370 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3371 qemu_opt_set(opts
, "path", p
, &error_abort
);
3374 if (strstart(filename
, "tcp:", &p
) ||
3375 strstart(filename
, "telnet:", &p
)) {
3376 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3378 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3381 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3382 qemu_opt_set(opts
, "host", host
, &error_abort
);
3383 qemu_opt_set(opts
, "port", port
, &error_abort
);
3384 if (p
[pos
] == ',') {
3385 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3387 error_report_err(local_err
);
3391 if (strstart(filename
, "telnet:", &p
))
3392 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3395 if (strstart(filename
, "udp:", &p
)) {
3396 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3397 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3399 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3403 qemu_opt_set(opts
, "host", host
, &error_abort
);
3404 qemu_opt_set(opts
, "port", port
, &error_abort
);
3405 if (p
[pos
] == '@') {
3407 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3409 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3413 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3414 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3418 if (strstart(filename
, "unix:", &p
)) {
3419 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3420 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3422 error_report_err(local_err
);
3427 if (strstart(filename
, "/dev/parport", NULL
) ||
3428 strstart(filename
, "/dev/ppi", NULL
)) {
3429 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3430 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3433 if (strstart(filename
, "/dev/", NULL
)) {
3434 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3435 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3440 qemu_opts_del(opts
);
3444 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3447 const char *path
= qemu_opt_get(opts
, "path");
3450 error_setg(errp
, "chardev: file: no filename given");
3453 backend
->file
= g_new0(ChardevFile
, 1);
3454 backend
->file
->out
= g_strdup(path
);
3457 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3460 backend
->stdio
= g_new0(ChardevStdio
, 1);
3461 backend
->stdio
->has_signal
= true;
3462 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3465 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3468 const char *device
= qemu_opt_get(opts
, "path");
3470 if (device
== NULL
) {
3471 error_setg(errp
, "chardev: serial/tty: no device path given");
3474 backend
->serial
= g_new0(ChardevHostdev
, 1);
3475 backend
->serial
->device
= g_strdup(device
);
3478 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3481 const char *device
= qemu_opt_get(opts
, "path");
3483 if (device
== NULL
) {
3484 error_setg(errp
, "chardev: parallel: no device path given");
3487 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3488 backend
->parallel
->device
= g_strdup(device
);
3491 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3494 const char *device
= qemu_opt_get(opts
, "path");
3496 if (device
== NULL
) {
3497 error_setg(errp
, "chardev: pipe: no device path given");
3500 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3501 backend
->pipe
->device
= g_strdup(device
);
3504 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3509 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3511 val
= qemu_opt_get_size(opts
, "size", 0);
3513 backend
->ringbuf
->has_size
= true;
3514 backend
->ringbuf
->size
= val
;
3518 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3521 const char *chardev
= qemu_opt_get(opts
, "chardev");
3523 if (chardev
== NULL
) {
3524 error_setg(errp
, "chardev: mux: no chardev given");
3527 backend
->mux
= g_new0(ChardevMux
, 1);
3528 backend
->mux
->chardev
= g_strdup(chardev
);
3531 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3534 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3535 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3536 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3537 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3538 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3539 const char *path
= qemu_opt_get(opts
, "path");
3540 const char *host
= qemu_opt_get(opts
, "host");
3541 const char *port
= qemu_opt_get(opts
, "port");
3542 SocketAddress
*addr
;
3546 error_setg(errp
, "chardev: socket: no host given");
3550 error_setg(errp
, "chardev: socket: no port given");
3555 backend
->socket
= g_new0(ChardevSocket
, 1);
3557 backend
->socket
->has_nodelay
= true;
3558 backend
->socket
->nodelay
= do_nodelay
;
3559 backend
->socket
->has_server
= true;
3560 backend
->socket
->server
= is_listen
;
3561 backend
->socket
->has_telnet
= true;
3562 backend
->socket
->telnet
= is_telnet
;
3563 backend
->socket
->has_wait
= true;
3564 backend
->socket
->wait
= is_waitconnect
;
3565 backend
->socket
->has_reconnect
= true;
3566 backend
->socket
->reconnect
= reconnect
;
3568 addr
= g_new0(SocketAddress
, 1);
3570 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
3571 addr
->q_unix
= g_new0(UnixSocketAddress
, 1);
3572 addr
->q_unix
->path
= g_strdup(path
);
3574 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3575 addr
->inet
= g_new0(InetSocketAddress
, 1);
3576 addr
->inet
->host
= g_strdup(host
);
3577 addr
->inet
->port
= g_strdup(port
);
3578 addr
->inet
->has_to
= qemu_opt_get(opts
, "to");
3579 addr
->inet
->to
= qemu_opt_get_number(opts
, "to", 0);
3580 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3581 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3582 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3583 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3585 backend
->socket
->addr
= addr
;
3588 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
3591 const char *host
= qemu_opt_get(opts
, "host");
3592 const char *port
= qemu_opt_get(opts
, "port");
3593 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
3594 const char *localport
= qemu_opt_get(opts
, "localport");
3595 bool has_local
= false;
3596 SocketAddress
*addr
;
3598 if (host
== NULL
|| strlen(host
) == 0) {
3601 if (port
== NULL
|| strlen(port
) == 0) {
3602 error_setg(errp
, "chardev: udp: remote port not specified");
3605 if (localport
== NULL
|| strlen(localport
) == 0) {
3610 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
3616 backend
->udp
= g_new0(ChardevUdp
, 1);
3618 addr
= g_new0(SocketAddress
, 1);
3619 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3620 addr
->inet
= g_new0(InetSocketAddress
, 1);
3621 addr
->inet
->host
= g_strdup(host
);
3622 addr
->inet
->port
= g_strdup(port
);
3623 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3624 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3625 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3626 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3627 backend
->udp
->remote
= addr
;
3630 backend
->udp
->has_local
= true;
3631 addr
= g_new0(SocketAddress
, 1);
3632 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3633 addr
->inet
= g_new0(InetSocketAddress
, 1);
3634 addr
->inet
->host
= g_strdup(localaddr
);
3635 addr
->inet
->port
= g_strdup(localport
);
3636 backend
->udp
->local
= addr
;
3640 typedef struct CharDriver
{
3642 ChardevBackendKind kind
;
3643 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3646 static GSList
*backends
;
3648 void register_char_driver(const char *name
, ChardevBackendKind kind
,
3649 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3653 s
= g_malloc0(sizeof(*s
));
3654 s
->name
= g_strdup(name
);
3658 backends
= g_slist_append(backends
, s
);
3661 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3662 void (*init
)(struct CharDriverState
*s
),
3665 Error
*local_err
= NULL
;
3667 CharDriverState
*chr
;
3669 ChardevReturn
*ret
= NULL
;
3670 ChardevBackend
*backend
;
3671 const char *id
= qemu_opts_id(opts
);
3675 error_setg(errp
, "chardev: no id specified");
3679 if (qemu_opt_get(opts
, "backend") == NULL
) {
3680 error_setg(errp
, "chardev: \"%s\" missing backend",
3681 qemu_opts_id(opts
));
3684 for (i
= backends
; i
; i
= i
->next
) {
3687 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3692 error_setg(errp
, "chardev: backend \"%s\" not found",
3693 qemu_opt_get(opts
, "backend"));
3697 backend
= g_new0(ChardevBackend
, 1);
3699 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3700 bid
= g_strdup_printf("%s-base", id
);
3704 backend
->kind
= cd
->kind
;
3706 cd
->parse(opts
, backend
, &local_err
);
3708 error_propagate(errp
, local_err
);
3712 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3718 qapi_free_ChardevBackend(backend
);
3719 qapi_free_ChardevReturn(ret
);
3720 backend
= g_new0(ChardevBackend
, 1);
3721 backend
->mux
= g_new0(ChardevMux
, 1);
3722 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3723 backend
->mux
->chardev
= g_strdup(bid
);
3724 ret
= qmp_chardev_add(id
, backend
, errp
);
3726 chr
= qemu_chr_find(bid
);
3727 qemu_chr_delete(chr
);
3733 chr
= qemu_chr_find(id
);
3737 qapi_free_ChardevBackend(backend
);
3738 qapi_free_ChardevReturn(ret
);
3743 qemu_opts_del(opts
);
3747 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3750 CharDriverState
*chr
;
3754 if (strstart(filename
, "chardev:", &p
)) {
3755 return qemu_chr_find(p
);
3758 opts
= qemu_chr_parse_compat(label
, filename
);
3762 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3764 error_report_err(err
);
3766 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3767 qemu_chr_fe_claim_no_fail(chr
);
3768 monitor_init(chr
, MONITOR_USE_READLINE
);
3773 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3775 if (chr
->chr_set_echo
) {
3776 chr
->chr_set_echo(chr
, echo
);
3780 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3782 if (chr
->fe_open
== fe_open
) {
3785 chr
->fe_open
= fe_open
;
3786 if (chr
->chr_set_fe_open
) {
3787 chr
->chr_set_fe_open(chr
, fe_open
);
3791 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3793 if (chr
->chr_fe_event
) {
3794 chr
->chr_fe_event(chr
, event
);
3798 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3799 GIOFunc func
, void *user_data
)
3804 if (s
->chr_add_watch
== NULL
) {
3808 src
= s
->chr_add_watch(s
, cond
);
3813 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3814 tag
= g_source_attach(src
, NULL
);
3815 g_source_unref(src
);
3820 int qemu_chr_fe_claim(CharDriverState
*s
)
3822 if (s
->avail_connections
< 1) {
3825 s
->avail_connections
--;
3829 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3831 if (qemu_chr_fe_claim(s
) != 0) {
3832 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3833 __func__
, s
->label
);
3838 void qemu_chr_fe_release(CharDriverState
*s
)
3840 s
->avail_connections
++;
3843 void qemu_chr_delete(CharDriverState
*chr
)
3845 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3846 if (chr
->chr_close
) {
3847 chr
->chr_close(chr
);
3849 g_free(chr
->filename
);
3851 qemu_opts_del(chr
->opts
);
3855 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3857 ChardevInfoList
*chr_list
= NULL
;
3858 CharDriverState
*chr
;
3860 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3861 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3862 info
->value
= g_malloc0(sizeof(*info
->value
));
3863 info
->value
->label
= g_strdup(chr
->label
);
3864 info
->value
->filename
= g_strdup(chr
->filename
);
3865 info
->value
->frontend_open
= chr
->fe_open
;
3867 info
->next
= chr_list
;
3874 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3876 ChardevBackendInfoList
*backend_list
= NULL
;
3877 CharDriver
*c
= NULL
;
3880 for (i
= backends
; i
; i
= i
->next
) {
3881 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3883 info
->value
= g_malloc0(sizeof(*info
->value
));
3884 info
->value
->name
= g_strdup(c
->name
);
3886 info
->next
= backend_list
;
3887 backend_list
= info
;
3890 return backend_list
;
3893 CharDriverState
*qemu_chr_find(const char *name
)
3895 CharDriverState
*chr
;
3897 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3898 if (strcmp(chr
->label
, name
) != 0)
3905 /* Get a character (serial) device interface. */
3906 CharDriverState
*qemu_char_get_next_serial(void)
3908 static int next_serial
;
3909 CharDriverState
*chr
;
3911 /* FIXME: This function needs to go away: use chardev properties! */
3913 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3914 chr
= serial_hds
[next_serial
++];
3915 qemu_chr_fe_claim_no_fail(chr
);
3921 QemuOptsList qemu_chardev_opts
= {
3923 .implied_opt_name
= "backend",
3924 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3928 .type
= QEMU_OPT_STRING
,
3931 .type
= QEMU_OPT_STRING
,
3934 .type
= QEMU_OPT_STRING
,
3937 .type
= QEMU_OPT_STRING
,
3939 .name
= "localaddr",
3940 .type
= QEMU_OPT_STRING
,
3942 .name
= "localport",
3943 .type
= QEMU_OPT_STRING
,
3946 .type
= QEMU_OPT_NUMBER
,
3949 .type
= QEMU_OPT_BOOL
,
3952 .type
= QEMU_OPT_BOOL
,
3955 .type
= QEMU_OPT_BOOL
,
3958 .type
= QEMU_OPT_BOOL
,
3961 .type
= QEMU_OPT_BOOL
,
3963 .name
= "reconnect",
3964 .type
= QEMU_OPT_NUMBER
,
3967 .type
= QEMU_OPT_BOOL
,
3970 .type
= QEMU_OPT_NUMBER
,
3973 .type
= QEMU_OPT_NUMBER
,
3976 .type
= QEMU_OPT_NUMBER
,
3979 .type
= QEMU_OPT_NUMBER
,
3982 .type
= QEMU_OPT_BOOL
,
3985 .type
= QEMU_OPT_BOOL
,
3988 .type
= QEMU_OPT_STRING
,
3991 .type
= QEMU_OPT_NUMBER
,
3994 .type
= QEMU_OPT_SIZE
,
3997 .type
= QEMU_OPT_STRING
,
3999 { /* end of list */ }
4005 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
4010 error_setg(errp
, "input file not supported");
4014 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
4015 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4016 if (out
== INVALID_HANDLE_VALUE
) {
4017 error_setg(errp
, "open %s failed", file
->out
);
4020 return qemu_chr_open_win_file(out
);
4023 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
4026 return qemu_chr_open_win_path(serial
->device
);
4029 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
4032 error_setg(errp
, "character device backend type 'parallel' not supported");
4038 static int qmp_chardev_open_file_source(char *src
, int flags
,
4043 TFR(fd
= qemu_open(src
, flags
, 0666));
4045 error_setg_file_open(errp
, errno
, src
);
4050 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
4052 int flags
, in
= -1, out
;
4054 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
4055 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4062 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4069 return qemu_chr_open_fd(in
, out
);
4072 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
4075 #ifdef HAVE_CHARDEV_TTY
4078 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4082 qemu_set_nonblock(fd
);
4083 return qemu_chr_open_tty_fd(fd
);
4085 error_setg(errp
, "character device backend type 'serial' not supported");
4090 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
4093 #ifdef HAVE_CHARDEV_PARPORT
4096 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4100 return qemu_chr_open_pp_fd(fd
);
4102 error_setg(errp
, "character device backend type 'parallel' not supported");
4109 static void socket_try_connect(CharDriverState
*chr
)
4113 if (!qemu_chr_open_socket_fd(chr
, &err
)) {
4114 check_report_connect_error(chr
, err
);
4118 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4120 CharDriverState
*chr
= opaque
;
4121 TCPCharDriver
*s
= chr
->opaque
;
4123 s
->reconnect_timer
= 0;
4129 socket_try_connect(chr
);
4134 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
4137 CharDriverState
*chr
;
4139 SocketAddress
*addr
= sock
->addr
;
4140 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4141 bool is_listen
= sock
->has_server
? sock
->server
: true;
4142 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4143 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4144 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4146 chr
= qemu_chr_alloc();
4147 s
= g_new0(TCPCharDriver
, 1);
4151 s
->is_unix
= addr
->kind
== SOCKET_ADDRESS_KIND_UNIX
;
4152 s
->is_listen
= is_listen
;
4153 s
->is_telnet
= is_telnet
;
4154 s
->do_nodelay
= do_nodelay
;
4155 qapi_copy_SocketAddress(&s
->addr
, sock
->addr
);
4158 chr
->chr_write
= tcp_chr_write
;
4159 chr
->chr_sync_read
= tcp_chr_sync_read
;
4160 chr
->chr_close
= tcp_chr_close
;
4161 chr
->get_msgfds
= tcp_get_msgfds
;
4162 chr
->set_msgfds
= tcp_set_msgfds
;
4163 chr
->chr_add_client
= tcp_chr_add_client
;
4164 chr
->chr_add_watch
= tcp_chr_add_watch
;
4165 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4166 /* be isn't opened until we get a connection */
4167 chr
->explicit_be_open
= true;
4169 chr
->filename
= g_malloc(CHR_MAX_FILENAME_SIZE
);
4170 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
, "disconnected:",
4171 addr
, is_listen
, is_telnet
);
4175 s
->do_telnetopt
= 1;
4177 } else if (reconnect
> 0) {
4178 s
->reconnect_time
= reconnect
;
4181 if (s
->reconnect_time
) {
4182 socket_try_connect(chr
);
4183 } else if (!qemu_chr_open_socket_fd(chr
, errp
)) {
4185 g_free(chr
->filename
);
4190 if (is_listen
&& is_waitconnect
) {
4191 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
4193 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
4194 qemu_set_nonblock(s
->listen_fd
);
4200 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
4205 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
4209 return qemu_chr_open_udp_fd(fd
);
4212 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4215 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4216 CharDriverState
*base
, *chr
= NULL
;
4218 chr
= qemu_chr_find(id
);
4220 error_setg(errp
, "Chardev '%s' already exists", id
);
4225 switch (backend
->kind
) {
4226 case CHARDEV_BACKEND_KIND_FILE
:
4227 chr
= qmp_chardev_open_file(backend
->file
, errp
);
4229 case CHARDEV_BACKEND_KIND_SERIAL
:
4230 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
4232 case CHARDEV_BACKEND_KIND_PARALLEL
:
4233 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
4235 case CHARDEV_BACKEND_KIND_PIPE
:
4236 chr
= qemu_chr_open_pipe(backend
->pipe
);
4238 case CHARDEV_BACKEND_KIND_SOCKET
:
4239 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
4241 case CHARDEV_BACKEND_KIND_UDP
:
4242 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
4244 #ifdef HAVE_CHARDEV_TTY
4245 case CHARDEV_BACKEND_KIND_PTY
:
4246 chr
= qemu_chr_open_pty(id
, ret
);
4249 case CHARDEV_BACKEND_KIND_NULL
:
4250 chr
= qemu_chr_open_null();
4252 case CHARDEV_BACKEND_KIND_MUX
:
4253 base
= qemu_chr_find(backend
->mux
->chardev
);
4255 error_setg(errp
, "mux: base chardev %s not found",
4256 backend
->mux
->chardev
);
4259 chr
= qemu_chr_open_mux(base
);
4261 case CHARDEV_BACKEND_KIND_MSMOUSE
:
4262 chr
= qemu_chr_open_msmouse();
4264 #ifdef CONFIG_BRLAPI
4265 case CHARDEV_BACKEND_KIND_BRAILLE
:
4266 chr
= chr_baum_init();
4269 case CHARDEV_BACKEND_KIND_TESTDEV
:
4270 chr
= chr_testdev_init();
4272 case CHARDEV_BACKEND_KIND_STDIO
:
4273 chr
= qemu_chr_open_stdio(backend
->stdio
);
4276 case CHARDEV_BACKEND_KIND_CONSOLE
:
4277 chr
= qemu_chr_open_win_con();
4281 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4282 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
4284 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4285 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
4288 case CHARDEV_BACKEND_KIND_VC
:
4289 chr
= vc_init(backend
->vc
);
4291 case CHARDEV_BACKEND_KIND_RINGBUF
:
4292 case CHARDEV_BACKEND_KIND_MEMORY
:
4293 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
4296 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4301 * Character backend open hasn't been fully converted to the Error
4302 * API. Some opens fail without setting an error. Set a generic
4304 * TODO full conversion to Error API
4306 if (chr
== NULL
&& errp
&& !*errp
) {
4307 error_setg(errp
, "Failed to create chardev");
4310 chr
->label
= g_strdup(id
);
4311 chr
->avail_connections
=
4312 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4313 if (!chr
->filename
) {
4314 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4316 if (!chr
->explicit_be_open
) {
4317 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4319 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4327 void qmp_chardev_remove(const char *id
, Error
**errp
)
4329 CharDriverState
*chr
;
4331 chr
= qemu_chr_find(id
);
4333 error_setg(errp
, "Chardev '%s' not found", id
);
4336 if (chr
->chr_can_read
|| chr
->chr_read
||
4337 chr
->chr_event
|| chr
->handler_opaque
) {
4338 error_setg(errp
, "Chardev '%s' is busy", id
);
4341 qemu_chr_delete(chr
);
4344 static void register_types(void)
4346 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
4347 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4348 qemu_chr_parse_socket
);
4349 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
);
4350 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4351 qemu_chr_parse_ringbuf
);
4352 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4353 qemu_chr_parse_file_out
);
4354 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4355 qemu_chr_parse_stdio
);
4356 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4357 qemu_chr_parse_serial
);
4358 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4359 qemu_chr_parse_serial
);
4360 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4361 qemu_chr_parse_parallel
);
4362 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4363 qemu_chr_parse_parallel
);
4364 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
4365 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
4366 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4367 qemu_chr_parse_pipe
);
4368 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
);
4369 /* Bug-compatibility: */
4370 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4371 qemu_chr_parse_ringbuf
);
4372 /* this must be done after machine init, since we register FEs with muxes
4373 * as part of realize functions like serial_isa_realizefn when -nographic
4376 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4379 type_init(register_types
);