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/timer.h"
28 #include "sysemu/char.h"
30 #include "qmp-commands.h"
31 #include "qapi/qmp-input-visitor.h"
32 #include "qapi/qmp-output-visitor.h"
33 #include "qapi-visit.h"
43 #include <sys/times.h>
47 #include <sys/ioctl.h>
48 #include <sys/resource.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #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_malloc0(sizeof(MuxDriver
));
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
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
810 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
811 g_source_attach(iwp
->src
, NULL
);
813 g_source_destroy(iwp
->src
);
814 g_source_unref(iwp
->src
);
820 static gboolean
io_watch_poll_check(GSource
*source
)
825 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
831 static void io_watch_poll_finalize(GSource
*source
)
833 /* Due to a glib bug, removing the last reference to a source
834 * inside a finalize callback causes recursive locking (and a
835 * deadlock). This is not a problem inside other callbacks,
836 * including dispatch callbacks, so we call io_remove_watch_poll
837 * to remove this source. At this point, iwp->src must
838 * be NULL, or we would leak it.
840 * This would be solved much more elegantly by child sources,
841 * but we support older glib versions that do not have them.
843 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
844 assert(iwp
->src
== NULL
);
847 static GSourceFuncs io_watch_poll_funcs
= {
848 .prepare
= io_watch_poll_prepare
,
849 .check
= io_watch_poll_check
,
850 .dispatch
= io_watch_poll_dispatch
,
851 .finalize
= io_watch_poll_finalize
,
854 /* Can only be used for read */
855 static guint
io_add_watch_poll(GIOChannel
*channel
,
856 IOCanReadHandler
*fd_can_read
,
863 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
864 iwp
->fd_can_read
= fd_can_read
;
865 iwp
->opaque
= user_data
;
866 iwp
->channel
= channel
;
867 iwp
->fd_read
= (GSourceFunc
) fd_read
;
870 tag
= g_source_attach(&iwp
->parent
, NULL
);
871 g_source_unref(&iwp
->parent
);
875 static void io_remove_watch_poll(guint tag
)
880 g_return_if_fail (tag
> 0);
882 source
= g_main_context_find_source_by_id(NULL
, tag
);
883 g_return_if_fail (source
!= NULL
);
885 iwp
= io_watch_poll_from_source(source
);
887 g_source_destroy(iwp
->src
);
888 g_source_unref(iwp
->src
);
891 g_source_destroy(&iwp
->parent
);
894 static void remove_fd_in_watch(CharDriverState
*chr
)
896 if (chr
->fd_in_tag
) {
897 io_remove_watch_poll(chr
->fd_in_tag
);
903 static GIOChannel
*io_channel_from_fd(int fd
)
911 chan
= g_io_channel_unix_new(fd
);
913 g_io_channel_set_encoding(chan
, NULL
, NULL
);
914 g_io_channel_set_buffered(chan
, FALSE
);
920 static GIOChannel
*io_channel_from_socket(int fd
)
929 chan
= g_io_channel_win32_new_socket(fd
);
931 chan
= g_io_channel_unix_new(fd
);
934 g_io_channel_set_encoding(chan
, NULL
, NULL
);
935 g_io_channel_set_buffered(chan
, FALSE
);
940 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
943 GIOStatus status
= G_IO_STATUS_NORMAL
;
945 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
946 gsize bytes_written
= 0;
948 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
949 &bytes_written
, NULL
);
950 offset
+= bytes_written
;
957 case G_IO_STATUS_NORMAL
:
960 case G_IO_STATUS_AGAIN
:
972 typedef struct FDCharDriver
{
973 CharDriverState
*chr
;
974 GIOChannel
*fd_in
, *fd_out
;
978 /* Called with chr_write_lock held. */
979 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
981 FDCharDriver
*s
= chr
->opaque
;
983 return io_channel_send(s
->fd_out
, buf
, len
);
986 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
988 CharDriverState
*chr
= opaque
;
989 FDCharDriver
*s
= chr
->opaque
;
991 uint8_t buf
[READ_BUF_LEN
];
996 if (len
> s
->max_size
) {
1003 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
1004 len
, &bytes_read
, NULL
);
1005 if (status
== G_IO_STATUS_EOF
) {
1006 remove_fd_in_watch(chr
);
1007 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1010 if (status
== G_IO_STATUS_NORMAL
) {
1011 qemu_chr_be_write(chr
, buf
, bytes_read
);
1017 static int fd_chr_read_poll(void *opaque
)
1019 CharDriverState
*chr
= opaque
;
1020 FDCharDriver
*s
= chr
->opaque
;
1022 s
->max_size
= qemu_chr_be_can_write(chr
);
1026 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1028 FDCharDriver
*s
= chr
->opaque
;
1029 return g_io_create_watch(s
->fd_out
, cond
);
1032 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1034 FDCharDriver
*s
= chr
->opaque
;
1036 remove_fd_in_watch(chr
);
1038 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
1043 static void fd_chr_close(struct CharDriverState
*chr
)
1045 FDCharDriver
*s
= chr
->opaque
;
1047 remove_fd_in_watch(chr
);
1049 g_io_channel_unref(s
->fd_in
);
1052 g_io_channel_unref(s
->fd_out
);
1056 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1059 /* open a character device to a unix fd */
1060 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1062 CharDriverState
*chr
;
1065 chr
= qemu_chr_alloc();
1066 s
= g_malloc0(sizeof(FDCharDriver
));
1067 s
->fd_in
= io_channel_from_fd(fd_in
);
1068 s
->fd_out
= io_channel_from_fd(fd_out
);
1069 qemu_set_nonblock(fd_out
);
1072 chr
->chr_add_watch
= fd_chr_add_watch
;
1073 chr
->chr_write
= fd_chr_write
;
1074 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1075 chr
->chr_close
= fd_chr_close
;
1080 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1083 char filename_in
[CHR_MAX_FILENAME_SIZE
];
1084 char filename_out
[CHR_MAX_FILENAME_SIZE
];
1085 const char *filename
= opts
->device
;
1087 if (filename
== NULL
) {
1088 fprintf(stderr
, "chardev: pipe: no filename given\n");
1092 snprintf(filename_in
, CHR_MAX_FILENAME_SIZE
, "%s.in", filename
);
1093 snprintf(filename_out
, CHR_MAX_FILENAME_SIZE
, "%s.out", filename
);
1094 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1095 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1096 if (fd_in
< 0 || fd_out
< 0) {
1101 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1106 return qemu_chr_open_fd(fd_in
, fd_out
);
1109 /* init terminal so that we can grab keys */
1110 static struct termios oldtty
;
1111 static int old_fd0_flags
;
1112 static bool stdio_in_use
;
1113 static bool stdio_allow_signal
;
1114 static bool stdio_echo_state
;
1116 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
);
1118 static void term_exit(void)
1120 tcsetattr (0, TCSANOW
, &oldtty
);
1121 fcntl(0, F_SETFL
, old_fd0_flags
);
1124 static void term_stdio_handler(int sig
)
1126 /* restore echo after resume from suspend. */
1127 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
1130 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1134 stdio_echo_state
= echo
;
1137 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1138 |INLCR
|IGNCR
|ICRNL
|IXON
);
1139 tty
.c_oflag
|= OPOST
;
1140 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1141 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1144 tty
.c_cc
[VTIME
] = 0;
1146 if (!stdio_allow_signal
)
1147 tty
.c_lflag
&= ~ISIG
;
1149 tcsetattr (0, TCSANOW
, &tty
);
1152 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1158 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
1160 CharDriverState
*chr
;
1161 struct sigaction act
;
1163 if (is_daemonized()) {
1164 error_report("cannot use stdio with -daemonize");
1169 error_report("cannot use stdio by multiple character devices");
1173 stdio_in_use
= true;
1174 old_fd0_flags
= fcntl(0, F_GETFL
);
1175 tcgetattr(0, &oldtty
);
1176 qemu_set_nonblock(0);
1179 memset(&act
, 0, sizeof(act
));
1180 act
.sa_handler
= term_stdio_handler
;
1181 sigaction(SIGCONT
, &act
, NULL
);
1183 chr
= qemu_chr_open_fd(0, 1);
1184 chr
->chr_close
= qemu_chr_close_stdio
;
1185 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1186 if (opts
->has_signal
) {
1187 stdio_allow_signal
= opts
->signal
;
1189 qemu_chr_fe_set_echo(chr
, false);
1194 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1195 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1196 || defined(__GLIBC__)
1198 #define HAVE_CHARDEV_TTY 1
1204 /* Protected by the CharDriverState chr_write_lock. */
1210 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1211 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1213 static gboolean
pty_chr_timer(gpointer opaque
)
1215 struct CharDriverState
*chr
= opaque
;
1216 PtyCharDriver
*s
= chr
->opaque
;
1218 qemu_mutex_lock(&chr
->chr_write_lock
);
1221 if (!s
->connected
) {
1223 pty_chr_update_read_handler_locked(chr
);
1225 qemu_mutex_unlock(&chr
->chr_write_lock
);
1229 /* Called with chr_write_lock held. */
1230 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1232 PtyCharDriver
*s
= chr
->opaque
;
1235 g_source_remove(s
->timer_tag
);
1240 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1242 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1246 /* Called with chr_write_lock held. */
1247 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1249 PtyCharDriver
*s
= chr
->opaque
;
1252 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1253 pfd
.events
= G_IO_OUT
;
1256 if (pfd
.revents
& G_IO_HUP
) {
1257 pty_chr_state(chr
, 0);
1259 pty_chr_state(chr
, 1);
1263 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1265 qemu_mutex_lock(&chr
->chr_write_lock
);
1266 pty_chr_update_read_handler_locked(chr
);
1267 qemu_mutex_unlock(&chr
->chr_write_lock
);
1270 /* Called with chr_write_lock held. */
1271 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1273 PtyCharDriver
*s
= chr
->opaque
;
1275 if (!s
->connected
) {
1276 /* guest sends data, check for (re-)connect */
1277 pty_chr_update_read_handler_locked(chr
);
1278 if (!s
->connected
) {
1282 return io_channel_send(s
->fd
, buf
, len
);
1285 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1287 PtyCharDriver
*s
= chr
->opaque
;
1288 if (!s
->connected
) {
1291 return g_io_create_watch(s
->fd
, cond
);
1294 static int pty_chr_read_poll(void *opaque
)
1296 CharDriverState
*chr
= opaque
;
1297 PtyCharDriver
*s
= chr
->opaque
;
1299 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1300 return s
->read_bytes
;
1303 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1305 CharDriverState
*chr
= opaque
;
1306 PtyCharDriver
*s
= chr
->opaque
;
1308 uint8_t buf
[READ_BUF_LEN
];
1312 if (len
> s
->read_bytes
)
1313 len
= s
->read_bytes
;
1317 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1318 if (status
!= G_IO_STATUS_NORMAL
) {
1319 pty_chr_state(chr
, 0);
1322 pty_chr_state(chr
, 1);
1323 qemu_chr_be_write(chr
, buf
, size
);
1328 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1330 CharDriverState
*chr
= opaque
;
1331 PtyCharDriver
*s
= chr
->opaque
;
1334 qemu_chr_be_generic_open(chr
);
1338 /* Called with chr_write_lock held. */
1339 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1341 PtyCharDriver
*s
= chr
->opaque
;
1345 g_source_remove(s
->open_tag
);
1348 remove_fd_in_watch(chr
);
1350 /* (re-)connect poll interval for idle guests: once per second.
1351 * We check more frequently in case the guests sends data to
1352 * the virtual device linked to our pty. */
1353 pty_chr_rearm_timer(chr
, 1000);
1356 g_source_remove(s
->timer_tag
);
1359 if (!s
->connected
) {
1360 g_assert(s
->open_tag
== 0);
1362 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1364 if (!chr
->fd_in_tag
) {
1365 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1371 static void pty_chr_close(struct CharDriverState
*chr
)
1373 PtyCharDriver
*s
= chr
->opaque
;
1376 qemu_mutex_lock(&chr
->chr_write_lock
);
1377 pty_chr_state(chr
, 0);
1378 fd
= g_io_channel_unix_get_fd(s
->fd
);
1379 g_io_channel_unref(s
->fd
);
1382 g_source_remove(s
->timer_tag
);
1385 qemu_mutex_unlock(&chr
->chr_write_lock
);
1387 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1390 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1393 CharDriverState
*chr
;
1395 int master_fd
, slave_fd
;
1396 char pty_name
[PATH_MAX
];
1398 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1399 if (master_fd
< 0) {
1404 qemu_set_nonblock(master_fd
);
1406 chr
= qemu_chr_alloc();
1408 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1409 ret
->pty
= g_strdup(pty_name
);
1410 ret
->has_pty
= true;
1412 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1415 s
= g_malloc0(sizeof(PtyCharDriver
));
1417 chr
->chr_write
= pty_chr_write
;
1418 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1419 chr
->chr_close
= pty_chr_close
;
1420 chr
->chr_add_watch
= pty_chr_add_watch
;
1421 chr
->explicit_be_open
= true;
1423 s
->fd
= io_channel_from_fd(master_fd
);
1429 static void tty_serial_init(int fd
, int speed
,
1430 int parity
, int data_bits
, int stop_bits
)
1436 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1437 speed
, parity
, data_bits
, stop_bits
);
1439 tcgetattr (fd
, &tty
);
1441 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1442 speed
= speed
* 10 / 11;
1459 /* Non-Posix values follow. They may be unsupported on some systems. */
1461 check_speed(115200);
1463 check_speed(230400);
1466 check_speed(460800);
1469 check_speed(500000);
1472 check_speed(576000);
1475 check_speed(921600);
1478 check_speed(1000000);
1481 check_speed(1152000);
1484 check_speed(1500000);
1487 check_speed(2000000);
1490 check_speed(2500000);
1493 check_speed(3000000);
1496 check_speed(3500000);
1499 check_speed(4000000);
1504 cfsetispeed(&tty
, spd
);
1505 cfsetospeed(&tty
, spd
);
1507 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1508 |INLCR
|IGNCR
|ICRNL
|IXON
);
1509 tty
.c_oflag
|= OPOST
;
1510 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1511 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1532 tty
.c_cflag
|= PARENB
;
1535 tty
.c_cflag
|= PARENB
| PARODD
;
1539 tty
.c_cflag
|= CSTOPB
;
1541 tcsetattr (fd
, TCSANOW
, &tty
);
1544 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1546 FDCharDriver
*s
= chr
->opaque
;
1549 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1551 QEMUSerialSetParams
*ssp
= arg
;
1552 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1553 ssp
->speed
, ssp
->parity
,
1554 ssp
->data_bits
, ssp
->stop_bits
);
1557 case CHR_IOCTL_SERIAL_SET_BREAK
:
1559 int enable
= *(int *)arg
;
1561 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1565 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1568 int *targ
= (int *)arg
;
1569 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1571 if (sarg
& TIOCM_CTS
)
1572 *targ
|= CHR_TIOCM_CTS
;
1573 if (sarg
& TIOCM_CAR
)
1574 *targ
|= CHR_TIOCM_CAR
;
1575 if (sarg
& TIOCM_DSR
)
1576 *targ
|= CHR_TIOCM_DSR
;
1577 if (sarg
& TIOCM_RI
)
1578 *targ
|= CHR_TIOCM_RI
;
1579 if (sarg
& TIOCM_DTR
)
1580 *targ
|= CHR_TIOCM_DTR
;
1581 if (sarg
& TIOCM_RTS
)
1582 *targ
|= CHR_TIOCM_RTS
;
1585 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1587 int sarg
= *(int *)arg
;
1589 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1590 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1591 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1592 if (sarg
& CHR_TIOCM_CTS
)
1594 if (sarg
& CHR_TIOCM_CAR
)
1596 if (sarg
& CHR_TIOCM_DSR
)
1598 if (sarg
& CHR_TIOCM_RI
)
1600 if (sarg
& CHR_TIOCM_DTR
)
1602 if (sarg
& CHR_TIOCM_RTS
)
1604 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1613 static void qemu_chr_close_tty(CharDriverState
*chr
)
1615 FDCharDriver
*s
= chr
->opaque
;
1619 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1629 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1631 CharDriverState
*chr
;
1633 tty_serial_init(fd
, 115200, 'N', 8, 1);
1634 chr
= qemu_chr_open_fd(fd
, fd
);
1635 chr
->chr_ioctl
= tty_serial_ioctl
;
1636 chr
->chr_close
= qemu_chr_close_tty
;
1639 #endif /* __linux__ || __sun__ */
1641 #if defined(__linux__)
1643 #define HAVE_CHARDEV_PARPORT 1
1648 } ParallelCharDriver
;
1650 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1652 if (s
->mode
!= mode
) {
1654 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1661 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1663 ParallelCharDriver
*drv
= chr
->opaque
;
1668 case CHR_IOCTL_PP_READ_DATA
:
1669 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1671 *(uint8_t *)arg
= b
;
1673 case CHR_IOCTL_PP_WRITE_DATA
:
1674 b
= *(uint8_t *)arg
;
1675 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1678 case CHR_IOCTL_PP_READ_CONTROL
:
1679 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1681 /* Linux gives only the lowest bits, and no way to know data
1682 direction! For better compatibility set the fixed upper
1684 *(uint8_t *)arg
= b
| 0xc0;
1686 case CHR_IOCTL_PP_WRITE_CONTROL
:
1687 b
= *(uint8_t *)arg
;
1688 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1691 case CHR_IOCTL_PP_READ_STATUS
:
1692 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1694 *(uint8_t *)arg
= b
;
1696 case CHR_IOCTL_PP_DATA_DIR
:
1697 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1700 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1701 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1702 struct ParallelIOArg
*parg
= arg
;
1703 int n
= read(fd
, parg
->buffer
, parg
->count
);
1704 if (n
!= parg
->count
) {
1709 case CHR_IOCTL_PP_EPP_READ
:
1710 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1711 struct ParallelIOArg
*parg
= arg
;
1712 int n
= read(fd
, parg
->buffer
, parg
->count
);
1713 if (n
!= parg
->count
) {
1718 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1719 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1720 struct ParallelIOArg
*parg
= arg
;
1721 int n
= write(fd
, parg
->buffer
, parg
->count
);
1722 if (n
!= parg
->count
) {
1727 case CHR_IOCTL_PP_EPP_WRITE
:
1728 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1729 struct ParallelIOArg
*parg
= arg
;
1730 int n
= write(fd
, parg
->buffer
, parg
->count
);
1731 if (n
!= parg
->count
) {
1742 static void pp_close(CharDriverState
*chr
)
1744 ParallelCharDriver
*drv
= chr
->opaque
;
1747 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1748 ioctl(fd
, PPRELEASE
);
1751 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1754 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1756 CharDriverState
*chr
;
1757 ParallelCharDriver
*drv
;
1759 if (ioctl(fd
, PPCLAIM
) < 0) {
1764 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1766 drv
->mode
= IEEE1284_MODE_COMPAT
;
1768 chr
= qemu_chr_alloc();
1769 chr
->chr_write
= null_chr_write
;
1770 chr
->chr_ioctl
= pp_ioctl
;
1771 chr
->chr_close
= pp_close
;
1776 #endif /* __linux__ */
1778 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1780 #define HAVE_CHARDEV_PARPORT 1
1782 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1784 int fd
= (int)(intptr_t)chr
->opaque
;
1788 case CHR_IOCTL_PP_READ_DATA
:
1789 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1791 *(uint8_t *)arg
= b
;
1793 case CHR_IOCTL_PP_WRITE_DATA
:
1794 b
= *(uint8_t *)arg
;
1795 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1798 case CHR_IOCTL_PP_READ_CONTROL
:
1799 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1801 *(uint8_t *)arg
= b
;
1803 case CHR_IOCTL_PP_WRITE_CONTROL
:
1804 b
= *(uint8_t *)arg
;
1805 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1808 case CHR_IOCTL_PP_READ_STATUS
:
1809 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1811 *(uint8_t *)arg
= b
;
1819 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1821 CharDriverState
*chr
;
1823 chr
= qemu_chr_alloc();
1824 chr
->opaque
= (void *)(intptr_t)fd
;
1825 chr
->chr_write
= null_chr_write
;
1826 chr
->chr_ioctl
= pp_ioctl
;
1827 chr
->explicit_be_open
= true;
1836 HANDLE hcom
, hrecv
, hsend
;
1841 /* Protected by the CharDriverState chr_write_lock. */
1847 HANDLE hInputReadyEvent
;
1848 HANDLE hInputDoneEvent
;
1849 HANDLE hInputThread
;
1850 uint8_t win_stdio_buf
;
1851 } WinStdioCharState
;
1853 #define NSENDBUF 2048
1854 #define NRECVBUF 2048
1855 #define MAXCONNECT 1
1856 #define NTIMEOUT 5000
1858 static int win_chr_poll(void *opaque
);
1859 static int win_chr_pipe_poll(void *opaque
);
1861 static void win_chr_close(CharDriverState
*chr
)
1863 WinCharState
*s
= chr
->opaque
;
1866 CloseHandle(s
->hsend
);
1870 CloseHandle(s
->hrecv
);
1874 CloseHandle(s
->hcom
);
1878 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1880 qemu_del_polling_cb(win_chr_poll
, chr
);
1882 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1885 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1887 WinCharState
*s
= chr
->opaque
;
1889 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1894 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1896 fprintf(stderr
, "Failed CreateEvent\n");
1899 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1901 fprintf(stderr
, "Failed CreateEvent\n");
1905 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1906 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1907 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1908 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1913 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1914 fprintf(stderr
, "Failed SetupComm\n");
1918 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1919 size
= sizeof(COMMCONFIG
);
1920 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1921 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1922 CommConfigDialog(filename
, NULL
, &comcfg
);
1924 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1925 fprintf(stderr
, "Failed SetCommState\n");
1929 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1930 fprintf(stderr
, "Failed SetCommMask\n");
1934 cto
.ReadIntervalTimeout
= MAXDWORD
;
1935 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1936 fprintf(stderr
, "Failed SetCommTimeouts\n");
1940 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1941 fprintf(stderr
, "Failed ClearCommError\n");
1944 qemu_add_polling_cb(win_chr_poll
, chr
);
1952 /* Called with chr_write_lock held. */
1953 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1955 WinCharState
*s
= chr
->opaque
;
1956 DWORD len
, ret
, size
, err
;
1959 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1960 s
->osend
.hEvent
= s
->hsend
;
1963 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1965 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1967 err
= GetLastError();
1968 if (err
== ERROR_IO_PENDING
) {
1969 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1987 static int win_chr_read_poll(CharDriverState
*chr
)
1989 WinCharState
*s
= chr
->opaque
;
1991 s
->max_size
= qemu_chr_be_can_write(chr
);
1995 static void win_chr_readfile(CharDriverState
*chr
)
1997 WinCharState
*s
= chr
->opaque
;
1999 uint8_t buf
[READ_BUF_LEN
];
2002 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2003 s
->orecv
.hEvent
= s
->hrecv
;
2004 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2006 err
= GetLastError();
2007 if (err
== ERROR_IO_PENDING
) {
2008 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2013 qemu_chr_be_write(chr
, buf
, size
);
2017 static void win_chr_read(CharDriverState
*chr
)
2019 WinCharState
*s
= chr
->opaque
;
2021 if (s
->len
> s
->max_size
)
2022 s
->len
= s
->max_size
;
2026 win_chr_readfile(chr
);
2029 static int win_chr_poll(void *opaque
)
2031 CharDriverState
*chr
= opaque
;
2032 WinCharState
*s
= chr
->opaque
;
2036 ClearCommError(s
->hcom
, &comerr
, &status
);
2037 if (status
.cbInQue
> 0) {
2038 s
->len
= status
.cbInQue
;
2039 win_chr_read_poll(chr
);
2046 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
2048 CharDriverState
*chr
;
2051 chr
= qemu_chr_alloc();
2052 s
= g_malloc0(sizeof(WinCharState
));
2054 chr
->chr_write
= win_chr_write
;
2055 chr
->chr_close
= win_chr_close
;
2057 if (win_chr_init(chr
, filename
) < 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
)
2083 WinCharState
*s
= chr
->opaque
;
2087 char openname
[CHR_MAX_FILENAME_SIZE
];
2091 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2093 fprintf(stderr
, "Failed CreateEvent\n");
2096 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2098 fprintf(stderr
, "Failed CreateEvent\n");
2102 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2103 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2104 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2106 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2107 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2108 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2113 ZeroMemory(&ov
, sizeof(ov
));
2114 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2115 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2117 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2121 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2123 fprintf(stderr
, "Failed GetOverlappedResult\n");
2125 CloseHandle(ov
.hEvent
);
2132 CloseHandle(ov
.hEvent
);
2135 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2144 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
2146 const char *filename
= opts
->device
;
2147 CharDriverState
*chr
;
2150 chr
= qemu_chr_alloc();
2151 s
= g_malloc0(sizeof(WinCharState
));
2153 chr
->chr_write
= win_chr_write
;
2154 chr
->chr_close
= win_chr_close
;
2156 if (win_chr_pipe_init(chr
, filename
) < 0) {
2164 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2166 CharDriverState
*chr
;
2169 chr
= qemu_chr_alloc();
2170 s
= g_malloc0(sizeof(WinCharState
));
2173 chr
->chr_write
= win_chr_write
;
2177 static CharDriverState
*qemu_chr_open_win_con(void)
2179 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2182 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2184 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2191 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2201 static void win_stdio_wait_func(void *opaque
)
2203 CharDriverState
*chr
= opaque
;
2204 WinStdioCharState
*stdio
= chr
->opaque
;
2205 INPUT_RECORD buf
[4];
2210 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2213 /* Avoid error storm */
2214 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2218 for (i
= 0; i
< dwSize
; i
++) {
2219 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2221 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2223 if (kev
->uChar
.AsciiChar
!= 0) {
2224 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2225 if (qemu_chr_be_can_write(chr
)) {
2226 uint8_t c
= kev
->uChar
.AsciiChar
;
2227 qemu_chr_be_write(chr
, &c
, 1);
2235 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2237 CharDriverState
*chr
= param
;
2238 WinStdioCharState
*stdio
= chr
->opaque
;
2244 /* Wait for one byte */
2245 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2247 /* Exit in case of error, continue if nothing read */
2255 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2256 if (stdio
->win_stdio_buf
== '\r') {
2260 /* Signal the main thread and wait until the byte was eaten */
2261 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2264 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2270 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2274 static void win_stdio_thread_wait_func(void *opaque
)
2276 CharDriverState
*chr
= opaque
;
2277 WinStdioCharState
*stdio
= chr
->opaque
;
2279 if (qemu_chr_be_can_write(chr
)) {
2280 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2283 SetEvent(stdio
->hInputDoneEvent
);
2286 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2288 WinStdioCharState
*stdio
= chr
->opaque
;
2291 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2294 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2296 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2300 static void win_stdio_close(CharDriverState
*chr
)
2302 WinStdioCharState
*stdio
= chr
->opaque
;
2304 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2305 CloseHandle(stdio
->hInputReadyEvent
);
2307 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2308 CloseHandle(stdio
->hInputDoneEvent
);
2310 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2311 TerminateThread(stdio
->hInputThread
, 0);
2314 g_free(chr
->opaque
);
2318 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2320 CharDriverState
*chr
;
2321 WinStdioCharState
*stdio
;
2325 chr
= qemu_chr_alloc();
2326 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2328 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2329 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2330 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2334 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2336 chr
->opaque
= stdio
;
2337 chr
->chr_write
= win_stdio_write
;
2338 chr
->chr_close
= win_stdio_close
;
2341 if (qemu_add_wait_object(stdio
->hStdIn
,
2342 win_stdio_wait_func
, chr
)) {
2343 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2348 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2349 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2350 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2353 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2354 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2355 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2356 fprintf(stderr
, "cannot create stdio thread or event\n");
2359 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2360 win_stdio_thread_wait_func
, chr
)) {
2361 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2365 dwMode
|= ENABLE_LINE_INPUT
;
2368 /* set the terminal in raw mode */
2369 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2370 dwMode
|= ENABLE_PROCESSED_INPUT
;
2373 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2375 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2376 qemu_chr_fe_set_echo(chr
, false);
2380 #endif /* !_WIN32 */
2383 /***********************************************************/
2384 /* UDP Net console */
2389 uint8_t buf
[READ_BUF_LEN
];
2395 /* Called with chr_write_lock held. */
2396 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2398 NetCharDriver
*s
= chr
->opaque
;
2399 gsize bytes_written
;
2402 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2403 if (status
== G_IO_STATUS_EOF
) {
2405 } else if (status
!= G_IO_STATUS_NORMAL
) {
2409 return bytes_written
;
2412 static int udp_chr_read_poll(void *opaque
)
2414 CharDriverState
*chr
= opaque
;
2415 NetCharDriver
*s
= chr
->opaque
;
2417 s
->max_size
= qemu_chr_be_can_write(chr
);
2419 /* If there were any stray characters in the queue process them
2422 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2423 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2425 s
->max_size
= qemu_chr_be_can_write(chr
);
2430 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2432 CharDriverState
*chr
= opaque
;
2433 NetCharDriver
*s
= chr
->opaque
;
2434 gsize bytes_read
= 0;
2437 if (s
->max_size
== 0) {
2440 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2442 s
->bufcnt
= bytes_read
;
2443 s
->bufptr
= s
->bufcnt
;
2444 if (status
!= G_IO_STATUS_NORMAL
) {
2445 remove_fd_in_watch(chr
);
2450 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2451 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2453 s
->max_size
= qemu_chr_be_can_write(chr
);
2459 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2461 NetCharDriver
*s
= chr
->opaque
;
2463 remove_fd_in_watch(chr
);
2465 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2470 static void udp_chr_close(CharDriverState
*chr
)
2472 NetCharDriver
*s
= chr
->opaque
;
2474 remove_fd_in_watch(chr
);
2476 g_io_channel_unref(s
->chan
);
2480 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2483 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2485 CharDriverState
*chr
= NULL
;
2486 NetCharDriver
*s
= NULL
;
2488 chr
= qemu_chr_alloc();
2489 s
= g_malloc0(sizeof(NetCharDriver
));
2492 s
->chan
= io_channel_from_socket(s
->fd
);
2496 chr
->chr_write
= udp_chr_write
;
2497 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2498 chr
->chr_close
= udp_chr_close
;
2499 /* be isn't opened until we get a connection */
2500 chr
->explicit_be_open
= true;
2504 /***********************************************************/
2505 /* TCP Net console */
2509 GIOChannel
*chan
, *listen_chan
;
2518 int read_msgfds_num
;
2520 int write_msgfds_num
;
2522 SocketAddress
*addr
;
2526 guint reconnect_timer
;
2527 int64_t reconnect_time
;
2528 bool connect_err_reported
;
2531 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2533 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2535 TCPCharDriver
*s
= chr
->opaque
;
2536 assert(s
->connected
== 0);
2537 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2538 socket_reconnect_timeout
, chr
);
2541 static void check_report_connect_error(CharDriverState
*chr
,
2544 TCPCharDriver
*s
= chr
->opaque
;
2546 if (!s
->connect_err_reported
) {
2547 error_report("Unable to connect character device %s: %s",
2548 chr
->label
, error_get_pretty(err
));
2549 s
->connect_err_reported
= true;
2551 qemu_chr_socket_restart_timer(chr
);
2554 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2557 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2559 TCPCharDriver
*s
= chr
->opaque
;
2564 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2565 char control
[CMSG_SPACE(fd_size
)];
2566 struct cmsghdr
*cmsg
;
2568 memset(&msgh
, 0, sizeof(msgh
));
2569 memset(control
, 0, sizeof(control
));
2571 /* set the payload */
2572 iov
.iov_base
= (uint8_t *) buf
;
2575 msgh
.msg_iov
= &iov
;
2576 msgh
.msg_iovlen
= 1;
2578 msgh
.msg_control
= control
;
2579 msgh
.msg_controllen
= sizeof(control
);
2581 cmsg
= CMSG_FIRSTHDR(&msgh
);
2583 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2584 cmsg
->cmsg_level
= SOL_SOCKET
;
2585 cmsg
->cmsg_type
= SCM_RIGHTS
;
2586 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2589 r
= sendmsg(s
->fd
, &msgh
, 0);
2590 } while (r
< 0 && errno
== EINTR
);
2592 /* free the written msgfds, no matter what */
2593 if (s
->write_msgfds_num
) {
2594 g_free(s
->write_msgfds
);
2595 s
->write_msgfds
= 0;
2596 s
->write_msgfds_num
= 0;
2603 /* Called with chr_write_lock held. */
2604 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2606 TCPCharDriver
*s
= chr
->opaque
;
2609 if (s
->is_unix
&& s
->write_msgfds_num
) {
2610 return unix_send_msgfds(chr
, buf
, len
);
2614 return io_channel_send(s
->chan
, buf
, len
);
2617 /* XXX: indicate an error ? */
2622 static int tcp_chr_read_poll(void *opaque
)
2624 CharDriverState
*chr
= opaque
;
2625 TCPCharDriver
*s
= chr
->opaque
;
2628 s
->max_size
= qemu_chr_be_can_write(chr
);
2633 #define IAC_BREAK 243
2634 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2636 uint8_t *buf
, int *size
)
2638 /* Handle any telnet client's basic IAC options to satisfy char by
2639 * char mode with no echo. All IAC options will be removed from
2640 * the buf and the do_telnetopt variable will be used to track the
2641 * state of the width of the IAC information.
2643 * IAC commands come in sets of 3 bytes with the exception of the
2644 * "IAC BREAK" command and the double IAC.
2650 for (i
= 0; i
< *size
; i
++) {
2651 if (s
->do_telnetopt
> 1) {
2652 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2653 /* Double IAC means send an IAC */
2657 s
->do_telnetopt
= 1;
2659 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2660 /* Handle IAC break commands by sending a serial break */
2661 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2666 if (s
->do_telnetopt
>= 4) {
2667 s
->do_telnetopt
= 1;
2670 if ((unsigned char)buf
[i
] == IAC
) {
2671 s
->do_telnetopt
= 2;
2682 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2684 TCPCharDriver
*s
= chr
->opaque
;
2685 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2687 assert(num
<= TCP_MAX_FDS
);
2692 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2694 /* Close unused fds */
2695 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2696 close(s
->read_msgfds
[i
]);
2699 g_free(s
->read_msgfds
);
2701 s
->read_msgfds_num
= 0;
2707 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2709 TCPCharDriver
*s
= chr
->opaque
;
2711 /* clear old pending fd array */
2712 if (s
->write_msgfds
) {
2713 g_free(s
->write_msgfds
);
2717 s
->write_msgfds
= g_malloc(num
* sizeof(int));
2718 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2721 s
->write_msgfds_num
= num
;
2727 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2729 TCPCharDriver
*s
= chr
->opaque
;
2730 struct cmsghdr
*cmsg
;
2732 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2735 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2736 cmsg
->cmsg_level
!= SOL_SOCKET
||
2737 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2741 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2747 /* close and clean read_msgfds */
2748 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2749 close(s
->read_msgfds
[i
]);
2752 if (s
->read_msgfds_num
) {
2753 g_free(s
->read_msgfds
);
2756 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2757 s
->read_msgfds
= g_malloc(fd_size
);
2758 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2760 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2761 int fd
= s
->read_msgfds
[i
];
2766 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2769 #ifndef MSG_CMSG_CLOEXEC
2770 qemu_set_cloexec(fd
);
2776 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2778 TCPCharDriver
*s
= chr
->opaque
;
2779 struct msghdr msg
= { NULL
, };
2780 struct iovec iov
[1];
2782 struct cmsghdr cmsg
;
2783 char control
[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS
)];
2788 iov
[0].iov_base
= buf
;
2789 iov
[0].iov_len
= len
;
2793 msg
.msg_control
= &msg_control
;
2794 msg
.msg_controllen
= sizeof(msg_control
);
2796 #ifdef MSG_CMSG_CLOEXEC
2797 flags
|= MSG_CMSG_CLOEXEC
;
2799 ret
= recvmsg(s
->fd
, &msg
, flags
);
2800 if (ret
> 0 && s
->is_unix
) {
2801 unix_process_msgfd(chr
, &msg
);
2807 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2809 TCPCharDriver
*s
= chr
->opaque
;
2810 return qemu_recv(s
->fd
, buf
, len
, 0);
2814 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2816 TCPCharDriver
*s
= chr
->opaque
;
2817 return g_io_create_watch(s
->chan
, cond
);
2820 static void tcp_chr_disconnect(CharDriverState
*chr
)
2822 TCPCharDriver
*s
= chr
->opaque
;
2825 if (s
->listen_chan
) {
2826 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2827 tcp_chr_accept
, chr
);
2829 remove_fd_in_watch(chr
);
2830 g_io_channel_unref(s
->chan
);
2834 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2835 "disconnected:", s
->addr
, s
->is_listen
, s
->is_telnet
);
2836 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2837 if (s
->reconnect_time
) {
2838 qemu_chr_socket_restart_timer(chr
);
2842 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2844 CharDriverState
*chr
= opaque
;
2845 TCPCharDriver
*s
= chr
->opaque
;
2846 uint8_t buf
[READ_BUF_LEN
];
2849 if (cond
& G_IO_HUP
) {
2850 /* connection closed */
2851 tcp_chr_disconnect(chr
);
2855 if (!s
->connected
|| s
->max_size
<= 0) {
2859 if (len
> s
->max_size
)
2861 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2863 /* connection closed */
2864 tcp_chr_disconnect(chr
);
2865 } else if (size
> 0) {
2866 if (s
->do_telnetopt
)
2867 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2869 qemu_chr_be_write(chr
, buf
, size
);
2875 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2877 TCPCharDriver
*s
= chr
->opaque
;
2880 if (!s
->connected
) {
2884 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2886 /* connection closed */
2887 tcp_chr_disconnect(chr
);
2894 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2896 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2899 chr
->avail_connections
= 1;
2906 static void tcp_chr_connect(void *opaque
)
2908 CharDriverState
*chr
= opaque
;
2909 TCPCharDriver
*s
= chr
->opaque
;
2910 struct sockaddr_storage ss
, ps
;
2911 socklen_t ss_len
= sizeof(ss
), ps_len
= sizeof(ps
);
2913 memset(&ss
, 0, ss_len
);
2914 if (getsockname(s
->fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2915 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2916 "Error in getsockname: %s\n", strerror(errno
));
2917 } else if (getpeername(s
->fd
, (struct sockaddr
*) &ps
, &ps_len
) != 0) {
2918 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2919 "Error in getpeername: %s\n", strerror(errno
));
2921 sockaddr_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2922 &ss
, ss_len
, &ps
, ps_len
,
2923 s
->is_listen
, s
->is_telnet
);
2928 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2931 qemu_chr_be_generic_open(chr
);
2934 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2936 TCPCharDriver
*s
= chr
->opaque
;
2938 remove_fd_in_watch(chr
);
2940 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2945 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2946 static void tcp_chr_telnet_init(int fd
)
2949 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2950 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2951 send(fd
, (char *)buf
, 3, 0);
2952 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2953 send(fd
, (char *)buf
, 3, 0);
2954 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2955 send(fd
, (char *)buf
, 3, 0);
2956 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2957 send(fd
, (char *)buf
, 3, 0);
2960 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2962 TCPCharDriver
*s
= chr
->opaque
;
2966 qemu_set_nonblock(fd
);
2968 socket_set_nodelay(fd
);
2970 s
->chan
= io_channel_from_socket(fd
);
2971 if (s
->listen_tag
) {
2972 g_source_remove(s
->listen_tag
);
2975 tcp_chr_connect(chr
);
2980 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2982 CharDriverState
*chr
= opaque
;
2983 TCPCharDriver
*s
= chr
->opaque
;
2984 struct sockaddr_in saddr
;
2986 struct sockaddr_un uaddr
;
2988 struct sockaddr
*addr
;
2995 len
= sizeof(uaddr
);
2996 addr
= (struct sockaddr
*)&uaddr
;
3000 len
= sizeof(saddr
);
3001 addr
= (struct sockaddr
*)&saddr
;
3003 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
3004 if (fd
< 0 && errno
!= EINTR
) {
3007 } else if (fd
>= 0) {
3008 if (s
->do_telnetopt
)
3009 tcp_chr_telnet_init(fd
);
3013 if (tcp_chr_add_client(chr
, fd
) < 0)
3019 static void tcp_chr_close(CharDriverState
*chr
)
3021 TCPCharDriver
*s
= chr
->opaque
;
3024 if (s
->reconnect_timer
) {
3025 g_source_remove(s
->reconnect_timer
);
3026 s
->reconnect_timer
= 0;
3028 qapi_free_SocketAddress(s
->addr
);
3030 remove_fd_in_watch(chr
);
3032 g_io_channel_unref(s
->chan
);
3036 if (s
->listen_fd
>= 0) {
3037 if (s
->listen_tag
) {
3038 g_source_remove(s
->listen_tag
);
3041 if (s
->listen_chan
) {
3042 g_io_channel_unref(s
->listen_chan
);
3044 closesocket(s
->listen_fd
);
3046 if (s
->read_msgfds_num
) {
3047 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3048 close(s
->read_msgfds
[i
]);
3050 g_free(s
->read_msgfds
);
3052 if (s
->write_msgfds_num
) {
3053 g_free(s
->write_msgfds
);
3056 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3059 static void qemu_chr_finish_socket_connection(CharDriverState
*chr
, int fd
)
3061 TCPCharDriver
*s
= chr
->opaque
;
3065 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
3066 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
3067 tcp_chr_accept
, chr
);
3071 socket_set_nodelay(fd
);
3072 s
->chan
= io_channel_from_socket(s
->fd
);
3073 tcp_chr_connect(chr
);
3077 static void qemu_chr_socket_connected(int fd
, Error
*err
, void *opaque
)
3079 CharDriverState
*chr
= opaque
;
3080 TCPCharDriver
*s
= chr
->opaque
;
3083 check_report_connect_error(chr
, err
);
3087 s
->connect_err_reported
= false;
3088 qemu_chr_finish_socket_connection(chr
, fd
);
3091 static bool qemu_chr_open_socket_fd(CharDriverState
*chr
, Error
**errp
)
3093 TCPCharDriver
*s
= chr
->opaque
;
3097 fd
= socket_listen(s
->addr
, errp
);
3098 } else if (s
->reconnect_time
) {
3099 fd
= socket_connect(s
->addr
, errp
, qemu_chr_socket_connected
, chr
);
3102 fd
= socket_connect(s
->addr
, errp
, NULL
, NULL
);
3108 qemu_chr_finish_socket_connection(chr
, fd
);
3112 /*********************************************************/
3113 /* Ring buffer chardev */
3120 } RingBufCharDriver
;
3122 static size_t ringbuf_count(const CharDriverState
*chr
)
3124 const RingBufCharDriver
*d
= chr
->opaque
;
3126 return d
->prod
- d
->cons
;
3129 /* Called with chr_write_lock held. */
3130 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3132 RingBufCharDriver
*d
= chr
->opaque
;
3135 if (!buf
|| (len
< 0)) {
3139 for (i
= 0; i
< len
; i
++ ) {
3140 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3141 if (d
->prod
- d
->cons
> d
->size
) {
3142 d
->cons
= d
->prod
- d
->size
;
3149 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3151 RingBufCharDriver
*d
= chr
->opaque
;
3154 qemu_mutex_lock(&chr
->chr_write_lock
);
3155 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3156 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3158 qemu_mutex_unlock(&chr
->chr_write_lock
);
3163 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3165 RingBufCharDriver
*d
= chr
->opaque
;
3172 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3175 CharDriverState
*chr
;
3176 RingBufCharDriver
*d
;
3178 chr
= qemu_chr_alloc();
3179 d
= g_malloc(sizeof(*d
));
3181 d
->size
= opts
->has_size
? opts
->size
: 65536;
3183 /* The size must be power of 2 */
3184 if (d
->size
& (d
->size
- 1)) {
3185 error_setg(errp
, "size of ringbuf chardev must be power of two");
3191 d
->cbuf
= g_malloc0(d
->size
);
3194 chr
->chr_write
= ringbuf_chr_write
;
3195 chr
->chr_close
= ringbuf_chr_close
;
3205 bool chr_is_ringbuf(const CharDriverState
*chr
)
3207 return chr
->chr_write
== ringbuf_chr_write
;
3210 void qmp_ringbuf_write(const char *device
, const char *data
,
3211 bool has_format
, enum DataFormat format
,
3214 CharDriverState
*chr
;
3215 const uint8_t *write_data
;
3219 chr
= qemu_chr_find(device
);
3221 error_setg(errp
, "Device '%s' not found", device
);
3225 if (!chr_is_ringbuf(chr
)) {
3226 error_setg(errp
,"%s is not a ringbuf device", device
);
3230 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3231 write_data
= g_base64_decode(data
, &write_count
);
3233 write_data
= (uint8_t *)data
;
3234 write_count
= strlen(data
);
3237 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3239 if (write_data
!= (uint8_t *)data
) {
3240 g_free((void *)write_data
);
3244 error_setg(errp
, "Failed to write to device %s", device
);
3249 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3250 bool has_format
, enum DataFormat format
,
3253 CharDriverState
*chr
;
3258 chr
= qemu_chr_find(device
);
3260 error_setg(errp
, "Device '%s' not found", device
);
3264 if (!chr_is_ringbuf(chr
)) {
3265 error_setg(errp
,"%s is not a ringbuf device", device
);
3270 error_setg(errp
, "size must be greater than zero");
3274 count
= ringbuf_count(chr
);
3275 size
= size
> count
? count
: size
;
3276 read_data
= g_malloc(size
+ 1);
3278 ringbuf_chr_read(chr
, read_data
, size
);
3280 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3281 data
= g_base64_encode(read_data
, size
);
3285 * FIXME should read only complete, valid UTF-8 characters up
3286 * to @size bytes. Invalid sequences should be replaced by a
3287 * suitable replacement character. Except when (and only
3288 * when) ring buffer lost characters since last read, initial
3289 * continuation characters should be dropped.
3291 read_data
[size
] = 0;
3292 data
= (char *)read_data
;
3298 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3300 char host
[65], port
[33], width
[8], height
[8];
3304 Error
*local_err
= NULL
;
3306 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3308 error_report_err(local_err
);
3312 if (strstart(filename
, "mon:", &p
)) {
3314 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3315 if (strcmp(filename
, "stdio") == 0) {
3316 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3317 * but pass it to the guest. Handle this only for compat syntax,
3318 * for -chardev syntax we have special option for this.
3319 * This is what -nographic did, redirecting+muxing serial+monitor
3320 * to stdio causing Ctrl+C to be passed to guest. */
3321 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3325 if (strcmp(filename
, "null") == 0 ||
3326 strcmp(filename
, "pty") == 0 ||
3327 strcmp(filename
, "msmouse") == 0 ||
3328 strcmp(filename
, "braille") == 0 ||
3329 strcmp(filename
, "testdev") == 0 ||
3330 strcmp(filename
, "stdio") == 0) {
3331 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3334 if (strstart(filename
, "vc", &p
)) {
3335 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3337 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3339 qemu_opt_set(opts
, "width", width
, &error_abort
);
3340 qemu_opt_set(opts
, "height", height
, &error_abort
);
3341 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3343 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3344 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3351 if (strcmp(filename
, "con:") == 0) {
3352 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3355 if (strstart(filename
, "COM", NULL
)) {
3356 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3357 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3360 if (strstart(filename
, "file:", &p
)) {
3361 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3362 qemu_opt_set(opts
, "path", p
, &error_abort
);
3365 if (strstart(filename
, "pipe:", &p
)) {
3366 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3367 qemu_opt_set(opts
, "path", p
, &error_abort
);
3370 if (strstart(filename
, "tcp:", &p
) ||
3371 strstart(filename
, "telnet:", &p
)) {
3372 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3374 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3377 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3378 qemu_opt_set(opts
, "host", host
, &error_abort
);
3379 qemu_opt_set(opts
, "port", port
, &error_abort
);
3380 if (p
[pos
] == ',') {
3381 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3383 error_report_err(local_err
);
3387 if (strstart(filename
, "telnet:", &p
))
3388 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3391 if (strstart(filename
, "udp:", &p
)) {
3392 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3393 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3395 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3399 qemu_opt_set(opts
, "host", host
, &error_abort
);
3400 qemu_opt_set(opts
, "port", port
, &error_abort
);
3401 if (p
[pos
] == '@') {
3403 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3405 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3409 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3410 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3414 if (strstart(filename
, "unix:", &p
)) {
3415 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3416 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3418 error_report_err(local_err
);
3423 if (strstart(filename
, "/dev/parport", NULL
) ||
3424 strstart(filename
, "/dev/ppi", NULL
)) {
3425 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3426 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3429 if (strstart(filename
, "/dev/", NULL
)) {
3430 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3431 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3436 qemu_opts_del(opts
);
3440 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3443 const char *path
= qemu_opt_get(opts
, "path");
3446 error_setg(errp
, "chardev: file: no filename given");
3449 backend
->file
= g_new0(ChardevFile
, 1);
3450 backend
->file
->out
= g_strdup(path
);
3453 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3456 backend
->stdio
= g_new0(ChardevStdio
, 1);
3457 backend
->stdio
->has_signal
= true;
3458 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3461 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3464 const char *device
= qemu_opt_get(opts
, "path");
3466 if (device
== NULL
) {
3467 error_setg(errp
, "chardev: serial/tty: no device path given");
3470 backend
->serial
= g_new0(ChardevHostdev
, 1);
3471 backend
->serial
->device
= g_strdup(device
);
3474 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3477 const char *device
= qemu_opt_get(opts
, "path");
3479 if (device
== NULL
) {
3480 error_setg(errp
, "chardev: parallel: no device path given");
3483 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3484 backend
->parallel
->device
= g_strdup(device
);
3487 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3490 const char *device
= qemu_opt_get(opts
, "path");
3492 if (device
== NULL
) {
3493 error_setg(errp
, "chardev: pipe: no device path given");
3496 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3497 backend
->pipe
->device
= g_strdup(device
);
3500 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3505 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3507 val
= qemu_opt_get_size(opts
, "size", 0);
3509 backend
->ringbuf
->has_size
= true;
3510 backend
->ringbuf
->size
= val
;
3514 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3517 const char *chardev
= qemu_opt_get(opts
, "chardev");
3519 if (chardev
== NULL
) {
3520 error_setg(errp
, "chardev: mux: no chardev given");
3523 backend
->mux
= g_new0(ChardevMux
, 1);
3524 backend
->mux
->chardev
= g_strdup(chardev
);
3527 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3530 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3531 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3532 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3533 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3534 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3535 const char *path
= qemu_opt_get(opts
, "path");
3536 const char *host
= qemu_opt_get(opts
, "host");
3537 const char *port
= qemu_opt_get(opts
, "port");
3538 SocketAddress
*addr
;
3542 error_setg(errp
, "chardev: socket: no host given");
3546 error_setg(errp
, "chardev: socket: no port given");
3551 backend
->socket
= g_new0(ChardevSocket
, 1);
3553 backend
->socket
->has_nodelay
= true;
3554 backend
->socket
->nodelay
= do_nodelay
;
3555 backend
->socket
->has_server
= true;
3556 backend
->socket
->server
= is_listen
;
3557 backend
->socket
->has_telnet
= true;
3558 backend
->socket
->telnet
= is_telnet
;
3559 backend
->socket
->has_wait
= true;
3560 backend
->socket
->wait
= is_waitconnect
;
3561 backend
->socket
->has_reconnect
= true;
3562 backend
->socket
->reconnect
= reconnect
;
3564 addr
= g_new0(SocketAddress
, 1);
3566 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
3567 addr
->q_unix
= g_new0(UnixSocketAddress
, 1);
3568 addr
->q_unix
->path
= g_strdup(path
);
3570 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3571 addr
->inet
= g_new0(InetSocketAddress
, 1);
3572 addr
->inet
->host
= g_strdup(host
);
3573 addr
->inet
->port
= g_strdup(port
);
3574 addr
->inet
->has_to
= qemu_opt_get(opts
, "to");
3575 addr
->inet
->to
= qemu_opt_get_number(opts
, "to", 0);
3576 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3577 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3578 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3579 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3581 backend
->socket
->addr
= addr
;
3584 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
3587 const char *host
= qemu_opt_get(opts
, "host");
3588 const char *port
= qemu_opt_get(opts
, "port");
3589 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
3590 const char *localport
= qemu_opt_get(opts
, "localport");
3591 bool has_local
= false;
3592 SocketAddress
*addr
;
3594 if (host
== NULL
|| strlen(host
) == 0) {
3597 if (port
== NULL
|| strlen(port
) == 0) {
3598 error_setg(errp
, "chardev: udp: remote port not specified");
3601 if (localport
== NULL
|| strlen(localport
) == 0) {
3606 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
3612 backend
->udp
= g_new0(ChardevUdp
, 1);
3614 addr
= g_new0(SocketAddress
, 1);
3615 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3616 addr
->inet
= g_new0(InetSocketAddress
, 1);
3617 addr
->inet
->host
= g_strdup(host
);
3618 addr
->inet
->port
= g_strdup(port
);
3619 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3620 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3621 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3622 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3623 backend
->udp
->remote
= addr
;
3626 backend
->udp
->has_local
= true;
3627 addr
= g_new0(SocketAddress
, 1);
3628 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3629 addr
->inet
= g_new0(InetSocketAddress
, 1);
3630 addr
->inet
->host
= g_strdup(localaddr
);
3631 addr
->inet
->port
= g_strdup(localport
);
3632 backend
->udp
->local
= addr
;
3636 typedef struct CharDriver
{
3638 ChardevBackendKind kind
;
3639 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3642 static GSList
*backends
;
3644 void register_char_driver(const char *name
, ChardevBackendKind kind
,
3645 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3649 s
= g_malloc0(sizeof(*s
));
3650 s
->name
= g_strdup(name
);
3654 backends
= g_slist_append(backends
, s
);
3657 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3658 void (*init
)(struct CharDriverState
*s
),
3661 Error
*local_err
= NULL
;
3663 CharDriverState
*chr
;
3665 ChardevReturn
*ret
= NULL
;
3666 ChardevBackend
*backend
;
3667 const char *id
= qemu_opts_id(opts
);
3671 error_setg(errp
, "chardev: no id specified");
3675 if (qemu_opt_get(opts
, "backend") == NULL
) {
3676 error_setg(errp
, "chardev: \"%s\" missing backend",
3677 qemu_opts_id(opts
));
3680 for (i
= backends
; i
; i
= i
->next
) {
3683 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3688 error_setg(errp
, "chardev: backend \"%s\" not found",
3689 qemu_opt_get(opts
, "backend"));
3693 backend
= g_new0(ChardevBackend
, 1);
3695 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3696 bid
= g_strdup_printf("%s-base", id
);
3700 backend
->kind
= cd
->kind
;
3702 cd
->parse(opts
, backend
, &local_err
);
3704 error_propagate(errp
, local_err
);
3708 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3714 qapi_free_ChardevBackend(backend
);
3715 qapi_free_ChardevReturn(ret
);
3716 backend
= g_new0(ChardevBackend
, 1);
3717 backend
->mux
= g_new0(ChardevMux
, 1);
3718 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3719 backend
->mux
->chardev
= g_strdup(bid
);
3720 ret
= qmp_chardev_add(id
, backend
, errp
);
3722 chr
= qemu_chr_find(bid
);
3723 qemu_chr_delete(chr
);
3729 chr
= qemu_chr_find(id
);
3733 qapi_free_ChardevBackend(backend
);
3734 qapi_free_ChardevReturn(ret
);
3739 qemu_opts_del(opts
);
3743 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3746 CharDriverState
*chr
;
3750 if (strstart(filename
, "chardev:", &p
)) {
3751 return qemu_chr_find(p
);
3754 opts
= qemu_chr_parse_compat(label
, filename
);
3758 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3760 error_report_err(err
);
3762 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3763 qemu_chr_fe_claim_no_fail(chr
);
3764 monitor_init(chr
, MONITOR_USE_READLINE
);
3769 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3771 if (chr
->chr_set_echo
) {
3772 chr
->chr_set_echo(chr
, echo
);
3776 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3778 if (chr
->fe_open
== fe_open
) {
3781 chr
->fe_open
= fe_open
;
3782 if (chr
->chr_set_fe_open
) {
3783 chr
->chr_set_fe_open(chr
, fe_open
);
3787 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3789 if (chr
->chr_fe_event
) {
3790 chr
->chr_fe_event(chr
, event
);
3794 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3795 GIOFunc func
, void *user_data
)
3800 if (s
->chr_add_watch
== NULL
) {
3804 src
= s
->chr_add_watch(s
, cond
);
3809 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3810 tag
= g_source_attach(src
, NULL
);
3811 g_source_unref(src
);
3816 int qemu_chr_fe_claim(CharDriverState
*s
)
3818 if (s
->avail_connections
< 1) {
3821 s
->avail_connections
--;
3825 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3827 if (qemu_chr_fe_claim(s
) != 0) {
3828 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3829 __func__
, s
->label
);
3834 void qemu_chr_fe_release(CharDriverState
*s
)
3836 s
->avail_connections
++;
3839 void qemu_chr_delete(CharDriverState
*chr
)
3841 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3842 if (chr
->chr_close
) {
3843 chr
->chr_close(chr
);
3845 g_free(chr
->filename
);
3847 qemu_opts_del(chr
->opts
);
3851 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3853 ChardevInfoList
*chr_list
= NULL
;
3854 CharDriverState
*chr
;
3856 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3857 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3858 info
->value
= g_malloc0(sizeof(*info
->value
));
3859 info
->value
->label
= g_strdup(chr
->label
);
3860 info
->value
->filename
= g_strdup(chr
->filename
);
3861 info
->value
->frontend_open
= chr
->fe_open
;
3863 info
->next
= chr_list
;
3870 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3872 ChardevBackendInfoList
*backend_list
= NULL
;
3873 CharDriver
*c
= NULL
;
3876 for (i
= backends
; i
; i
= i
->next
) {
3877 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3879 info
->value
= g_malloc0(sizeof(*info
->value
));
3880 info
->value
->name
= g_strdup(c
->name
);
3882 info
->next
= backend_list
;
3883 backend_list
= info
;
3886 return backend_list
;
3889 CharDriverState
*qemu_chr_find(const char *name
)
3891 CharDriverState
*chr
;
3893 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3894 if (strcmp(chr
->label
, name
) != 0)
3901 /* Get a character (serial) device interface. */
3902 CharDriverState
*qemu_char_get_next_serial(void)
3904 static int next_serial
;
3905 CharDriverState
*chr
;
3907 /* FIXME: This function needs to go away: use chardev properties! */
3909 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3910 chr
= serial_hds
[next_serial
++];
3911 qemu_chr_fe_claim_no_fail(chr
);
3917 QemuOptsList qemu_chardev_opts
= {
3919 .implied_opt_name
= "backend",
3920 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3924 .type
= QEMU_OPT_STRING
,
3927 .type
= QEMU_OPT_STRING
,
3930 .type
= QEMU_OPT_STRING
,
3933 .type
= QEMU_OPT_STRING
,
3935 .name
= "localaddr",
3936 .type
= QEMU_OPT_STRING
,
3938 .name
= "localport",
3939 .type
= QEMU_OPT_STRING
,
3942 .type
= QEMU_OPT_NUMBER
,
3945 .type
= QEMU_OPT_BOOL
,
3948 .type
= QEMU_OPT_BOOL
,
3951 .type
= QEMU_OPT_BOOL
,
3954 .type
= QEMU_OPT_BOOL
,
3957 .type
= QEMU_OPT_BOOL
,
3959 .name
= "reconnect",
3960 .type
= QEMU_OPT_NUMBER
,
3963 .type
= QEMU_OPT_BOOL
,
3966 .type
= QEMU_OPT_NUMBER
,
3969 .type
= QEMU_OPT_NUMBER
,
3972 .type
= QEMU_OPT_NUMBER
,
3975 .type
= QEMU_OPT_NUMBER
,
3978 .type
= QEMU_OPT_BOOL
,
3981 .type
= QEMU_OPT_BOOL
,
3984 .type
= QEMU_OPT_STRING
,
3987 .type
= QEMU_OPT_NUMBER
,
3990 .type
= QEMU_OPT_SIZE
,
3993 .type
= QEMU_OPT_STRING
,
3995 { /* end of list */ }
4001 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
4006 error_setg(errp
, "input file not supported");
4010 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
4011 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4012 if (out
== INVALID_HANDLE_VALUE
) {
4013 error_setg(errp
, "open %s failed", file
->out
);
4016 return qemu_chr_open_win_file(out
);
4019 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
4022 return qemu_chr_open_win_path(serial
->device
);
4025 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
4028 error_setg(errp
, "character device backend type 'parallel' not supported");
4034 static int qmp_chardev_open_file_source(char *src
, int flags
,
4039 TFR(fd
= qemu_open(src
, flags
, 0666));
4041 error_setg_file_open(errp
, errno
, src
);
4046 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
4048 int flags
, in
= -1, out
;
4050 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
4051 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4058 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4065 return qemu_chr_open_fd(in
, out
);
4068 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
4071 #ifdef HAVE_CHARDEV_TTY
4074 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4078 qemu_set_nonblock(fd
);
4079 return qemu_chr_open_tty_fd(fd
);
4081 error_setg(errp
, "character device backend type 'serial' not supported");
4086 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
4089 #ifdef HAVE_CHARDEV_PARPORT
4092 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4096 return qemu_chr_open_pp_fd(fd
);
4098 error_setg(errp
, "character device backend type 'parallel' not supported");
4105 static void socket_try_connect(CharDriverState
*chr
)
4109 if (!qemu_chr_open_socket_fd(chr
, &err
)) {
4110 check_report_connect_error(chr
, err
);
4114 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4116 CharDriverState
*chr
= opaque
;
4117 TCPCharDriver
*s
= chr
->opaque
;
4119 s
->reconnect_timer
= 0;
4125 socket_try_connect(chr
);
4130 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
4133 CharDriverState
*chr
;
4135 SocketAddress
*addr
= sock
->addr
;
4136 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4137 bool is_listen
= sock
->has_server
? sock
->server
: true;
4138 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4139 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4140 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4142 chr
= qemu_chr_alloc();
4143 s
= g_malloc0(sizeof(TCPCharDriver
));
4147 s
->is_unix
= addr
->kind
== SOCKET_ADDRESS_KIND_UNIX
;
4148 s
->is_listen
= is_listen
;
4149 s
->is_telnet
= is_telnet
;
4150 s
->do_nodelay
= do_nodelay
;
4151 qapi_copy_SocketAddress(&s
->addr
, sock
->addr
);
4154 chr
->chr_write
= tcp_chr_write
;
4155 chr
->chr_sync_read
= tcp_chr_sync_read
;
4156 chr
->chr_close
= tcp_chr_close
;
4157 chr
->get_msgfds
= tcp_get_msgfds
;
4158 chr
->set_msgfds
= tcp_set_msgfds
;
4159 chr
->chr_add_client
= tcp_chr_add_client
;
4160 chr
->chr_add_watch
= tcp_chr_add_watch
;
4161 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4162 /* be isn't opened until we get a connection */
4163 chr
->explicit_be_open
= true;
4165 chr
->filename
= g_malloc(CHR_MAX_FILENAME_SIZE
);
4166 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
, "disconnected:",
4167 addr
, is_listen
, is_telnet
);
4171 s
->do_telnetopt
= 1;
4173 } else if (reconnect
> 0) {
4174 s
->reconnect_time
= reconnect
;
4177 if (s
->reconnect_time
) {
4178 socket_try_connect(chr
);
4179 } else if (!qemu_chr_open_socket_fd(chr
, errp
)) {
4181 g_free(chr
->filename
);
4186 if (is_listen
&& is_waitconnect
) {
4187 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
4189 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
4190 qemu_set_nonblock(s
->listen_fd
);
4196 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
4201 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
4205 return qemu_chr_open_udp_fd(fd
);
4208 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4211 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4212 CharDriverState
*base
, *chr
= NULL
;
4214 chr
= qemu_chr_find(id
);
4216 error_setg(errp
, "Chardev '%s' already exists", id
);
4221 switch (backend
->kind
) {
4222 case CHARDEV_BACKEND_KIND_FILE
:
4223 chr
= qmp_chardev_open_file(backend
->file
, errp
);
4225 case CHARDEV_BACKEND_KIND_SERIAL
:
4226 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
4228 case CHARDEV_BACKEND_KIND_PARALLEL
:
4229 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
4231 case CHARDEV_BACKEND_KIND_PIPE
:
4232 chr
= qemu_chr_open_pipe(backend
->pipe
);
4234 case CHARDEV_BACKEND_KIND_SOCKET
:
4235 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
4237 case CHARDEV_BACKEND_KIND_UDP
:
4238 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
4240 #ifdef HAVE_CHARDEV_TTY
4241 case CHARDEV_BACKEND_KIND_PTY
:
4242 chr
= qemu_chr_open_pty(id
, ret
);
4245 case CHARDEV_BACKEND_KIND_NULL
:
4246 chr
= qemu_chr_open_null();
4248 case CHARDEV_BACKEND_KIND_MUX
:
4249 base
= qemu_chr_find(backend
->mux
->chardev
);
4251 error_setg(errp
, "mux: base chardev %s not found",
4252 backend
->mux
->chardev
);
4255 chr
= qemu_chr_open_mux(base
);
4257 case CHARDEV_BACKEND_KIND_MSMOUSE
:
4258 chr
= qemu_chr_open_msmouse();
4260 #ifdef CONFIG_BRLAPI
4261 case CHARDEV_BACKEND_KIND_BRAILLE
:
4262 chr
= chr_baum_init();
4265 case CHARDEV_BACKEND_KIND_TESTDEV
:
4266 chr
= chr_testdev_init();
4268 case CHARDEV_BACKEND_KIND_STDIO
:
4269 chr
= qemu_chr_open_stdio(backend
->stdio
);
4272 case CHARDEV_BACKEND_KIND_CONSOLE
:
4273 chr
= qemu_chr_open_win_con();
4277 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4278 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
4280 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4281 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
4284 case CHARDEV_BACKEND_KIND_VC
:
4285 chr
= vc_init(backend
->vc
);
4287 case CHARDEV_BACKEND_KIND_RINGBUF
:
4288 case CHARDEV_BACKEND_KIND_MEMORY
:
4289 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
4292 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4297 * Character backend open hasn't been fully converted to the Error
4298 * API. Some opens fail without setting an error. Set a generic
4300 * TODO full conversion to Error API
4302 if (chr
== NULL
&& errp
&& !*errp
) {
4303 error_setg(errp
, "Failed to create chardev");
4306 chr
->label
= g_strdup(id
);
4307 chr
->avail_connections
=
4308 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4309 if (!chr
->filename
) {
4310 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4312 if (!chr
->explicit_be_open
) {
4313 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4315 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4323 void qmp_chardev_remove(const char *id
, Error
**errp
)
4325 CharDriverState
*chr
;
4327 chr
= qemu_chr_find(id
);
4329 error_setg(errp
, "Chardev '%s' not found", id
);
4332 if (chr
->chr_can_read
|| chr
->chr_read
||
4333 chr
->chr_event
|| chr
->handler_opaque
) {
4334 error_setg(errp
, "Chardev '%s' is busy", id
);
4337 qemu_chr_delete(chr
);
4340 static void register_types(void)
4342 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
4343 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4344 qemu_chr_parse_socket
);
4345 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
);
4346 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4347 qemu_chr_parse_ringbuf
);
4348 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4349 qemu_chr_parse_file_out
);
4350 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4351 qemu_chr_parse_stdio
);
4352 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4353 qemu_chr_parse_serial
);
4354 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4355 qemu_chr_parse_serial
);
4356 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4357 qemu_chr_parse_parallel
);
4358 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4359 qemu_chr_parse_parallel
);
4360 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
4361 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
4362 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4363 qemu_chr_parse_pipe
);
4364 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
);
4365 /* Bug-compatibility: */
4366 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4367 qemu_chr_parse_ringbuf
);
4368 /* this must be done after machine init, since we register FEs with muxes
4369 * as part of realize functions like serial_isa_realizefn when -nographic
4372 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4375 type_init(register_types
);