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(const char *id
,
387 ChardevBackend
*backend
,
391 CharDriverState
*chr
;
393 chr
= qemu_chr_alloc();
394 chr
->chr_write
= null_chr_write
;
395 chr
->explicit_be_open
= true;
399 /* MUX driver for serial I/O splitting */
401 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
402 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
404 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
405 IOReadHandler
*chr_read
[MAX_MUX
];
406 IOEventHandler
*chr_event
[MAX_MUX
];
407 void *ext_opaque
[MAX_MUX
];
408 CharDriverState
*drv
;
413 /* Intermediate input buffer allows to catch escape sequences even if the
414 currently active device is not accepting any input - but only until it
416 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
421 /* Protected by the CharDriverState chr_write_lock. */
423 int64_t timestamps_start
;
427 /* Called with chr_write_lock held. */
428 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
430 MuxDriver
*d
= chr
->opaque
;
432 if (!d
->timestamps
) {
433 ret
= qemu_chr_fe_write(d
->drv
, buf
, len
);
438 for (i
= 0; i
< len
; i
++) {
444 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
445 if (d
->timestamps_start
== -1)
446 d
->timestamps_start
= ti
;
447 ti
-= d
->timestamps_start
;
449 snprintf(buf1
, sizeof(buf1
),
450 "[%02d:%02d:%02d.%03d] ",
455 qemu_chr_fe_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
458 ret
+= qemu_chr_fe_write(d
->drv
, buf
+i
, 1);
459 if (buf
[i
] == '\n') {
467 static const char * const mux_help
[] = {
468 "% h print this help\n\r",
469 "% x exit emulator\n\r",
470 "% s save disk data back to file (if -snapshot)\n\r",
471 "% t toggle console timestamps\n\r",
472 "% b send break (magic sysrq)\n\r",
473 "% c switch between console and monitor\n\r",
478 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
479 static void mux_print_help(CharDriverState
*chr
)
482 char ebuf
[15] = "Escape-Char";
483 char cbuf
[50] = "\n\r";
485 if (term_escape_char
> 0 && term_escape_char
< 26) {
486 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
487 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
489 snprintf(cbuf
, sizeof(cbuf
),
490 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
493 qemu_chr_fe_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
494 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
495 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
496 if (mux_help
[i
][j
] == '%')
497 qemu_chr_fe_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
499 qemu_chr_fe_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
504 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
506 if (d
->chr_event
[mux_nr
])
507 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
510 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
512 if (d
->term_got_escape
) {
513 d
->term_got_escape
= 0;
514 if (ch
== term_escape_char
)
523 const char *term
= "QEMU: Terminated\n\r";
524 qemu_chr_fe_write(chr
, (uint8_t *)term
, strlen(term
));
532 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
535 /* Switch to the next registered device */
536 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
538 if (d
->focus
>= d
->mux_cnt
)
540 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
543 d
->timestamps
= !d
->timestamps
;
544 d
->timestamps_start
= -1;
548 } else if (ch
== term_escape_char
) {
549 d
->term_got_escape
= 1;
557 static void mux_chr_accept_input(CharDriverState
*chr
)
559 MuxDriver
*d
= chr
->opaque
;
562 while (d
->prod
[m
] != d
->cons
[m
] &&
563 d
->chr_can_read
[m
] &&
564 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
565 d
->chr_read
[m
](d
->ext_opaque
[m
],
566 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
570 static int mux_chr_can_read(void *opaque
)
572 CharDriverState
*chr
= opaque
;
573 MuxDriver
*d
= chr
->opaque
;
576 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
578 if (d
->chr_can_read
[m
])
579 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
583 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
585 CharDriverState
*chr
= opaque
;
586 MuxDriver
*d
= chr
->opaque
;
590 mux_chr_accept_input (opaque
);
592 for(i
= 0; i
< size
; i
++)
593 if (mux_proc_byte(chr
, d
, buf
[i
])) {
594 if (d
->prod
[m
] == d
->cons
[m
] &&
595 d
->chr_can_read
[m
] &&
596 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
597 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
599 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
603 static void mux_chr_event(void *opaque
, int event
)
605 CharDriverState
*chr
= opaque
;
606 MuxDriver
*d
= chr
->opaque
;
609 /* Send the event to all registered listeners */
610 for (i
= 0; i
< d
->mux_cnt
; i
++)
611 mux_chr_send_event(d
, i
, event
);
614 static void mux_chr_update_read_handler(CharDriverState
*chr
)
616 MuxDriver
*d
= chr
->opaque
;
618 if (d
->mux_cnt
>= MAX_MUX
) {
619 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
622 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
623 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
624 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
625 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
626 /* Fix up the real driver with mux routines */
627 if (d
->mux_cnt
== 0) {
628 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
631 if (d
->focus
!= -1) {
632 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
634 d
->focus
= d
->mux_cnt
;
636 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
639 static bool muxes_realized
;
642 * Called after processing of default and command-line-specified
643 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
644 * to a mux chardev. This is done here to ensure that
645 * output/prompts/banners are only displayed for the FE that has
646 * focus when initial command-line processing/machine init is
649 * After this point, any new FE attached to any new or existing
650 * mux will receive CHR_EVENT_OPENED notifications for the BE
653 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
655 CharDriverState
*chr
;
657 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
659 MuxDriver
*d
= chr
->opaque
;
662 /* send OPENED to all already-attached FEs */
663 for (i
= 0; i
< d
->mux_cnt
; i
++) {
664 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
666 /* mark mux as OPENED so any new FEs will immediately receive
669 qemu_chr_be_generic_open(chr
);
672 muxes_realized
= true;
675 static Notifier muxes_realize_notify
= {
676 .notify
= muxes_realize_done
,
679 static GSource
*mux_chr_add_watch(CharDriverState
*s
, GIOCondition cond
)
681 MuxDriver
*d
= s
->opaque
;
682 return d
->drv
->chr_add_watch(d
->drv
, cond
);
685 static CharDriverState
*qemu_chr_open_mux(const char *id
,
686 ChardevBackend
*backend
,
687 ChardevReturn
*ret
, Error
**errp
)
689 ChardevMux
*mux
= backend
->mux
;
690 CharDriverState
*chr
, *drv
;
693 drv
= qemu_chr_find(mux
->chardev
);
695 error_setg(errp
, "mux: base chardev %s not found", mux
->chardev
);
699 chr
= qemu_chr_alloc();
700 d
= g_new0(MuxDriver
, 1);
705 chr
->chr_write
= mux_chr_write
;
706 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
707 chr
->chr_accept_input
= mux_chr_accept_input
;
708 /* Frontend guest-open / -close notification is not support with muxes */
709 chr
->chr_set_fe_open
= NULL
;
710 if (drv
->chr_add_watch
) {
711 chr
->chr_add_watch
= mux_chr_add_watch
;
713 /* only default to opened state if we've realized the initial
716 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
724 int send_all(int fd
, const void *buf
, int len1
)
730 ret
= send(fd
, buf
, len
, 0);
732 errno
= WSAGetLastError();
733 if (errno
!= WSAEWOULDBLOCK
) {
736 } else if (ret
== 0) {
748 int send_all(int fd
, const void *_buf
, int len1
)
751 const uint8_t *buf
= _buf
;
755 ret
= write(fd
, buf
, len
);
757 if (errno
!= EINTR
&& errno
!= EAGAIN
)
759 } else if (ret
== 0) {
769 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
775 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
777 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
794 typedef struct IOWatchPoll
801 IOCanReadHandler
*fd_can_read
;
806 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
808 return container_of(source
, IOWatchPoll
, parent
);
811 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
813 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
814 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
815 bool was_active
= iwp
->src
!= NULL
;
816 if (was_active
== now_active
) {
821 iwp
->src
= g_io_create_watch(iwp
->channel
,
822 G_IO_IN
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
);
823 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
824 g_source_attach(iwp
->src
, NULL
);
826 g_source_destroy(iwp
->src
);
827 g_source_unref(iwp
->src
);
833 static gboolean
io_watch_poll_check(GSource
*source
)
838 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
844 static void io_watch_poll_finalize(GSource
*source
)
846 /* Due to a glib bug, removing the last reference to a source
847 * inside a finalize callback causes recursive locking (and a
848 * deadlock). This is not a problem inside other callbacks,
849 * including dispatch callbacks, so we call io_remove_watch_poll
850 * to remove this source. At this point, iwp->src must
851 * be NULL, or we would leak it.
853 * This would be solved much more elegantly by child sources,
854 * but we support older glib versions that do not have them.
856 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
857 assert(iwp
->src
== NULL
);
860 static GSourceFuncs io_watch_poll_funcs
= {
861 .prepare
= io_watch_poll_prepare
,
862 .check
= io_watch_poll_check
,
863 .dispatch
= io_watch_poll_dispatch
,
864 .finalize
= io_watch_poll_finalize
,
867 /* Can only be used for read */
868 static guint
io_add_watch_poll(GIOChannel
*channel
,
869 IOCanReadHandler
*fd_can_read
,
876 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
877 iwp
->fd_can_read
= fd_can_read
;
878 iwp
->opaque
= user_data
;
879 iwp
->channel
= channel
;
880 iwp
->fd_read
= (GSourceFunc
) fd_read
;
883 tag
= g_source_attach(&iwp
->parent
, NULL
);
884 g_source_unref(&iwp
->parent
);
888 static void io_remove_watch_poll(guint tag
)
893 g_return_if_fail (tag
> 0);
895 source
= g_main_context_find_source_by_id(NULL
, tag
);
896 g_return_if_fail (source
!= NULL
);
898 iwp
= io_watch_poll_from_source(source
);
900 g_source_destroy(iwp
->src
);
901 g_source_unref(iwp
->src
);
904 g_source_destroy(&iwp
->parent
);
907 static void remove_fd_in_watch(CharDriverState
*chr
)
909 if (chr
->fd_in_tag
) {
910 io_remove_watch_poll(chr
->fd_in_tag
);
916 static GIOChannel
*io_channel_from_fd(int fd
)
924 chan
= g_io_channel_unix_new(fd
);
926 g_io_channel_set_encoding(chan
, NULL
, NULL
);
927 g_io_channel_set_buffered(chan
, FALSE
);
933 static GIOChannel
*io_channel_from_socket(int fd
)
942 chan
= g_io_channel_win32_new_socket(fd
);
944 chan
= g_io_channel_unix_new(fd
);
947 g_io_channel_set_encoding(chan
, NULL
, NULL
);
948 g_io_channel_set_buffered(chan
, FALSE
);
953 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
956 GIOStatus status
= G_IO_STATUS_NORMAL
;
958 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
959 gsize bytes_written
= 0;
961 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
962 &bytes_written
, NULL
);
963 offset
+= bytes_written
;
970 case G_IO_STATUS_NORMAL
:
973 case G_IO_STATUS_AGAIN
:
985 typedef struct FDCharDriver
{
986 CharDriverState
*chr
;
987 GIOChannel
*fd_in
, *fd_out
;
991 /* Called with chr_write_lock held. */
992 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
994 FDCharDriver
*s
= chr
->opaque
;
996 return io_channel_send(s
->fd_out
, buf
, len
);
999 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1001 CharDriverState
*chr
= opaque
;
1002 FDCharDriver
*s
= chr
->opaque
;
1004 uint8_t buf
[READ_BUF_LEN
];
1009 if (len
> s
->max_size
) {
1016 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
1017 len
, &bytes_read
, NULL
);
1018 if (status
== G_IO_STATUS_EOF
) {
1019 remove_fd_in_watch(chr
);
1020 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1023 if (status
== G_IO_STATUS_NORMAL
) {
1024 qemu_chr_be_write(chr
, buf
, bytes_read
);
1030 static int fd_chr_read_poll(void *opaque
)
1032 CharDriverState
*chr
= opaque
;
1033 FDCharDriver
*s
= chr
->opaque
;
1035 s
->max_size
= qemu_chr_be_can_write(chr
);
1039 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1041 FDCharDriver
*s
= chr
->opaque
;
1042 return g_io_create_watch(s
->fd_out
, cond
);
1045 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1047 FDCharDriver
*s
= chr
->opaque
;
1049 remove_fd_in_watch(chr
);
1051 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
1056 static void fd_chr_close(struct CharDriverState
*chr
)
1058 FDCharDriver
*s
= chr
->opaque
;
1060 remove_fd_in_watch(chr
);
1062 g_io_channel_unref(s
->fd_in
);
1065 g_io_channel_unref(s
->fd_out
);
1069 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1072 /* open a character device to a unix fd */
1073 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1075 CharDriverState
*chr
;
1078 chr
= qemu_chr_alloc();
1079 s
= g_new0(FDCharDriver
, 1);
1080 s
->fd_in
= io_channel_from_fd(fd_in
);
1081 s
->fd_out
= io_channel_from_fd(fd_out
);
1082 qemu_set_nonblock(fd_out
);
1085 chr
->chr_add_watch
= fd_chr_add_watch
;
1086 chr
->chr_write
= fd_chr_write
;
1087 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1088 chr
->chr_close
= fd_chr_close
;
1093 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
1094 ChardevBackend
*backend
,
1098 ChardevHostdev
*opts
= backend
->pipe
;
1100 char filename_in
[CHR_MAX_FILENAME_SIZE
];
1101 char filename_out
[CHR_MAX_FILENAME_SIZE
];
1102 const char *filename
= opts
->device
;
1104 snprintf(filename_in
, CHR_MAX_FILENAME_SIZE
, "%s.in", filename
);
1105 snprintf(filename_out
, CHR_MAX_FILENAME_SIZE
, "%s.out", filename
);
1106 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1107 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1108 if (fd_in
< 0 || fd_out
< 0) {
1113 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1115 error_setg_file_open(errp
, errno
, filename
);
1119 return qemu_chr_open_fd(fd_in
, fd_out
);
1122 /* init terminal so that we can grab keys */
1123 static struct termios oldtty
;
1124 static int old_fd0_flags
;
1125 static bool stdio_in_use
;
1126 static bool stdio_allow_signal
;
1127 static bool stdio_echo_state
;
1129 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
);
1131 static void term_exit(void)
1133 tcsetattr (0, TCSANOW
, &oldtty
);
1134 fcntl(0, F_SETFL
, old_fd0_flags
);
1137 static void term_stdio_handler(int sig
)
1139 /* restore echo after resume from suspend. */
1140 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
1143 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1147 stdio_echo_state
= echo
;
1150 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1151 |INLCR
|IGNCR
|ICRNL
|IXON
);
1152 tty
.c_oflag
|= OPOST
;
1153 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1154 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1157 tty
.c_cc
[VTIME
] = 0;
1159 if (!stdio_allow_signal
)
1160 tty
.c_lflag
&= ~ISIG
;
1162 tcsetattr (0, TCSANOW
, &tty
);
1165 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1171 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
1172 ChardevBackend
*backend
,
1176 ChardevStdio
*opts
= backend
->stdio
;
1177 CharDriverState
*chr
;
1178 struct sigaction act
;
1180 if (is_daemonized()) {
1181 error_setg(errp
, "cannot use stdio with -daemonize");
1186 error_setg(errp
, "cannot use stdio by multiple character devices");
1190 stdio_in_use
= true;
1191 old_fd0_flags
= fcntl(0, F_GETFL
);
1192 tcgetattr(0, &oldtty
);
1193 qemu_set_nonblock(0);
1196 memset(&act
, 0, sizeof(act
));
1197 act
.sa_handler
= term_stdio_handler
;
1198 sigaction(SIGCONT
, &act
, NULL
);
1200 chr
= qemu_chr_open_fd(0, 1);
1201 chr
->chr_close
= qemu_chr_close_stdio
;
1202 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1203 if (opts
->has_signal
) {
1204 stdio_allow_signal
= opts
->signal
;
1206 qemu_chr_fe_set_echo(chr
, false);
1211 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1212 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1213 || defined(__GLIBC__)
1215 #define HAVE_CHARDEV_SERIAL 1
1216 #define HAVE_CHARDEV_PTY 1
1222 /* Protected by the CharDriverState chr_write_lock. */
1228 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1229 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1231 static gboolean
pty_chr_timer(gpointer opaque
)
1233 struct CharDriverState
*chr
= opaque
;
1234 PtyCharDriver
*s
= chr
->opaque
;
1236 qemu_mutex_lock(&chr
->chr_write_lock
);
1239 if (!s
->connected
) {
1241 pty_chr_update_read_handler_locked(chr
);
1243 qemu_mutex_unlock(&chr
->chr_write_lock
);
1247 /* Called with chr_write_lock held. */
1248 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1250 PtyCharDriver
*s
= chr
->opaque
;
1253 g_source_remove(s
->timer_tag
);
1258 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1260 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1264 /* Called with chr_write_lock held. */
1265 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1267 PtyCharDriver
*s
= chr
->opaque
;
1270 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1271 pfd
.events
= G_IO_OUT
;
1274 if (pfd
.revents
& G_IO_HUP
) {
1275 pty_chr_state(chr
, 0);
1277 pty_chr_state(chr
, 1);
1281 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1283 qemu_mutex_lock(&chr
->chr_write_lock
);
1284 pty_chr_update_read_handler_locked(chr
);
1285 qemu_mutex_unlock(&chr
->chr_write_lock
);
1288 /* Called with chr_write_lock held. */
1289 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1291 PtyCharDriver
*s
= chr
->opaque
;
1293 if (!s
->connected
) {
1294 /* guest sends data, check for (re-)connect */
1295 pty_chr_update_read_handler_locked(chr
);
1296 if (!s
->connected
) {
1300 return io_channel_send(s
->fd
, buf
, len
);
1303 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1305 PtyCharDriver
*s
= chr
->opaque
;
1306 if (!s
->connected
) {
1309 return g_io_create_watch(s
->fd
, cond
);
1312 static int pty_chr_read_poll(void *opaque
)
1314 CharDriverState
*chr
= opaque
;
1315 PtyCharDriver
*s
= chr
->opaque
;
1317 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1318 return s
->read_bytes
;
1321 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1323 CharDriverState
*chr
= opaque
;
1324 PtyCharDriver
*s
= chr
->opaque
;
1326 uint8_t buf
[READ_BUF_LEN
];
1330 if (len
> s
->read_bytes
)
1331 len
= s
->read_bytes
;
1335 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1336 if (status
!= G_IO_STATUS_NORMAL
) {
1337 pty_chr_state(chr
, 0);
1340 pty_chr_state(chr
, 1);
1341 qemu_chr_be_write(chr
, buf
, size
);
1346 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1348 CharDriverState
*chr
= opaque
;
1349 PtyCharDriver
*s
= chr
->opaque
;
1352 qemu_chr_be_generic_open(chr
);
1356 /* Called with chr_write_lock held. */
1357 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1359 PtyCharDriver
*s
= chr
->opaque
;
1363 g_source_remove(s
->open_tag
);
1366 remove_fd_in_watch(chr
);
1368 /* (re-)connect poll interval for idle guests: once per second.
1369 * We check more frequently in case the guests sends data to
1370 * the virtual device linked to our pty. */
1371 pty_chr_rearm_timer(chr
, 1000);
1374 g_source_remove(s
->timer_tag
);
1377 if (!s
->connected
) {
1378 g_assert(s
->open_tag
== 0);
1380 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1382 if (!chr
->fd_in_tag
) {
1383 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1389 static void pty_chr_close(struct CharDriverState
*chr
)
1391 PtyCharDriver
*s
= chr
->opaque
;
1394 qemu_mutex_lock(&chr
->chr_write_lock
);
1395 pty_chr_state(chr
, 0);
1396 fd
= g_io_channel_unix_get_fd(s
->fd
);
1397 g_io_channel_unref(s
->fd
);
1400 g_source_remove(s
->timer_tag
);
1403 qemu_mutex_unlock(&chr
->chr_write_lock
);
1405 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1408 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1409 ChardevBackend
*backend
,
1413 CharDriverState
*chr
;
1415 int master_fd
, slave_fd
;
1416 char pty_name
[PATH_MAX
];
1418 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1419 if (master_fd
< 0) {
1420 error_setg_errno(errp
, errno
, "Failed to create PTY");
1425 qemu_set_nonblock(master_fd
);
1427 chr
= qemu_chr_alloc();
1429 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1430 ret
->pty
= g_strdup(pty_name
);
1431 ret
->has_pty
= true;
1433 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1436 s
= g_new0(PtyCharDriver
, 1);
1438 chr
->chr_write
= pty_chr_write
;
1439 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1440 chr
->chr_close
= pty_chr_close
;
1441 chr
->chr_add_watch
= pty_chr_add_watch
;
1442 chr
->explicit_be_open
= true;
1444 s
->fd
= io_channel_from_fd(master_fd
);
1450 static void tty_serial_init(int fd
, int speed
,
1451 int parity
, int data_bits
, int stop_bits
)
1457 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1458 speed
, parity
, data_bits
, stop_bits
);
1460 tcgetattr (fd
, &tty
);
1462 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1463 speed
= speed
* 10 / 11;
1480 /* Non-Posix values follow. They may be unsupported on some systems. */
1482 check_speed(115200);
1484 check_speed(230400);
1487 check_speed(460800);
1490 check_speed(500000);
1493 check_speed(576000);
1496 check_speed(921600);
1499 check_speed(1000000);
1502 check_speed(1152000);
1505 check_speed(1500000);
1508 check_speed(2000000);
1511 check_speed(2500000);
1514 check_speed(3000000);
1517 check_speed(3500000);
1520 check_speed(4000000);
1525 cfsetispeed(&tty
, spd
);
1526 cfsetospeed(&tty
, spd
);
1528 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1529 |INLCR
|IGNCR
|ICRNL
|IXON
);
1530 tty
.c_oflag
|= OPOST
;
1531 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1532 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1553 tty
.c_cflag
|= PARENB
;
1556 tty
.c_cflag
|= PARENB
| PARODD
;
1560 tty
.c_cflag
|= CSTOPB
;
1562 tcsetattr (fd
, TCSANOW
, &tty
);
1565 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1567 FDCharDriver
*s
= chr
->opaque
;
1570 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1572 QEMUSerialSetParams
*ssp
= arg
;
1573 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1574 ssp
->speed
, ssp
->parity
,
1575 ssp
->data_bits
, ssp
->stop_bits
);
1578 case CHR_IOCTL_SERIAL_SET_BREAK
:
1580 int enable
= *(int *)arg
;
1582 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1586 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1589 int *targ
= (int *)arg
;
1590 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1592 if (sarg
& TIOCM_CTS
)
1593 *targ
|= CHR_TIOCM_CTS
;
1594 if (sarg
& TIOCM_CAR
)
1595 *targ
|= CHR_TIOCM_CAR
;
1596 if (sarg
& TIOCM_DSR
)
1597 *targ
|= CHR_TIOCM_DSR
;
1598 if (sarg
& TIOCM_RI
)
1599 *targ
|= CHR_TIOCM_RI
;
1600 if (sarg
& TIOCM_DTR
)
1601 *targ
|= CHR_TIOCM_DTR
;
1602 if (sarg
& TIOCM_RTS
)
1603 *targ
|= CHR_TIOCM_RTS
;
1606 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1608 int sarg
= *(int *)arg
;
1610 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1611 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1612 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1613 if (sarg
& CHR_TIOCM_CTS
)
1615 if (sarg
& CHR_TIOCM_CAR
)
1617 if (sarg
& CHR_TIOCM_DSR
)
1619 if (sarg
& CHR_TIOCM_RI
)
1621 if (sarg
& CHR_TIOCM_DTR
)
1623 if (sarg
& CHR_TIOCM_RTS
)
1625 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1634 static void qemu_chr_close_tty(CharDriverState
*chr
)
1636 FDCharDriver
*s
= chr
->opaque
;
1640 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1650 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1652 CharDriverState
*chr
;
1654 tty_serial_init(fd
, 115200, 'N', 8, 1);
1655 chr
= qemu_chr_open_fd(fd
, fd
);
1656 chr
->chr_ioctl
= tty_serial_ioctl
;
1657 chr
->chr_close
= qemu_chr_close_tty
;
1660 #endif /* __linux__ || __sun__ */
1662 #if defined(__linux__)
1664 #define HAVE_CHARDEV_PARPORT 1
1669 } ParallelCharDriver
;
1671 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1673 if (s
->mode
!= mode
) {
1675 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1682 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1684 ParallelCharDriver
*drv
= chr
->opaque
;
1689 case CHR_IOCTL_PP_READ_DATA
:
1690 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1692 *(uint8_t *)arg
= b
;
1694 case CHR_IOCTL_PP_WRITE_DATA
:
1695 b
= *(uint8_t *)arg
;
1696 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1699 case CHR_IOCTL_PP_READ_CONTROL
:
1700 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1702 /* Linux gives only the lowest bits, and no way to know data
1703 direction! For better compatibility set the fixed upper
1705 *(uint8_t *)arg
= b
| 0xc0;
1707 case CHR_IOCTL_PP_WRITE_CONTROL
:
1708 b
= *(uint8_t *)arg
;
1709 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1712 case CHR_IOCTL_PP_READ_STATUS
:
1713 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1715 *(uint8_t *)arg
= b
;
1717 case CHR_IOCTL_PP_DATA_DIR
:
1718 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1721 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1722 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1723 struct ParallelIOArg
*parg
= arg
;
1724 int n
= read(fd
, parg
->buffer
, parg
->count
);
1725 if (n
!= parg
->count
) {
1730 case CHR_IOCTL_PP_EPP_READ
:
1731 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1732 struct ParallelIOArg
*parg
= arg
;
1733 int n
= read(fd
, parg
->buffer
, parg
->count
);
1734 if (n
!= parg
->count
) {
1739 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1740 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1741 struct ParallelIOArg
*parg
= arg
;
1742 int n
= write(fd
, parg
->buffer
, parg
->count
);
1743 if (n
!= parg
->count
) {
1748 case CHR_IOCTL_PP_EPP_WRITE
:
1749 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1750 struct ParallelIOArg
*parg
= arg
;
1751 int n
= write(fd
, parg
->buffer
, parg
->count
);
1752 if (n
!= parg
->count
) {
1763 static void pp_close(CharDriverState
*chr
)
1765 ParallelCharDriver
*drv
= chr
->opaque
;
1768 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1769 ioctl(fd
, PPRELEASE
);
1772 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1775 static CharDriverState
*qemu_chr_open_pp_fd(int fd
, Error
**errp
)
1777 CharDriverState
*chr
;
1778 ParallelCharDriver
*drv
;
1780 if (ioctl(fd
, PPCLAIM
) < 0) {
1781 error_setg_errno(errp
, errno
, "not a parallel port");
1786 drv
= g_new0(ParallelCharDriver
, 1);
1788 drv
->mode
= IEEE1284_MODE_COMPAT
;
1790 chr
= qemu_chr_alloc();
1791 chr
->chr_write
= null_chr_write
;
1792 chr
->chr_ioctl
= pp_ioctl
;
1793 chr
->chr_close
= pp_close
;
1798 #endif /* __linux__ */
1800 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1802 #define HAVE_CHARDEV_PARPORT 1
1804 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1806 int fd
= (int)(intptr_t)chr
->opaque
;
1810 case CHR_IOCTL_PP_READ_DATA
:
1811 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1813 *(uint8_t *)arg
= b
;
1815 case CHR_IOCTL_PP_WRITE_DATA
:
1816 b
= *(uint8_t *)arg
;
1817 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1820 case CHR_IOCTL_PP_READ_CONTROL
:
1821 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1823 *(uint8_t *)arg
= b
;
1825 case CHR_IOCTL_PP_WRITE_CONTROL
:
1826 b
= *(uint8_t *)arg
;
1827 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1830 case CHR_IOCTL_PP_READ_STATUS
:
1831 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1833 *(uint8_t *)arg
= b
;
1841 static CharDriverState
*qemu_chr_open_pp_fd(int fd
, Error
**errp
)
1843 CharDriverState
*chr
;
1845 chr
= qemu_chr_alloc();
1846 chr
->opaque
= (void *)(intptr_t)fd
;
1847 chr
->chr_write
= null_chr_write
;
1848 chr
->chr_ioctl
= pp_ioctl
;
1849 chr
->explicit_be_open
= true;
1856 #define HAVE_CHARDEV_SERIAL 1
1860 HANDLE hcom
, hrecv
, hsend
;
1865 /* Protected by the CharDriverState chr_write_lock. */
1871 HANDLE hInputReadyEvent
;
1872 HANDLE hInputDoneEvent
;
1873 HANDLE hInputThread
;
1874 uint8_t win_stdio_buf
;
1875 } WinStdioCharState
;
1877 #define NSENDBUF 2048
1878 #define NRECVBUF 2048
1879 #define MAXCONNECT 1
1880 #define NTIMEOUT 5000
1882 static int win_chr_poll(void *opaque
);
1883 static int win_chr_pipe_poll(void *opaque
);
1885 static void win_chr_close(CharDriverState
*chr
)
1887 WinCharState
*s
= chr
->opaque
;
1890 CloseHandle(s
->hsend
);
1894 CloseHandle(s
->hrecv
);
1898 CloseHandle(s
->hcom
);
1902 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1904 qemu_del_polling_cb(win_chr_poll
, chr
);
1906 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1909 static int win_chr_init(CharDriverState
*chr
, const char *filename
, Error
**errp
)
1911 WinCharState
*s
= chr
->opaque
;
1913 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1918 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1920 error_setg(errp
, "Failed CreateEvent");
1923 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1925 error_setg(errp
, "Failed CreateEvent");
1929 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1930 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1931 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1932 error_setg(errp
, "Failed CreateFile (%lu)", GetLastError());
1937 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1938 error_setg(errp
, "Failed SetupComm");
1942 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1943 size
= sizeof(COMMCONFIG
);
1944 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1945 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1946 CommConfigDialog(filename
, NULL
, &comcfg
);
1948 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1949 error_setg(errp
, "Failed SetCommState");
1953 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1954 error_setg(errp
, "Failed SetCommMask");
1958 cto
.ReadIntervalTimeout
= MAXDWORD
;
1959 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1960 error_setg(errp
, "Failed SetCommTimeouts");
1964 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1965 error_setg(errp
, "Failed ClearCommError");
1968 qemu_add_polling_cb(win_chr_poll
, chr
);
1976 /* Called with chr_write_lock held. */
1977 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1979 WinCharState
*s
= chr
->opaque
;
1980 DWORD len
, ret
, size
, err
;
1983 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1984 s
->osend
.hEvent
= s
->hsend
;
1987 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1989 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1991 err
= GetLastError();
1992 if (err
== ERROR_IO_PENDING
) {
1993 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2011 static int win_chr_read_poll(CharDriverState
*chr
)
2013 WinCharState
*s
= chr
->opaque
;
2015 s
->max_size
= qemu_chr_be_can_write(chr
);
2019 static void win_chr_readfile(CharDriverState
*chr
)
2021 WinCharState
*s
= chr
->opaque
;
2023 uint8_t buf
[READ_BUF_LEN
];
2026 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2027 s
->orecv
.hEvent
= s
->hrecv
;
2028 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2030 err
= GetLastError();
2031 if (err
== ERROR_IO_PENDING
) {
2032 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2037 qemu_chr_be_write(chr
, buf
, size
);
2041 static void win_chr_read(CharDriverState
*chr
)
2043 WinCharState
*s
= chr
->opaque
;
2045 if (s
->len
> s
->max_size
)
2046 s
->len
= s
->max_size
;
2050 win_chr_readfile(chr
);
2053 static int win_chr_poll(void *opaque
)
2055 CharDriverState
*chr
= opaque
;
2056 WinCharState
*s
= chr
->opaque
;
2060 ClearCommError(s
->hcom
, &comerr
, &status
);
2061 if (status
.cbInQue
> 0) {
2062 s
->len
= status
.cbInQue
;
2063 win_chr_read_poll(chr
);
2070 static CharDriverState
*qemu_chr_open_win_path(const char *filename
,
2073 CharDriverState
*chr
;
2076 chr
= qemu_chr_alloc();
2077 s
= g_new0(WinCharState
, 1);
2079 chr
->chr_write
= win_chr_write
;
2080 chr
->chr_close
= win_chr_close
;
2082 if (win_chr_init(chr
, filename
, errp
) < 0) {
2090 static int win_chr_pipe_poll(void *opaque
)
2092 CharDriverState
*chr
= opaque
;
2093 WinCharState
*s
= chr
->opaque
;
2096 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2099 win_chr_read_poll(chr
);
2106 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
,
2109 WinCharState
*s
= chr
->opaque
;
2113 char openname
[CHR_MAX_FILENAME_SIZE
];
2117 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2119 error_setg(errp
, "Failed CreateEvent");
2122 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2124 error_setg(errp
, "Failed CreateEvent");
2128 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2129 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2130 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2132 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2133 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2134 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
2139 ZeroMemory(&ov
, sizeof(ov
));
2140 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2141 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2143 error_setg(errp
, "Failed ConnectNamedPipe");
2147 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2149 error_setg(errp
, "Failed GetOverlappedResult");
2151 CloseHandle(ov
.hEvent
);
2158 CloseHandle(ov
.hEvent
);
2161 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2170 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
2171 ChardevBackend
*backend
,
2175 ChardevHostdev
*opts
= backend
->pipe
;
2176 const char *filename
= opts
->device
;
2177 CharDriverState
*chr
;
2180 chr
= qemu_chr_alloc();
2181 s
= g_new0(WinCharState
, 1);
2183 chr
->chr_write
= win_chr_write
;
2184 chr
->chr_close
= win_chr_close
;
2186 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
2194 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2196 CharDriverState
*chr
;
2199 chr
= qemu_chr_alloc();
2200 s
= g_new0(WinCharState
, 1);
2203 chr
->chr_write
= win_chr_write
;
2207 static CharDriverState
*qemu_chr_open_win_con(const char *id
,
2208 ChardevBackend
*backend
,
2212 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2215 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2217 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2224 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2234 static void win_stdio_wait_func(void *opaque
)
2236 CharDriverState
*chr
= opaque
;
2237 WinStdioCharState
*stdio
= chr
->opaque
;
2238 INPUT_RECORD buf
[4];
2243 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2246 /* Avoid error storm */
2247 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2251 for (i
= 0; i
< dwSize
; i
++) {
2252 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2254 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2256 if (kev
->uChar
.AsciiChar
!= 0) {
2257 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2258 if (qemu_chr_be_can_write(chr
)) {
2259 uint8_t c
= kev
->uChar
.AsciiChar
;
2260 qemu_chr_be_write(chr
, &c
, 1);
2268 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2270 CharDriverState
*chr
= param
;
2271 WinStdioCharState
*stdio
= chr
->opaque
;
2277 /* Wait for one byte */
2278 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2280 /* Exit in case of error, continue if nothing read */
2288 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2289 if (stdio
->win_stdio_buf
== '\r') {
2293 /* Signal the main thread and wait until the byte was eaten */
2294 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2297 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2303 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2307 static void win_stdio_thread_wait_func(void *opaque
)
2309 CharDriverState
*chr
= opaque
;
2310 WinStdioCharState
*stdio
= chr
->opaque
;
2312 if (qemu_chr_be_can_write(chr
)) {
2313 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2316 SetEvent(stdio
->hInputDoneEvent
);
2319 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2321 WinStdioCharState
*stdio
= chr
->opaque
;
2324 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2327 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2329 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2333 static void win_stdio_close(CharDriverState
*chr
)
2335 WinStdioCharState
*stdio
= chr
->opaque
;
2337 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2338 CloseHandle(stdio
->hInputReadyEvent
);
2340 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2341 CloseHandle(stdio
->hInputDoneEvent
);
2343 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2344 TerminateThread(stdio
->hInputThread
, 0);
2347 g_free(chr
->opaque
);
2351 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
2352 ChardevBackend
*backend
,
2356 CharDriverState
*chr
;
2357 WinStdioCharState
*stdio
;
2361 chr
= qemu_chr_alloc();
2362 stdio
= g_new0(WinStdioCharState
, 1);
2364 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2365 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2366 error_setg(errp
, "cannot open stdio: invalid handle");
2370 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2372 chr
->opaque
= stdio
;
2373 chr
->chr_write
= win_stdio_write
;
2374 chr
->chr_close
= win_stdio_close
;
2377 if (qemu_add_wait_object(stdio
->hStdIn
,
2378 win_stdio_wait_func
, chr
)) {
2379 error_setg(errp
, "qemu_add_wait_object: failed");
2385 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2386 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2387 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2388 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2389 error_setg(errp
, "cannot create event");
2392 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2393 win_stdio_thread_wait_func
, chr
)) {
2394 error_setg(errp
, "qemu_add_wait_object: failed");
2397 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2400 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
2401 error_setg(errp
, "cannot create stdio thread");
2406 dwMode
|= ENABLE_LINE_INPUT
;
2409 /* set the terminal in raw mode */
2410 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2411 dwMode
|= ENABLE_PROCESSED_INPUT
;
2414 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2416 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2417 qemu_chr_fe_set_echo(chr
, false);
2422 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2424 CloseHandle(stdio
->hInputReadyEvent
);
2425 CloseHandle(stdio
->hInputDoneEvent
);
2427 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2430 #endif /* !_WIN32 */
2433 /***********************************************************/
2434 /* UDP Net console */
2439 uint8_t buf
[READ_BUF_LEN
];
2445 /* Called with chr_write_lock held. */
2446 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2448 NetCharDriver
*s
= chr
->opaque
;
2449 gsize bytes_written
;
2452 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2453 if (status
== G_IO_STATUS_EOF
) {
2455 } else if (status
!= G_IO_STATUS_NORMAL
) {
2459 return bytes_written
;
2462 static int udp_chr_read_poll(void *opaque
)
2464 CharDriverState
*chr
= opaque
;
2465 NetCharDriver
*s
= chr
->opaque
;
2467 s
->max_size
= qemu_chr_be_can_write(chr
);
2469 /* If there were any stray characters in the queue process them
2472 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2473 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2475 s
->max_size
= qemu_chr_be_can_write(chr
);
2480 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2482 CharDriverState
*chr
= opaque
;
2483 NetCharDriver
*s
= chr
->opaque
;
2484 gsize bytes_read
= 0;
2487 if (s
->max_size
== 0) {
2490 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2492 s
->bufcnt
= bytes_read
;
2493 s
->bufptr
= s
->bufcnt
;
2494 if (status
!= G_IO_STATUS_NORMAL
) {
2495 remove_fd_in_watch(chr
);
2500 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2501 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2503 s
->max_size
= qemu_chr_be_can_write(chr
);
2509 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2511 NetCharDriver
*s
= chr
->opaque
;
2513 remove_fd_in_watch(chr
);
2515 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2520 static void udp_chr_close(CharDriverState
*chr
)
2522 NetCharDriver
*s
= chr
->opaque
;
2524 remove_fd_in_watch(chr
);
2526 g_io_channel_unref(s
->chan
);
2530 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2533 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2535 CharDriverState
*chr
= NULL
;
2536 NetCharDriver
*s
= NULL
;
2538 chr
= qemu_chr_alloc();
2539 s
= g_new0(NetCharDriver
, 1);
2542 s
->chan
= io_channel_from_socket(s
->fd
);
2546 chr
->chr_write
= udp_chr_write
;
2547 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2548 chr
->chr_close
= udp_chr_close
;
2549 /* be isn't opened until we get a connection */
2550 chr
->explicit_be_open
= true;
2554 /***********************************************************/
2555 /* TCP Net console */
2559 GIOChannel
*chan
, *listen_chan
;
2568 int read_msgfds_num
;
2570 int write_msgfds_num
;
2572 SocketAddress
*addr
;
2576 guint reconnect_timer
;
2577 int64_t reconnect_time
;
2578 bool connect_err_reported
;
2581 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2583 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2585 TCPCharDriver
*s
= chr
->opaque
;
2586 assert(s
->connected
== 0);
2587 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2588 socket_reconnect_timeout
, chr
);
2591 static void check_report_connect_error(CharDriverState
*chr
,
2594 TCPCharDriver
*s
= chr
->opaque
;
2596 if (!s
->connect_err_reported
) {
2597 error_report("Unable to connect character device %s: %s",
2598 chr
->label
, error_get_pretty(err
));
2599 s
->connect_err_reported
= true;
2601 qemu_chr_socket_restart_timer(chr
);
2604 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2607 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2609 TCPCharDriver
*s
= chr
->opaque
;
2614 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2615 char control
[CMSG_SPACE(fd_size
)];
2616 struct cmsghdr
*cmsg
;
2618 memset(&msgh
, 0, sizeof(msgh
));
2619 memset(control
, 0, sizeof(control
));
2621 /* set the payload */
2622 iov
.iov_base
= (uint8_t *) buf
;
2625 msgh
.msg_iov
= &iov
;
2626 msgh
.msg_iovlen
= 1;
2628 msgh
.msg_control
= control
;
2629 msgh
.msg_controllen
= sizeof(control
);
2631 cmsg
= CMSG_FIRSTHDR(&msgh
);
2633 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2634 cmsg
->cmsg_level
= SOL_SOCKET
;
2635 cmsg
->cmsg_type
= SCM_RIGHTS
;
2636 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2639 r
= sendmsg(s
->fd
, &msgh
, 0);
2640 } while (r
< 0 && errno
== EINTR
);
2642 /* free the written msgfds, no matter what */
2643 if (s
->write_msgfds_num
) {
2644 g_free(s
->write_msgfds
);
2645 s
->write_msgfds
= 0;
2646 s
->write_msgfds_num
= 0;
2653 /* Called with chr_write_lock held. */
2654 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2656 TCPCharDriver
*s
= chr
->opaque
;
2659 if (s
->is_unix
&& s
->write_msgfds_num
) {
2660 return unix_send_msgfds(chr
, buf
, len
);
2664 return io_channel_send(s
->chan
, buf
, len
);
2667 /* XXX: indicate an error ? */
2672 static int tcp_chr_read_poll(void *opaque
)
2674 CharDriverState
*chr
= opaque
;
2675 TCPCharDriver
*s
= chr
->opaque
;
2678 s
->max_size
= qemu_chr_be_can_write(chr
);
2683 #define IAC_BREAK 243
2684 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2686 uint8_t *buf
, int *size
)
2688 /* Handle any telnet client's basic IAC options to satisfy char by
2689 * char mode with no echo. All IAC options will be removed from
2690 * the buf and the do_telnetopt variable will be used to track the
2691 * state of the width of the IAC information.
2693 * IAC commands come in sets of 3 bytes with the exception of the
2694 * "IAC BREAK" command and the double IAC.
2700 for (i
= 0; i
< *size
; i
++) {
2701 if (s
->do_telnetopt
> 1) {
2702 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2703 /* Double IAC means send an IAC */
2707 s
->do_telnetopt
= 1;
2709 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2710 /* Handle IAC break commands by sending a serial break */
2711 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2716 if (s
->do_telnetopt
>= 4) {
2717 s
->do_telnetopt
= 1;
2720 if ((unsigned char)buf
[i
] == IAC
) {
2721 s
->do_telnetopt
= 2;
2732 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2734 TCPCharDriver
*s
= chr
->opaque
;
2735 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2737 assert(num
<= TCP_MAX_FDS
);
2742 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2744 /* Close unused fds */
2745 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2746 close(s
->read_msgfds
[i
]);
2749 g_free(s
->read_msgfds
);
2751 s
->read_msgfds_num
= 0;
2757 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2759 TCPCharDriver
*s
= chr
->opaque
;
2761 /* clear old pending fd array */
2762 g_free(s
->write_msgfds
);
2765 s
->write_msgfds
= g_new(int, num
);
2766 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2769 s
->write_msgfds_num
= num
;
2775 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2777 TCPCharDriver
*s
= chr
->opaque
;
2778 struct cmsghdr
*cmsg
;
2780 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2783 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2784 cmsg
->cmsg_level
!= SOL_SOCKET
||
2785 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2789 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2795 /* close and clean read_msgfds */
2796 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2797 close(s
->read_msgfds
[i
]);
2800 if (s
->read_msgfds_num
) {
2801 g_free(s
->read_msgfds
);
2804 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2805 s
->read_msgfds
= g_malloc(fd_size
);
2806 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2808 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2809 int fd
= s
->read_msgfds
[i
];
2814 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2817 #ifndef MSG_CMSG_CLOEXEC
2818 qemu_set_cloexec(fd
);
2824 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2826 TCPCharDriver
*s
= chr
->opaque
;
2827 struct msghdr msg
= { NULL
, };
2828 struct iovec iov
[1];
2830 struct cmsghdr cmsg
;
2831 char control
[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS
)];
2836 iov
[0].iov_base
= buf
;
2837 iov
[0].iov_len
= len
;
2841 msg
.msg_control
= &msg_control
;
2842 msg
.msg_controllen
= sizeof(msg_control
);
2844 #ifdef MSG_CMSG_CLOEXEC
2845 flags
|= MSG_CMSG_CLOEXEC
;
2848 ret
= recvmsg(s
->fd
, &msg
, flags
);
2849 } while (ret
== -1 && errno
== EINTR
);
2851 if (ret
> 0 && s
->is_unix
) {
2852 unix_process_msgfd(chr
, &msg
);
2858 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2860 TCPCharDriver
*s
= chr
->opaque
;
2864 ret
= qemu_recv(s
->fd
, buf
, len
, 0);
2865 } while (ret
== -1 && socket_error() == EINTR
);
2871 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2873 TCPCharDriver
*s
= chr
->opaque
;
2874 return g_io_create_watch(s
->chan
, cond
);
2877 static void tcp_chr_disconnect(CharDriverState
*chr
)
2879 TCPCharDriver
*s
= chr
->opaque
;
2882 if (s
->listen_chan
) {
2883 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2884 tcp_chr_accept
, chr
);
2886 remove_fd_in_watch(chr
);
2887 g_io_channel_unref(s
->chan
);
2891 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2892 "disconnected:", s
->addr
, s
->is_listen
, s
->is_telnet
);
2893 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2894 if (s
->reconnect_time
) {
2895 qemu_chr_socket_restart_timer(chr
);
2899 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2901 CharDriverState
*chr
= opaque
;
2902 TCPCharDriver
*s
= chr
->opaque
;
2903 uint8_t buf
[READ_BUF_LEN
];
2906 if (!s
->connected
|| s
->max_size
<= 0) {
2910 if (len
> s
->max_size
)
2912 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2915 socket_error() != EAGAIN
&& socket_error() != EWOULDBLOCK
)) {
2916 /* connection closed */
2917 tcp_chr_disconnect(chr
);
2918 } else if (size
> 0) {
2919 if (s
->do_telnetopt
)
2920 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2922 qemu_chr_be_write(chr
, buf
, size
);
2928 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2930 TCPCharDriver
*s
= chr
->opaque
;
2933 if (!s
->connected
) {
2937 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2939 /* connection closed */
2940 tcp_chr_disconnect(chr
);
2947 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2949 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2952 chr
->avail_connections
= 1;
2959 static void tcp_chr_connect(void *opaque
)
2961 CharDriverState
*chr
= opaque
;
2962 TCPCharDriver
*s
= chr
->opaque
;
2963 struct sockaddr_storage ss
, ps
;
2964 socklen_t ss_len
= sizeof(ss
), ps_len
= sizeof(ps
);
2966 memset(&ss
, 0, ss_len
);
2967 if (getsockname(s
->fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2968 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2969 "Error in getsockname: %s\n", strerror(errno
));
2970 } else if (getpeername(s
->fd
, (struct sockaddr
*) &ps
, &ps_len
) != 0) {
2971 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2972 "Error in getpeername: %s\n", strerror(errno
));
2974 sockaddr_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2975 &ss
, ss_len
, &ps
, ps_len
,
2976 s
->is_listen
, s
->is_telnet
);
2981 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2984 qemu_chr_be_generic_open(chr
);
2987 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2989 TCPCharDriver
*s
= chr
->opaque
;
2991 remove_fd_in_watch(chr
);
2993 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2998 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2999 static void tcp_chr_telnet_init(int fd
)
3002 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3003 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3004 send(fd
, (char *)buf
, 3, 0);
3005 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3006 send(fd
, (char *)buf
, 3, 0);
3007 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3008 send(fd
, (char *)buf
, 3, 0);
3009 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3010 send(fd
, (char *)buf
, 3, 0);
3013 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
3015 TCPCharDriver
*s
= chr
->opaque
;
3019 qemu_set_nonblock(fd
);
3021 socket_set_nodelay(fd
);
3023 s
->chan
= io_channel_from_socket(fd
);
3024 if (s
->listen_tag
) {
3025 g_source_remove(s
->listen_tag
);
3028 tcp_chr_connect(chr
);
3033 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
3035 CharDriverState
*chr
= opaque
;
3036 TCPCharDriver
*s
= chr
->opaque
;
3037 struct sockaddr_in saddr
;
3039 struct sockaddr_un uaddr
;
3041 struct sockaddr
*addr
;
3048 len
= sizeof(uaddr
);
3049 addr
= (struct sockaddr
*)&uaddr
;
3053 len
= sizeof(saddr
);
3054 addr
= (struct sockaddr
*)&saddr
;
3056 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
3057 if (fd
< 0 && errno
!= EINTR
) {
3060 } else if (fd
>= 0) {
3061 if (s
->do_telnetopt
)
3062 tcp_chr_telnet_init(fd
);
3066 if (tcp_chr_add_client(chr
, fd
) < 0)
3072 static void tcp_chr_close(CharDriverState
*chr
)
3074 TCPCharDriver
*s
= chr
->opaque
;
3077 if (s
->reconnect_timer
) {
3078 g_source_remove(s
->reconnect_timer
);
3079 s
->reconnect_timer
= 0;
3081 qapi_free_SocketAddress(s
->addr
);
3083 remove_fd_in_watch(chr
);
3085 g_io_channel_unref(s
->chan
);
3089 if (s
->listen_fd
>= 0) {
3090 if (s
->listen_tag
) {
3091 g_source_remove(s
->listen_tag
);
3094 if (s
->listen_chan
) {
3095 g_io_channel_unref(s
->listen_chan
);
3097 closesocket(s
->listen_fd
);
3099 if (s
->read_msgfds_num
) {
3100 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3101 close(s
->read_msgfds
[i
]);
3103 g_free(s
->read_msgfds
);
3105 if (s
->write_msgfds_num
) {
3106 g_free(s
->write_msgfds
);
3109 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3112 static void qemu_chr_finish_socket_connection(CharDriverState
*chr
, int fd
)
3114 TCPCharDriver
*s
= chr
->opaque
;
3118 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
3119 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
3120 tcp_chr_accept
, chr
);
3124 socket_set_nodelay(fd
);
3125 s
->chan
= io_channel_from_socket(s
->fd
);
3126 tcp_chr_connect(chr
);
3130 static void qemu_chr_socket_connected(int fd
, Error
*err
, void *opaque
)
3132 CharDriverState
*chr
= opaque
;
3133 TCPCharDriver
*s
= chr
->opaque
;
3136 check_report_connect_error(chr
, err
);
3140 s
->connect_err_reported
= false;
3141 qemu_chr_finish_socket_connection(chr
, fd
);
3144 static bool qemu_chr_open_socket_fd(CharDriverState
*chr
, Error
**errp
)
3146 TCPCharDriver
*s
= chr
->opaque
;
3150 fd
= socket_listen(s
->addr
, errp
);
3151 } else if (s
->reconnect_time
) {
3152 fd
= socket_connect(s
->addr
, errp
, qemu_chr_socket_connected
, chr
);
3155 fd
= socket_connect(s
->addr
, errp
, NULL
, NULL
);
3161 qemu_chr_finish_socket_connection(chr
, fd
);
3165 /*********************************************************/
3166 /* Ring buffer chardev */
3173 } RingBufCharDriver
;
3175 static size_t ringbuf_count(const CharDriverState
*chr
)
3177 const RingBufCharDriver
*d
= chr
->opaque
;
3179 return d
->prod
- d
->cons
;
3182 /* Called with chr_write_lock held. */
3183 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3185 RingBufCharDriver
*d
= chr
->opaque
;
3188 if (!buf
|| (len
< 0)) {
3192 for (i
= 0; i
< len
; i
++ ) {
3193 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3194 if (d
->prod
- d
->cons
> d
->size
) {
3195 d
->cons
= d
->prod
- d
->size
;
3202 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3204 RingBufCharDriver
*d
= chr
->opaque
;
3207 qemu_mutex_lock(&chr
->chr_write_lock
);
3208 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3209 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3211 qemu_mutex_unlock(&chr
->chr_write_lock
);
3216 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3218 RingBufCharDriver
*d
= chr
->opaque
;
3225 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3228 CharDriverState
*chr
;
3229 RingBufCharDriver
*d
;
3231 chr
= qemu_chr_alloc();
3232 d
= g_malloc(sizeof(*d
));
3234 d
->size
= opts
->has_size
? opts
->size
: 65536;
3236 /* The size must be power of 2 */
3237 if (d
->size
& (d
->size
- 1)) {
3238 error_setg(errp
, "size of ringbuf chardev must be power of two");
3244 d
->cbuf
= g_malloc0(d
->size
);
3247 chr
->chr_write
= ringbuf_chr_write
;
3248 chr
->chr_close
= ringbuf_chr_close
;
3258 bool chr_is_ringbuf(const CharDriverState
*chr
)
3260 return chr
->chr_write
== ringbuf_chr_write
;
3263 void qmp_ringbuf_write(const char *device
, const char *data
,
3264 bool has_format
, enum DataFormat format
,
3267 CharDriverState
*chr
;
3268 const uint8_t *write_data
;
3272 chr
= qemu_chr_find(device
);
3274 error_setg(errp
, "Device '%s' not found", device
);
3278 if (!chr_is_ringbuf(chr
)) {
3279 error_setg(errp
,"%s is not a ringbuf device", device
);
3283 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3284 write_data
= g_base64_decode(data
, &write_count
);
3286 write_data
= (uint8_t *)data
;
3287 write_count
= strlen(data
);
3290 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3292 if (write_data
!= (uint8_t *)data
) {
3293 g_free((void *)write_data
);
3297 error_setg(errp
, "Failed to write to device %s", device
);
3302 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3303 bool has_format
, enum DataFormat format
,
3306 CharDriverState
*chr
;
3311 chr
= qemu_chr_find(device
);
3313 error_setg(errp
, "Device '%s' not found", device
);
3317 if (!chr_is_ringbuf(chr
)) {
3318 error_setg(errp
,"%s is not a ringbuf device", device
);
3323 error_setg(errp
, "size must be greater than zero");
3327 count
= ringbuf_count(chr
);
3328 size
= size
> count
? count
: size
;
3329 read_data
= g_malloc(size
+ 1);
3331 ringbuf_chr_read(chr
, read_data
, size
);
3333 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3334 data
= g_base64_encode(read_data
, size
);
3338 * FIXME should read only complete, valid UTF-8 characters up
3339 * to @size bytes. Invalid sequences should be replaced by a
3340 * suitable replacement character. Except when (and only
3341 * when) ring buffer lost characters since last read, initial
3342 * continuation characters should be dropped.
3344 read_data
[size
] = 0;
3345 data
= (char *)read_data
;
3351 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3353 char host
[65], port
[33], width
[8], height
[8];
3357 Error
*local_err
= NULL
;
3359 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3361 error_report_err(local_err
);
3365 if (strstart(filename
, "mon:", &p
)) {
3367 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3368 if (strcmp(filename
, "stdio") == 0) {
3369 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3370 * but pass it to the guest. Handle this only for compat syntax,
3371 * for -chardev syntax we have special option for this.
3372 * This is what -nographic did, redirecting+muxing serial+monitor
3373 * to stdio causing Ctrl+C to be passed to guest. */
3374 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3378 if (strcmp(filename
, "null") == 0 ||
3379 strcmp(filename
, "pty") == 0 ||
3380 strcmp(filename
, "msmouse") == 0 ||
3381 strcmp(filename
, "braille") == 0 ||
3382 strcmp(filename
, "testdev") == 0 ||
3383 strcmp(filename
, "stdio") == 0) {
3384 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3387 if (strstart(filename
, "vc", &p
)) {
3388 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3390 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3392 qemu_opt_set(opts
, "width", width
, &error_abort
);
3393 qemu_opt_set(opts
, "height", height
, &error_abort
);
3394 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3396 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3397 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3404 if (strcmp(filename
, "con:") == 0) {
3405 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3408 if (strstart(filename
, "COM", NULL
)) {
3409 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3410 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3413 if (strstart(filename
, "file:", &p
)) {
3414 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3415 qemu_opt_set(opts
, "path", p
, &error_abort
);
3418 if (strstart(filename
, "pipe:", &p
)) {
3419 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3420 qemu_opt_set(opts
, "path", p
, &error_abort
);
3423 if (strstart(filename
, "tcp:", &p
) ||
3424 strstart(filename
, "telnet:", &p
)) {
3425 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3427 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3430 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3431 qemu_opt_set(opts
, "host", host
, &error_abort
);
3432 qemu_opt_set(opts
, "port", port
, &error_abort
);
3433 if (p
[pos
] == ',') {
3434 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3436 error_report_err(local_err
);
3440 if (strstart(filename
, "telnet:", &p
))
3441 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3444 if (strstart(filename
, "udp:", &p
)) {
3445 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3446 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3448 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3452 qemu_opt_set(opts
, "host", host
, &error_abort
);
3453 qemu_opt_set(opts
, "port", port
, &error_abort
);
3454 if (p
[pos
] == '@') {
3456 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3458 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3462 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3463 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3467 if (strstart(filename
, "unix:", &p
)) {
3468 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3469 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3471 error_report_err(local_err
);
3476 if (strstart(filename
, "/dev/parport", NULL
) ||
3477 strstart(filename
, "/dev/ppi", NULL
)) {
3478 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3479 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3482 if (strstart(filename
, "/dev/", NULL
)) {
3483 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3484 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3489 qemu_opts_del(opts
);
3493 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3496 const char *path
= qemu_opt_get(opts
, "path");
3499 error_setg(errp
, "chardev: file: no filename given");
3502 backend
->file
= g_new0(ChardevFile
, 1);
3503 backend
->file
->out
= g_strdup(path
);
3506 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3509 backend
->stdio
= g_new0(ChardevStdio
, 1);
3510 backend
->stdio
->has_signal
= true;
3511 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3514 #ifdef HAVE_CHARDEV_SERIAL
3515 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3518 const char *device
= qemu_opt_get(opts
, "path");
3520 if (device
== NULL
) {
3521 error_setg(errp
, "chardev: serial/tty: no device path given");
3524 backend
->serial
= g_new0(ChardevHostdev
, 1);
3525 backend
->serial
->device
= g_strdup(device
);
3529 #ifdef HAVE_CHARDEV_PARPORT
3530 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3533 const char *device
= qemu_opt_get(opts
, "path");
3535 if (device
== NULL
) {
3536 error_setg(errp
, "chardev: parallel: no device path given");
3539 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3540 backend
->parallel
->device
= g_strdup(device
);
3544 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3547 const char *device
= qemu_opt_get(opts
, "path");
3549 if (device
== NULL
) {
3550 error_setg(errp
, "chardev: pipe: no device path given");
3553 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3554 backend
->pipe
->device
= g_strdup(device
);
3557 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3562 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3564 val
= qemu_opt_get_size(opts
, "size", 0);
3566 backend
->ringbuf
->has_size
= true;
3567 backend
->ringbuf
->size
= val
;
3571 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3574 const char *chardev
= qemu_opt_get(opts
, "chardev");
3576 if (chardev
== NULL
) {
3577 error_setg(errp
, "chardev: mux: no chardev given");
3580 backend
->mux
= g_new0(ChardevMux
, 1);
3581 backend
->mux
->chardev
= g_strdup(chardev
);
3584 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3587 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3588 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3589 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3590 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3591 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3592 const char *path
= qemu_opt_get(opts
, "path");
3593 const char *host
= qemu_opt_get(opts
, "host");
3594 const char *port
= qemu_opt_get(opts
, "port");
3595 SocketAddress
*addr
;
3599 error_setg(errp
, "chardev: socket: no host given");
3603 error_setg(errp
, "chardev: socket: no port given");
3608 backend
->socket
= g_new0(ChardevSocket
, 1);
3610 backend
->socket
->has_nodelay
= true;
3611 backend
->socket
->nodelay
= do_nodelay
;
3612 backend
->socket
->has_server
= true;
3613 backend
->socket
->server
= is_listen
;
3614 backend
->socket
->has_telnet
= true;
3615 backend
->socket
->telnet
= is_telnet
;
3616 backend
->socket
->has_wait
= true;
3617 backend
->socket
->wait
= is_waitconnect
;
3618 backend
->socket
->has_reconnect
= true;
3619 backend
->socket
->reconnect
= reconnect
;
3621 addr
= g_new0(SocketAddress
, 1);
3623 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
3624 addr
->q_unix
= g_new0(UnixSocketAddress
, 1);
3625 addr
->q_unix
->path
= g_strdup(path
);
3627 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3628 addr
->inet
= g_new0(InetSocketAddress
, 1);
3629 addr
->inet
->host
= g_strdup(host
);
3630 addr
->inet
->port
= g_strdup(port
);
3631 addr
->inet
->has_to
= qemu_opt_get(opts
, "to");
3632 addr
->inet
->to
= qemu_opt_get_number(opts
, "to", 0);
3633 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3634 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3635 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3636 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3638 backend
->socket
->addr
= addr
;
3641 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
3644 const char *host
= qemu_opt_get(opts
, "host");
3645 const char *port
= qemu_opt_get(opts
, "port");
3646 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
3647 const char *localport
= qemu_opt_get(opts
, "localport");
3648 bool has_local
= false;
3649 SocketAddress
*addr
;
3651 if (host
== NULL
|| strlen(host
) == 0) {
3654 if (port
== NULL
|| strlen(port
) == 0) {
3655 error_setg(errp
, "chardev: udp: remote port not specified");
3658 if (localport
== NULL
|| strlen(localport
) == 0) {
3663 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
3669 backend
->udp
= g_new0(ChardevUdp
, 1);
3671 addr
= g_new0(SocketAddress
, 1);
3672 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3673 addr
->inet
= g_new0(InetSocketAddress
, 1);
3674 addr
->inet
->host
= g_strdup(host
);
3675 addr
->inet
->port
= g_strdup(port
);
3676 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3677 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3678 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3679 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3680 backend
->udp
->remote
= addr
;
3683 backend
->udp
->has_local
= true;
3684 addr
= g_new0(SocketAddress
, 1);
3685 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3686 addr
->inet
= g_new0(InetSocketAddress
, 1);
3687 addr
->inet
->host
= g_strdup(localaddr
);
3688 addr
->inet
->port
= g_strdup(localport
);
3689 backend
->udp
->local
= addr
;
3693 typedef struct CharDriver
{
3695 ChardevBackendKind kind
;
3696 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3697 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3698 ChardevReturn
*ret
, Error
**errp
);
3701 static GSList
*backends
;
3703 void register_char_driver(const char *name
, ChardevBackendKind kind
,
3704 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
),
3705 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3706 ChardevReturn
*ret
, Error
**errp
))
3710 s
= g_malloc0(sizeof(*s
));
3711 s
->name
= g_strdup(name
);
3716 backends
= g_slist_append(backends
, s
);
3719 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3720 void (*init
)(struct CharDriverState
*s
),
3723 Error
*local_err
= NULL
;
3725 CharDriverState
*chr
;
3727 ChardevReturn
*ret
= NULL
;
3728 ChardevBackend
*backend
;
3729 const char *id
= qemu_opts_id(opts
);
3733 error_setg(errp
, "chardev: no id specified");
3737 if (qemu_opt_get(opts
, "backend") == NULL
) {
3738 error_setg(errp
, "chardev: \"%s\" missing backend",
3739 qemu_opts_id(opts
));
3742 for (i
= backends
; i
; i
= i
->next
) {
3745 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3750 error_setg(errp
, "chardev: backend \"%s\" not found",
3751 qemu_opt_get(opts
, "backend"));
3755 backend
= g_new0(ChardevBackend
, 1);
3757 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3758 bid
= g_strdup_printf("%s-base", id
);
3762 backend
->kind
= cd
->kind
;
3764 cd
->parse(opts
, backend
, &local_err
);
3766 error_propagate(errp
, local_err
);
3770 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3776 qapi_free_ChardevBackend(backend
);
3777 qapi_free_ChardevReturn(ret
);
3778 backend
= g_new0(ChardevBackend
, 1);
3779 backend
->mux
= g_new0(ChardevMux
, 1);
3780 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3781 backend
->mux
->chardev
= g_strdup(bid
);
3782 ret
= qmp_chardev_add(id
, backend
, errp
);
3784 chr
= qemu_chr_find(bid
);
3785 qemu_chr_delete(chr
);
3791 chr
= qemu_chr_find(id
);
3795 qapi_free_ChardevBackend(backend
);
3796 qapi_free_ChardevReturn(ret
);
3801 qemu_opts_del(opts
);
3805 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3808 CharDriverState
*chr
;
3812 if (strstart(filename
, "chardev:", &p
)) {
3813 return qemu_chr_find(p
);
3816 opts
= qemu_chr_parse_compat(label
, filename
);
3820 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3822 error_report_err(err
);
3824 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3825 qemu_chr_fe_claim_no_fail(chr
);
3826 monitor_init(chr
, MONITOR_USE_READLINE
);
3831 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3833 if (chr
->chr_set_echo
) {
3834 chr
->chr_set_echo(chr
, echo
);
3838 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3840 if (chr
->fe_open
== fe_open
) {
3843 chr
->fe_open
= fe_open
;
3844 if (chr
->chr_set_fe_open
) {
3845 chr
->chr_set_fe_open(chr
, fe_open
);
3849 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3851 if (chr
->chr_fe_event
) {
3852 chr
->chr_fe_event(chr
, event
);
3856 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3857 GIOFunc func
, void *user_data
)
3862 if (s
->chr_add_watch
== NULL
) {
3866 src
= s
->chr_add_watch(s
, cond
);
3871 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3872 tag
= g_source_attach(src
, NULL
);
3873 g_source_unref(src
);
3878 int qemu_chr_fe_claim(CharDriverState
*s
)
3880 if (s
->avail_connections
< 1) {
3883 s
->avail_connections
--;
3887 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3889 if (qemu_chr_fe_claim(s
) != 0) {
3890 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3891 __func__
, s
->label
);
3896 void qemu_chr_fe_release(CharDriverState
*s
)
3898 s
->avail_connections
++;
3901 void qemu_chr_delete(CharDriverState
*chr
)
3903 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3904 if (chr
->chr_close
) {
3905 chr
->chr_close(chr
);
3907 g_free(chr
->filename
);
3909 qemu_opts_del(chr
->opts
);
3913 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3915 ChardevInfoList
*chr_list
= NULL
;
3916 CharDriverState
*chr
;
3918 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3919 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3920 info
->value
= g_malloc0(sizeof(*info
->value
));
3921 info
->value
->label
= g_strdup(chr
->label
);
3922 info
->value
->filename
= g_strdup(chr
->filename
);
3923 info
->value
->frontend_open
= chr
->fe_open
;
3925 info
->next
= chr_list
;
3932 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3934 ChardevBackendInfoList
*backend_list
= NULL
;
3935 CharDriver
*c
= NULL
;
3938 for (i
= backends
; i
; i
= i
->next
) {
3939 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3941 info
->value
= g_malloc0(sizeof(*info
->value
));
3942 info
->value
->name
= g_strdup(c
->name
);
3944 info
->next
= backend_list
;
3945 backend_list
= info
;
3948 return backend_list
;
3951 CharDriverState
*qemu_chr_find(const char *name
)
3953 CharDriverState
*chr
;
3955 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3956 if (strcmp(chr
->label
, name
) != 0)
3963 /* Get a character (serial) device interface. */
3964 CharDriverState
*qemu_char_get_next_serial(void)
3966 static int next_serial
;
3967 CharDriverState
*chr
;
3969 /* FIXME: This function needs to go away: use chardev properties! */
3971 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3972 chr
= serial_hds
[next_serial
++];
3973 qemu_chr_fe_claim_no_fail(chr
);
3979 QemuOptsList qemu_chardev_opts
= {
3981 .implied_opt_name
= "backend",
3982 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3986 .type
= QEMU_OPT_STRING
,
3989 .type
= QEMU_OPT_STRING
,
3992 .type
= QEMU_OPT_STRING
,
3995 .type
= QEMU_OPT_STRING
,
3997 .name
= "localaddr",
3998 .type
= QEMU_OPT_STRING
,
4000 .name
= "localport",
4001 .type
= QEMU_OPT_STRING
,
4004 .type
= QEMU_OPT_NUMBER
,
4007 .type
= QEMU_OPT_BOOL
,
4010 .type
= QEMU_OPT_BOOL
,
4013 .type
= QEMU_OPT_BOOL
,
4016 .type
= QEMU_OPT_BOOL
,
4019 .type
= QEMU_OPT_BOOL
,
4021 .name
= "reconnect",
4022 .type
= QEMU_OPT_NUMBER
,
4025 .type
= QEMU_OPT_BOOL
,
4028 .type
= QEMU_OPT_NUMBER
,
4031 .type
= QEMU_OPT_NUMBER
,
4034 .type
= QEMU_OPT_NUMBER
,
4037 .type
= QEMU_OPT_NUMBER
,
4040 .type
= QEMU_OPT_BOOL
,
4043 .type
= QEMU_OPT_BOOL
,
4046 .type
= QEMU_OPT_STRING
,
4049 .type
= QEMU_OPT_NUMBER
,
4052 .type
= QEMU_OPT_SIZE
,
4055 .type
= QEMU_OPT_STRING
,
4057 { /* end of list */ }
4063 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4064 ChardevBackend
*backend
,
4068 ChardevFile
*file
= backend
->file
;
4072 error_setg(errp
, "input file not supported");
4076 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
4077 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4078 if (out
== INVALID_HANDLE_VALUE
) {
4079 error_setg(errp
, "open %s failed", file
->out
);
4082 return qemu_chr_open_win_file(out
);
4085 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4086 ChardevBackend
*backend
,
4090 ChardevHostdev
*serial
= backend
->serial
;
4091 return qemu_chr_open_win_path(serial
->device
, errp
);
4096 static int qmp_chardev_open_file_source(char *src
, int flags
,
4101 TFR(fd
= qemu_open(src
, flags
, 0666));
4103 error_setg_file_open(errp
, errno
, src
);
4108 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4109 ChardevBackend
*backend
,
4113 ChardevFile
*file
= backend
->file
;
4114 int flags
, in
= -1, out
;
4116 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
4117 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4124 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4131 return qemu_chr_open_fd(in
, out
);
4134 #ifdef HAVE_CHARDEV_SERIAL
4135 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4136 ChardevBackend
*backend
,
4140 ChardevHostdev
*serial
= backend
->serial
;
4143 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4147 qemu_set_nonblock(fd
);
4148 return qemu_chr_open_tty_fd(fd
);
4152 #ifdef HAVE_CHARDEV_PARPORT
4153 static CharDriverState
*qmp_chardev_open_parallel(const char *id
,
4154 ChardevBackend
*backend
,
4158 ChardevHostdev
*parallel
= backend
->parallel
;
4161 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4165 return qemu_chr_open_pp_fd(fd
, errp
);
4171 static void socket_try_connect(CharDriverState
*chr
)
4175 if (!qemu_chr_open_socket_fd(chr
, &err
)) {
4176 check_report_connect_error(chr
, err
);
4180 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4182 CharDriverState
*chr
= opaque
;
4183 TCPCharDriver
*s
= chr
->opaque
;
4185 s
->reconnect_timer
= 0;
4191 socket_try_connect(chr
);
4196 static CharDriverState
*qmp_chardev_open_socket(const char *id
,
4197 ChardevBackend
*backend
,
4201 CharDriverState
*chr
;
4203 ChardevSocket
*sock
= backend
->socket
;
4204 SocketAddress
*addr
= sock
->addr
;
4205 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4206 bool is_listen
= sock
->has_server
? sock
->server
: true;
4207 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4208 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4209 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4211 chr
= qemu_chr_alloc();
4212 s
= g_new0(TCPCharDriver
, 1);
4216 s
->is_unix
= addr
->kind
== SOCKET_ADDRESS_KIND_UNIX
;
4217 s
->is_listen
= is_listen
;
4218 s
->is_telnet
= is_telnet
;
4219 s
->do_nodelay
= do_nodelay
;
4220 qapi_copy_SocketAddress(&s
->addr
, sock
->addr
);
4223 chr
->chr_write
= tcp_chr_write
;
4224 chr
->chr_sync_read
= tcp_chr_sync_read
;
4225 chr
->chr_close
= tcp_chr_close
;
4226 chr
->get_msgfds
= tcp_get_msgfds
;
4227 chr
->set_msgfds
= tcp_set_msgfds
;
4228 chr
->chr_add_client
= tcp_chr_add_client
;
4229 chr
->chr_add_watch
= tcp_chr_add_watch
;
4230 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4231 /* be isn't opened until we get a connection */
4232 chr
->explicit_be_open
= true;
4234 chr
->filename
= g_malloc(CHR_MAX_FILENAME_SIZE
);
4235 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
, "disconnected:",
4236 addr
, is_listen
, is_telnet
);
4240 s
->do_telnetopt
= 1;
4242 } else if (reconnect
> 0) {
4243 s
->reconnect_time
= reconnect
;
4246 if (s
->reconnect_time
) {
4247 socket_try_connect(chr
);
4248 } else if (!qemu_chr_open_socket_fd(chr
, errp
)) {
4250 g_free(chr
->filename
);
4255 if (is_listen
&& is_waitconnect
) {
4256 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
4258 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
4259 qemu_set_nonblock(s
->listen_fd
);
4265 static CharDriverState
*qmp_chardev_open_udp(const char *id
,
4266 ChardevBackend
*backend
,
4270 ChardevUdp
*udp
= backend
->udp
;
4273 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
4277 return qemu_chr_open_udp_fd(fd
);
4280 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4283 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4284 CharDriverState
*chr
= NULL
;
4285 Error
*local_err
= NULL
;
4289 chr
= qemu_chr_find(id
);
4291 error_setg(errp
, "Chardev '%s' already exists", id
);
4296 for (i
= backends
; i
; i
= i
->next
) {
4299 if (cd
->kind
== backend
->kind
&& cd
->create
) {
4300 chr
= cd
->create(id
, backend
, ret
, &local_err
);
4302 error_propagate(errp
, local_err
);
4310 switch (backend
->kind
) {
4311 case CHARDEV_BACKEND_KIND_FILE
:
4314 case CHARDEV_BACKEND_KIND_SERIAL
:
4317 case CHARDEV_BACKEND_KIND_PARALLEL
:
4320 case CHARDEV_BACKEND_KIND_PIPE
:
4323 case CHARDEV_BACKEND_KIND_SOCKET
:
4326 case CHARDEV_BACKEND_KIND_UDP
:
4329 case CHARDEV_BACKEND_KIND_PTY
:
4332 case CHARDEV_BACKEND_KIND_NULL
:
4335 case CHARDEV_BACKEND_KIND_MUX
:
4338 case CHARDEV_BACKEND_KIND_MSMOUSE
:
4341 case CHARDEV_BACKEND_KIND_BRAILLE
:
4344 case CHARDEV_BACKEND_KIND_TESTDEV
:
4347 case CHARDEV_BACKEND_KIND_STDIO
:
4350 case CHARDEV_BACKEND_KIND_CONSOLE
:
4353 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4356 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4359 case CHARDEV_BACKEND_KIND_VC
:
4362 case CHARDEV_BACKEND_KIND_RINGBUF
:
4363 case CHARDEV_BACKEND_KIND_MEMORY
:
4364 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, &local_err
);
4367 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4372 * Character backend open hasn't been fully converted to the Error
4373 * API. Some opens fail without setting an error. Set a generic
4375 * TODO full conversion to Error API
4379 error_propagate(errp
, local_err
);
4381 error_setg(errp
, "Failed to create chardev");
4387 chr
->label
= g_strdup(id
);
4388 chr
->avail_connections
=
4389 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4390 if (!chr
->filename
) {
4391 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4393 if (!chr
->explicit_be_open
) {
4394 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4396 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4404 void qmp_chardev_remove(const char *id
, Error
**errp
)
4406 CharDriverState
*chr
;
4408 chr
= qemu_chr_find(id
);
4410 error_setg(errp
, "Chardev '%s' not found", id
);
4413 if (chr
->chr_can_read
|| chr
->chr_read
||
4414 chr
->chr_event
|| chr
->handler_opaque
) {
4415 error_setg(errp
, "Chardev '%s' is busy", id
);
4418 qemu_chr_delete(chr
);
4421 static void register_types(void)
4423 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
,
4424 qemu_chr_open_null
);
4425 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4426 qemu_chr_parse_socket
, qmp_chardev_open_socket
);
4427 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
,
4428 qmp_chardev_open_udp
);
4429 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4430 qemu_chr_parse_ringbuf
, NULL
);
4431 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4432 qemu_chr_parse_file_out
, qmp_chardev_open_file
);
4433 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4434 qemu_chr_parse_stdio
, qemu_chr_open_stdio
);
4435 #if defined HAVE_CHARDEV_SERIAL
4436 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4437 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4438 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4439 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4441 #ifdef HAVE_CHARDEV_PARPORT
4442 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4443 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4444 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4445 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4447 #ifdef HAVE_CHARDEV_PTY
4448 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
,
4452 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
,
4453 qemu_chr_open_win_con
);
4455 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4456 qemu_chr_parse_pipe
, qemu_chr_open_pipe
);
4457 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
,
4459 /* Bug-compatibility: */
4460 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4461 qemu_chr_parse_ringbuf
, NULL
);
4462 /* this must be done after machine init, since we register FEs with muxes
4463 * as part of realize functions like serial_isa_realizefn when -nographic
4466 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4469 type_init(register_types
);