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(ChardevStdio
*opts
)
1173 CharDriverState
*chr
;
1174 struct sigaction act
;
1176 if (is_daemonized()) {
1177 error_report("cannot use stdio with -daemonize");
1182 error_report("cannot use stdio by multiple character devices");
1186 stdio_in_use
= true;
1187 old_fd0_flags
= fcntl(0, F_GETFL
);
1188 tcgetattr(0, &oldtty
);
1189 qemu_set_nonblock(0);
1192 memset(&act
, 0, sizeof(act
));
1193 act
.sa_handler
= term_stdio_handler
;
1194 sigaction(SIGCONT
, &act
, NULL
);
1196 chr
= qemu_chr_open_fd(0, 1);
1197 chr
->chr_close
= qemu_chr_close_stdio
;
1198 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1199 if (opts
->has_signal
) {
1200 stdio_allow_signal
= opts
->signal
;
1202 qemu_chr_fe_set_echo(chr
, false);
1207 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1208 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1209 || defined(__GLIBC__)
1211 #define HAVE_CHARDEV_SERIAL 1
1212 #define HAVE_CHARDEV_PTY 1
1218 /* Protected by the CharDriverState chr_write_lock. */
1224 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1225 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1227 static gboolean
pty_chr_timer(gpointer opaque
)
1229 struct CharDriverState
*chr
= opaque
;
1230 PtyCharDriver
*s
= chr
->opaque
;
1232 qemu_mutex_lock(&chr
->chr_write_lock
);
1235 if (!s
->connected
) {
1237 pty_chr_update_read_handler_locked(chr
);
1239 qemu_mutex_unlock(&chr
->chr_write_lock
);
1243 /* Called with chr_write_lock held. */
1244 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1246 PtyCharDriver
*s
= chr
->opaque
;
1249 g_source_remove(s
->timer_tag
);
1254 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1256 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1260 /* Called with chr_write_lock held. */
1261 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1263 PtyCharDriver
*s
= chr
->opaque
;
1266 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1267 pfd
.events
= G_IO_OUT
;
1270 if (pfd
.revents
& G_IO_HUP
) {
1271 pty_chr_state(chr
, 0);
1273 pty_chr_state(chr
, 1);
1277 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1279 qemu_mutex_lock(&chr
->chr_write_lock
);
1280 pty_chr_update_read_handler_locked(chr
);
1281 qemu_mutex_unlock(&chr
->chr_write_lock
);
1284 /* Called with chr_write_lock held. */
1285 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1287 PtyCharDriver
*s
= chr
->opaque
;
1289 if (!s
->connected
) {
1290 /* guest sends data, check for (re-)connect */
1291 pty_chr_update_read_handler_locked(chr
);
1292 if (!s
->connected
) {
1296 return io_channel_send(s
->fd
, buf
, len
);
1299 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1301 PtyCharDriver
*s
= chr
->opaque
;
1302 if (!s
->connected
) {
1305 return g_io_create_watch(s
->fd
, cond
);
1308 static int pty_chr_read_poll(void *opaque
)
1310 CharDriverState
*chr
= opaque
;
1311 PtyCharDriver
*s
= chr
->opaque
;
1313 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1314 return s
->read_bytes
;
1317 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1319 CharDriverState
*chr
= opaque
;
1320 PtyCharDriver
*s
= chr
->opaque
;
1322 uint8_t buf
[READ_BUF_LEN
];
1326 if (len
> s
->read_bytes
)
1327 len
= s
->read_bytes
;
1331 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1332 if (status
!= G_IO_STATUS_NORMAL
) {
1333 pty_chr_state(chr
, 0);
1336 pty_chr_state(chr
, 1);
1337 qemu_chr_be_write(chr
, buf
, size
);
1342 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1344 CharDriverState
*chr
= opaque
;
1345 PtyCharDriver
*s
= chr
->opaque
;
1348 qemu_chr_be_generic_open(chr
);
1352 /* Called with chr_write_lock held. */
1353 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1355 PtyCharDriver
*s
= chr
->opaque
;
1359 g_source_remove(s
->open_tag
);
1362 remove_fd_in_watch(chr
);
1364 /* (re-)connect poll interval for idle guests: once per second.
1365 * We check more frequently in case the guests sends data to
1366 * the virtual device linked to our pty. */
1367 pty_chr_rearm_timer(chr
, 1000);
1370 g_source_remove(s
->timer_tag
);
1373 if (!s
->connected
) {
1374 g_assert(s
->open_tag
== 0);
1376 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1378 if (!chr
->fd_in_tag
) {
1379 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1385 static void pty_chr_close(struct CharDriverState
*chr
)
1387 PtyCharDriver
*s
= chr
->opaque
;
1390 qemu_mutex_lock(&chr
->chr_write_lock
);
1391 pty_chr_state(chr
, 0);
1392 fd
= g_io_channel_unix_get_fd(s
->fd
);
1393 g_io_channel_unref(s
->fd
);
1396 g_source_remove(s
->timer_tag
);
1399 qemu_mutex_unlock(&chr
->chr_write_lock
);
1401 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1404 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1405 ChardevBackend
*backend
,
1409 CharDriverState
*chr
;
1411 int master_fd
, slave_fd
;
1412 char pty_name
[PATH_MAX
];
1414 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1415 if (master_fd
< 0) {
1416 error_setg_errno(errp
, errno
, "Failed to create PTY");
1421 qemu_set_nonblock(master_fd
);
1423 chr
= qemu_chr_alloc();
1425 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1426 ret
->pty
= g_strdup(pty_name
);
1427 ret
->has_pty
= true;
1429 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1432 s
= g_new0(PtyCharDriver
, 1);
1434 chr
->chr_write
= pty_chr_write
;
1435 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1436 chr
->chr_close
= pty_chr_close
;
1437 chr
->chr_add_watch
= pty_chr_add_watch
;
1438 chr
->explicit_be_open
= true;
1440 s
->fd
= io_channel_from_fd(master_fd
);
1446 static void tty_serial_init(int fd
, int speed
,
1447 int parity
, int data_bits
, int stop_bits
)
1453 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1454 speed
, parity
, data_bits
, stop_bits
);
1456 tcgetattr (fd
, &tty
);
1458 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1459 speed
= speed
* 10 / 11;
1476 /* Non-Posix values follow. They may be unsupported on some systems. */
1478 check_speed(115200);
1480 check_speed(230400);
1483 check_speed(460800);
1486 check_speed(500000);
1489 check_speed(576000);
1492 check_speed(921600);
1495 check_speed(1000000);
1498 check_speed(1152000);
1501 check_speed(1500000);
1504 check_speed(2000000);
1507 check_speed(2500000);
1510 check_speed(3000000);
1513 check_speed(3500000);
1516 check_speed(4000000);
1521 cfsetispeed(&tty
, spd
);
1522 cfsetospeed(&tty
, spd
);
1524 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1525 |INLCR
|IGNCR
|ICRNL
|IXON
);
1526 tty
.c_oflag
|= OPOST
;
1527 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1528 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1549 tty
.c_cflag
|= PARENB
;
1552 tty
.c_cflag
|= PARENB
| PARODD
;
1556 tty
.c_cflag
|= CSTOPB
;
1558 tcsetattr (fd
, TCSANOW
, &tty
);
1561 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1563 FDCharDriver
*s
= chr
->opaque
;
1566 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1568 QEMUSerialSetParams
*ssp
= arg
;
1569 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1570 ssp
->speed
, ssp
->parity
,
1571 ssp
->data_bits
, ssp
->stop_bits
);
1574 case CHR_IOCTL_SERIAL_SET_BREAK
:
1576 int enable
= *(int *)arg
;
1578 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1582 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1585 int *targ
= (int *)arg
;
1586 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1588 if (sarg
& TIOCM_CTS
)
1589 *targ
|= CHR_TIOCM_CTS
;
1590 if (sarg
& TIOCM_CAR
)
1591 *targ
|= CHR_TIOCM_CAR
;
1592 if (sarg
& TIOCM_DSR
)
1593 *targ
|= CHR_TIOCM_DSR
;
1594 if (sarg
& TIOCM_RI
)
1595 *targ
|= CHR_TIOCM_RI
;
1596 if (sarg
& TIOCM_DTR
)
1597 *targ
|= CHR_TIOCM_DTR
;
1598 if (sarg
& TIOCM_RTS
)
1599 *targ
|= CHR_TIOCM_RTS
;
1602 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1604 int sarg
= *(int *)arg
;
1606 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1607 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1608 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1609 if (sarg
& CHR_TIOCM_CTS
)
1611 if (sarg
& CHR_TIOCM_CAR
)
1613 if (sarg
& CHR_TIOCM_DSR
)
1615 if (sarg
& CHR_TIOCM_RI
)
1617 if (sarg
& CHR_TIOCM_DTR
)
1619 if (sarg
& CHR_TIOCM_RTS
)
1621 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1630 static void qemu_chr_close_tty(CharDriverState
*chr
)
1632 FDCharDriver
*s
= chr
->opaque
;
1636 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1646 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1648 CharDriverState
*chr
;
1650 tty_serial_init(fd
, 115200, 'N', 8, 1);
1651 chr
= qemu_chr_open_fd(fd
, fd
);
1652 chr
->chr_ioctl
= tty_serial_ioctl
;
1653 chr
->chr_close
= qemu_chr_close_tty
;
1656 #endif /* __linux__ || __sun__ */
1658 #if defined(__linux__)
1660 #define HAVE_CHARDEV_PARPORT 1
1665 } ParallelCharDriver
;
1667 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1669 if (s
->mode
!= mode
) {
1671 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1678 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1680 ParallelCharDriver
*drv
= chr
->opaque
;
1685 case CHR_IOCTL_PP_READ_DATA
:
1686 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1688 *(uint8_t *)arg
= b
;
1690 case CHR_IOCTL_PP_WRITE_DATA
:
1691 b
= *(uint8_t *)arg
;
1692 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1695 case CHR_IOCTL_PP_READ_CONTROL
:
1696 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1698 /* Linux gives only the lowest bits, and no way to know data
1699 direction! For better compatibility set the fixed upper
1701 *(uint8_t *)arg
= b
| 0xc0;
1703 case CHR_IOCTL_PP_WRITE_CONTROL
:
1704 b
= *(uint8_t *)arg
;
1705 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1708 case CHR_IOCTL_PP_READ_STATUS
:
1709 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1711 *(uint8_t *)arg
= b
;
1713 case CHR_IOCTL_PP_DATA_DIR
:
1714 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1717 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1718 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1719 struct ParallelIOArg
*parg
= arg
;
1720 int n
= read(fd
, parg
->buffer
, parg
->count
);
1721 if (n
!= parg
->count
) {
1726 case CHR_IOCTL_PP_EPP_READ
:
1727 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1728 struct ParallelIOArg
*parg
= arg
;
1729 int n
= read(fd
, parg
->buffer
, parg
->count
);
1730 if (n
!= parg
->count
) {
1735 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1736 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1737 struct ParallelIOArg
*parg
= arg
;
1738 int n
= write(fd
, parg
->buffer
, parg
->count
);
1739 if (n
!= parg
->count
) {
1744 case CHR_IOCTL_PP_EPP_WRITE
:
1745 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1746 struct ParallelIOArg
*parg
= arg
;
1747 int n
= write(fd
, parg
->buffer
, parg
->count
);
1748 if (n
!= parg
->count
) {
1759 static void pp_close(CharDriverState
*chr
)
1761 ParallelCharDriver
*drv
= chr
->opaque
;
1764 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1765 ioctl(fd
, PPRELEASE
);
1768 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1771 static CharDriverState
*qemu_chr_open_pp_fd(int fd
, Error
**errp
)
1773 CharDriverState
*chr
;
1774 ParallelCharDriver
*drv
;
1776 if (ioctl(fd
, PPCLAIM
) < 0) {
1777 error_setg_errno(errp
, errno
, "not a parallel port");
1782 drv
= g_new0(ParallelCharDriver
, 1);
1784 drv
->mode
= IEEE1284_MODE_COMPAT
;
1786 chr
= qemu_chr_alloc();
1787 chr
->chr_write
= null_chr_write
;
1788 chr
->chr_ioctl
= pp_ioctl
;
1789 chr
->chr_close
= pp_close
;
1794 #endif /* __linux__ */
1796 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1798 #define HAVE_CHARDEV_PARPORT 1
1800 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1802 int fd
= (int)(intptr_t)chr
->opaque
;
1806 case CHR_IOCTL_PP_READ_DATA
:
1807 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1809 *(uint8_t *)arg
= b
;
1811 case CHR_IOCTL_PP_WRITE_DATA
:
1812 b
= *(uint8_t *)arg
;
1813 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1816 case CHR_IOCTL_PP_READ_CONTROL
:
1817 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1819 *(uint8_t *)arg
= b
;
1821 case CHR_IOCTL_PP_WRITE_CONTROL
:
1822 b
= *(uint8_t *)arg
;
1823 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1826 case CHR_IOCTL_PP_READ_STATUS
:
1827 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1829 *(uint8_t *)arg
= b
;
1837 static CharDriverState
*qemu_chr_open_pp_fd(int fd
, Error
**errp
)
1839 CharDriverState
*chr
;
1841 chr
= qemu_chr_alloc();
1842 chr
->opaque
= (void *)(intptr_t)fd
;
1843 chr
->chr_write
= null_chr_write
;
1844 chr
->chr_ioctl
= pp_ioctl
;
1845 chr
->explicit_be_open
= true;
1852 #define HAVE_CHARDEV_SERIAL 1
1856 HANDLE hcom
, hrecv
, hsend
;
1861 /* Protected by the CharDriverState chr_write_lock. */
1867 HANDLE hInputReadyEvent
;
1868 HANDLE hInputDoneEvent
;
1869 HANDLE hInputThread
;
1870 uint8_t win_stdio_buf
;
1871 } WinStdioCharState
;
1873 #define NSENDBUF 2048
1874 #define NRECVBUF 2048
1875 #define MAXCONNECT 1
1876 #define NTIMEOUT 5000
1878 static int win_chr_poll(void *opaque
);
1879 static int win_chr_pipe_poll(void *opaque
);
1881 static void win_chr_close(CharDriverState
*chr
)
1883 WinCharState
*s
= chr
->opaque
;
1886 CloseHandle(s
->hsend
);
1890 CloseHandle(s
->hrecv
);
1894 CloseHandle(s
->hcom
);
1898 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1900 qemu_del_polling_cb(win_chr_poll
, chr
);
1902 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1905 static int win_chr_init(CharDriverState
*chr
, const char *filename
, Error
**errp
)
1907 WinCharState
*s
= chr
->opaque
;
1909 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1914 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1916 error_setg(errp
, "Failed CreateEvent");
1919 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1921 error_setg(errp
, "Failed CreateEvent");
1925 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1926 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1927 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1928 error_setg(errp
, "Failed CreateFile (%lu)", GetLastError());
1933 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1934 error_setg(errp
, "Failed SetupComm");
1938 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1939 size
= sizeof(COMMCONFIG
);
1940 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1941 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1942 CommConfigDialog(filename
, NULL
, &comcfg
);
1944 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1945 error_setg(errp
, "Failed SetCommState");
1949 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1950 error_setg(errp
, "Failed SetCommMask");
1954 cto
.ReadIntervalTimeout
= MAXDWORD
;
1955 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1956 error_setg(errp
, "Failed SetCommTimeouts");
1960 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1961 error_setg(errp
, "Failed ClearCommError");
1964 qemu_add_polling_cb(win_chr_poll
, chr
);
1972 /* Called with chr_write_lock held. */
1973 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1975 WinCharState
*s
= chr
->opaque
;
1976 DWORD len
, ret
, size
, err
;
1979 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1980 s
->osend
.hEvent
= s
->hsend
;
1983 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1985 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1987 err
= GetLastError();
1988 if (err
== ERROR_IO_PENDING
) {
1989 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2007 static int win_chr_read_poll(CharDriverState
*chr
)
2009 WinCharState
*s
= chr
->opaque
;
2011 s
->max_size
= qemu_chr_be_can_write(chr
);
2015 static void win_chr_readfile(CharDriverState
*chr
)
2017 WinCharState
*s
= chr
->opaque
;
2019 uint8_t buf
[READ_BUF_LEN
];
2022 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2023 s
->orecv
.hEvent
= s
->hrecv
;
2024 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2026 err
= GetLastError();
2027 if (err
== ERROR_IO_PENDING
) {
2028 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2033 qemu_chr_be_write(chr
, buf
, size
);
2037 static void win_chr_read(CharDriverState
*chr
)
2039 WinCharState
*s
= chr
->opaque
;
2041 if (s
->len
> s
->max_size
)
2042 s
->len
= s
->max_size
;
2046 win_chr_readfile(chr
);
2049 static int win_chr_poll(void *opaque
)
2051 CharDriverState
*chr
= opaque
;
2052 WinCharState
*s
= chr
->opaque
;
2056 ClearCommError(s
->hcom
, &comerr
, &status
);
2057 if (status
.cbInQue
> 0) {
2058 s
->len
= status
.cbInQue
;
2059 win_chr_read_poll(chr
);
2066 static CharDriverState
*qemu_chr_open_win_path(const char *filename
,
2069 CharDriverState
*chr
;
2072 chr
= qemu_chr_alloc();
2073 s
= g_new0(WinCharState
, 1);
2075 chr
->chr_write
= win_chr_write
;
2076 chr
->chr_close
= win_chr_close
;
2078 if (win_chr_init(chr
, filename
, errp
) < 0) {
2086 static int win_chr_pipe_poll(void *opaque
)
2088 CharDriverState
*chr
= opaque
;
2089 WinCharState
*s
= chr
->opaque
;
2092 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2095 win_chr_read_poll(chr
);
2102 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
,
2105 WinCharState
*s
= chr
->opaque
;
2109 char openname
[CHR_MAX_FILENAME_SIZE
];
2113 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2115 error_setg(errp
, "Failed CreateEvent");
2118 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2120 error_setg(errp
, "Failed CreateEvent");
2124 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2125 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2126 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2128 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2129 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2130 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
2135 ZeroMemory(&ov
, sizeof(ov
));
2136 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2137 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2139 error_setg(errp
, "Failed ConnectNamedPipe");
2143 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2145 error_setg(errp
, "Failed GetOverlappedResult");
2147 CloseHandle(ov
.hEvent
);
2154 CloseHandle(ov
.hEvent
);
2157 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2166 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
2167 ChardevBackend
*backend
,
2171 ChardevHostdev
*opts
= backend
->pipe
;
2172 const char *filename
= opts
->device
;
2173 CharDriverState
*chr
;
2176 chr
= qemu_chr_alloc();
2177 s
= g_new0(WinCharState
, 1);
2179 chr
->chr_write
= win_chr_write
;
2180 chr
->chr_close
= win_chr_close
;
2182 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
2190 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2192 CharDriverState
*chr
;
2195 chr
= qemu_chr_alloc();
2196 s
= g_new0(WinCharState
, 1);
2199 chr
->chr_write
= win_chr_write
;
2203 static CharDriverState
*qemu_chr_open_win_con(void)
2205 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2208 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2210 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2217 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2227 static void win_stdio_wait_func(void *opaque
)
2229 CharDriverState
*chr
= opaque
;
2230 WinStdioCharState
*stdio
= chr
->opaque
;
2231 INPUT_RECORD buf
[4];
2236 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2239 /* Avoid error storm */
2240 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2244 for (i
= 0; i
< dwSize
; i
++) {
2245 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2247 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2249 if (kev
->uChar
.AsciiChar
!= 0) {
2250 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2251 if (qemu_chr_be_can_write(chr
)) {
2252 uint8_t c
= kev
->uChar
.AsciiChar
;
2253 qemu_chr_be_write(chr
, &c
, 1);
2261 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2263 CharDriverState
*chr
= param
;
2264 WinStdioCharState
*stdio
= chr
->opaque
;
2270 /* Wait for one byte */
2271 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2273 /* Exit in case of error, continue if nothing read */
2281 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2282 if (stdio
->win_stdio_buf
== '\r') {
2286 /* Signal the main thread and wait until the byte was eaten */
2287 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2290 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2296 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2300 static void win_stdio_thread_wait_func(void *opaque
)
2302 CharDriverState
*chr
= opaque
;
2303 WinStdioCharState
*stdio
= chr
->opaque
;
2305 if (qemu_chr_be_can_write(chr
)) {
2306 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2309 SetEvent(stdio
->hInputDoneEvent
);
2312 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2314 WinStdioCharState
*stdio
= chr
->opaque
;
2317 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2320 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2322 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2326 static void win_stdio_close(CharDriverState
*chr
)
2328 WinStdioCharState
*stdio
= chr
->opaque
;
2330 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2331 CloseHandle(stdio
->hInputReadyEvent
);
2333 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2334 CloseHandle(stdio
->hInputDoneEvent
);
2336 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2337 TerminateThread(stdio
->hInputThread
, 0);
2340 g_free(chr
->opaque
);
2344 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2346 CharDriverState
*chr
;
2347 WinStdioCharState
*stdio
;
2351 chr
= qemu_chr_alloc();
2352 stdio
= g_new0(WinStdioCharState
, 1);
2354 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2355 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2356 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2360 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2362 chr
->opaque
= stdio
;
2363 chr
->chr_write
= win_stdio_write
;
2364 chr
->chr_close
= win_stdio_close
;
2367 if (qemu_add_wait_object(stdio
->hStdIn
,
2368 win_stdio_wait_func
, chr
)) {
2369 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2374 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2375 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2376 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2379 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2380 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2381 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2382 fprintf(stderr
, "cannot create stdio thread or event\n");
2385 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2386 win_stdio_thread_wait_func
, chr
)) {
2387 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2391 dwMode
|= ENABLE_LINE_INPUT
;
2394 /* set the terminal in raw mode */
2395 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2396 dwMode
|= ENABLE_PROCESSED_INPUT
;
2399 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2401 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2402 qemu_chr_fe_set_echo(chr
, false);
2406 #endif /* !_WIN32 */
2409 /***********************************************************/
2410 /* UDP Net console */
2415 uint8_t buf
[READ_BUF_LEN
];
2421 /* Called with chr_write_lock held. */
2422 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2424 NetCharDriver
*s
= chr
->opaque
;
2425 gsize bytes_written
;
2428 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2429 if (status
== G_IO_STATUS_EOF
) {
2431 } else if (status
!= G_IO_STATUS_NORMAL
) {
2435 return bytes_written
;
2438 static int udp_chr_read_poll(void *opaque
)
2440 CharDriverState
*chr
= opaque
;
2441 NetCharDriver
*s
= chr
->opaque
;
2443 s
->max_size
= qemu_chr_be_can_write(chr
);
2445 /* If there were any stray characters in the queue process them
2448 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2449 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2451 s
->max_size
= qemu_chr_be_can_write(chr
);
2456 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2458 CharDriverState
*chr
= opaque
;
2459 NetCharDriver
*s
= chr
->opaque
;
2460 gsize bytes_read
= 0;
2463 if (s
->max_size
== 0) {
2466 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2468 s
->bufcnt
= bytes_read
;
2469 s
->bufptr
= s
->bufcnt
;
2470 if (status
!= G_IO_STATUS_NORMAL
) {
2471 remove_fd_in_watch(chr
);
2476 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2477 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2479 s
->max_size
= qemu_chr_be_can_write(chr
);
2485 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2487 NetCharDriver
*s
= chr
->opaque
;
2489 remove_fd_in_watch(chr
);
2491 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2496 static void udp_chr_close(CharDriverState
*chr
)
2498 NetCharDriver
*s
= chr
->opaque
;
2500 remove_fd_in_watch(chr
);
2502 g_io_channel_unref(s
->chan
);
2506 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2509 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2511 CharDriverState
*chr
= NULL
;
2512 NetCharDriver
*s
= NULL
;
2514 chr
= qemu_chr_alloc();
2515 s
= g_new0(NetCharDriver
, 1);
2518 s
->chan
= io_channel_from_socket(s
->fd
);
2522 chr
->chr_write
= udp_chr_write
;
2523 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2524 chr
->chr_close
= udp_chr_close
;
2525 /* be isn't opened until we get a connection */
2526 chr
->explicit_be_open
= true;
2530 /***********************************************************/
2531 /* TCP Net console */
2535 GIOChannel
*chan
, *listen_chan
;
2544 int read_msgfds_num
;
2546 int write_msgfds_num
;
2548 SocketAddress
*addr
;
2552 guint reconnect_timer
;
2553 int64_t reconnect_time
;
2554 bool connect_err_reported
;
2557 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2559 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2561 TCPCharDriver
*s
= chr
->opaque
;
2562 assert(s
->connected
== 0);
2563 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2564 socket_reconnect_timeout
, chr
);
2567 static void check_report_connect_error(CharDriverState
*chr
,
2570 TCPCharDriver
*s
= chr
->opaque
;
2572 if (!s
->connect_err_reported
) {
2573 error_report("Unable to connect character device %s: %s",
2574 chr
->label
, error_get_pretty(err
));
2575 s
->connect_err_reported
= true;
2577 qemu_chr_socket_restart_timer(chr
);
2580 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2583 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2585 TCPCharDriver
*s
= chr
->opaque
;
2590 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2591 char control
[CMSG_SPACE(fd_size
)];
2592 struct cmsghdr
*cmsg
;
2594 memset(&msgh
, 0, sizeof(msgh
));
2595 memset(control
, 0, sizeof(control
));
2597 /* set the payload */
2598 iov
.iov_base
= (uint8_t *) buf
;
2601 msgh
.msg_iov
= &iov
;
2602 msgh
.msg_iovlen
= 1;
2604 msgh
.msg_control
= control
;
2605 msgh
.msg_controllen
= sizeof(control
);
2607 cmsg
= CMSG_FIRSTHDR(&msgh
);
2609 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2610 cmsg
->cmsg_level
= SOL_SOCKET
;
2611 cmsg
->cmsg_type
= SCM_RIGHTS
;
2612 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2615 r
= sendmsg(s
->fd
, &msgh
, 0);
2616 } while (r
< 0 && errno
== EINTR
);
2618 /* free the written msgfds, no matter what */
2619 if (s
->write_msgfds_num
) {
2620 g_free(s
->write_msgfds
);
2621 s
->write_msgfds
= 0;
2622 s
->write_msgfds_num
= 0;
2629 /* Called with chr_write_lock held. */
2630 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2632 TCPCharDriver
*s
= chr
->opaque
;
2635 if (s
->is_unix
&& s
->write_msgfds_num
) {
2636 return unix_send_msgfds(chr
, buf
, len
);
2640 return io_channel_send(s
->chan
, buf
, len
);
2643 /* XXX: indicate an error ? */
2648 static int tcp_chr_read_poll(void *opaque
)
2650 CharDriverState
*chr
= opaque
;
2651 TCPCharDriver
*s
= chr
->opaque
;
2654 s
->max_size
= qemu_chr_be_can_write(chr
);
2659 #define IAC_BREAK 243
2660 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2662 uint8_t *buf
, int *size
)
2664 /* Handle any telnet client's basic IAC options to satisfy char by
2665 * char mode with no echo. All IAC options will be removed from
2666 * the buf and the do_telnetopt variable will be used to track the
2667 * state of the width of the IAC information.
2669 * IAC commands come in sets of 3 bytes with the exception of the
2670 * "IAC BREAK" command and the double IAC.
2676 for (i
= 0; i
< *size
; i
++) {
2677 if (s
->do_telnetopt
> 1) {
2678 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2679 /* Double IAC means send an IAC */
2683 s
->do_telnetopt
= 1;
2685 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2686 /* Handle IAC break commands by sending a serial break */
2687 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2692 if (s
->do_telnetopt
>= 4) {
2693 s
->do_telnetopt
= 1;
2696 if ((unsigned char)buf
[i
] == IAC
) {
2697 s
->do_telnetopt
= 2;
2708 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2710 TCPCharDriver
*s
= chr
->opaque
;
2711 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2713 assert(num
<= TCP_MAX_FDS
);
2718 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2720 /* Close unused fds */
2721 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2722 close(s
->read_msgfds
[i
]);
2725 g_free(s
->read_msgfds
);
2727 s
->read_msgfds_num
= 0;
2733 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2735 TCPCharDriver
*s
= chr
->opaque
;
2737 /* clear old pending fd array */
2738 g_free(s
->write_msgfds
);
2741 s
->write_msgfds
= g_new(int, num
);
2742 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2745 s
->write_msgfds_num
= num
;
2751 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2753 TCPCharDriver
*s
= chr
->opaque
;
2754 struct cmsghdr
*cmsg
;
2756 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2759 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2760 cmsg
->cmsg_level
!= SOL_SOCKET
||
2761 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2765 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2771 /* close and clean read_msgfds */
2772 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2773 close(s
->read_msgfds
[i
]);
2776 if (s
->read_msgfds_num
) {
2777 g_free(s
->read_msgfds
);
2780 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2781 s
->read_msgfds
= g_malloc(fd_size
);
2782 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2784 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2785 int fd
= s
->read_msgfds
[i
];
2790 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2793 #ifndef MSG_CMSG_CLOEXEC
2794 qemu_set_cloexec(fd
);
2800 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2802 TCPCharDriver
*s
= chr
->opaque
;
2803 struct msghdr msg
= { NULL
, };
2804 struct iovec iov
[1];
2806 struct cmsghdr cmsg
;
2807 char control
[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS
)];
2812 iov
[0].iov_base
= buf
;
2813 iov
[0].iov_len
= len
;
2817 msg
.msg_control
= &msg_control
;
2818 msg
.msg_controllen
= sizeof(msg_control
);
2820 #ifdef MSG_CMSG_CLOEXEC
2821 flags
|= MSG_CMSG_CLOEXEC
;
2824 ret
= recvmsg(s
->fd
, &msg
, flags
);
2825 } while (ret
== -1 && errno
== EINTR
);
2827 if (ret
> 0 && s
->is_unix
) {
2828 unix_process_msgfd(chr
, &msg
);
2834 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2836 TCPCharDriver
*s
= chr
->opaque
;
2840 ret
= qemu_recv(s
->fd
, buf
, len
, 0);
2841 } while (ret
== -1 && socket_error() == EINTR
);
2847 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2849 TCPCharDriver
*s
= chr
->opaque
;
2850 return g_io_create_watch(s
->chan
, cond
);
2853 static void tcp_chr_disconnect(CharDriverState
*chr
)
2855 TCPCharDriver
*s
= chr
->opaque
;
2858 if (s
->listen_chan
) {
2859 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2860 tcp_chr_accept
, chr
);
2862 remove_fd_in_watch(chr
);
2863 g_io_channel_unref(s
->chan
);
2867 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2868 "disconnected:", s
->addr
, s
->is_listen
, s
->is_telnet
);
2869 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2870 if (s
->reconnect_time
) {
2871 qemu_chr_socket_restart_timer(chr
);
2875 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2877 CharDriverState
*chr
= opaque
;
2878 TCPCharDriver
*s
= chr
->opaque
;
2879 uint8_t buf
[READ_BUF_LEN
];
2882 if (!s
->connected
|| s
->max_size
<= 0) {
2886 if (len
> s
->max_size
)
2888 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2891 socket_error() != EAGAIN
&& socket_error() != EWOULDBLOCK
)) {
2892 /* connection closed */
2893 tcp_chr_disconnect(chr
);
2894 } else if (size
> 0) {
2895 if (s
->do_telnetopt
)
2896 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2898 qemu_chr_be_write(chr
, buf
, size
);
2904 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2906 TCPCharDriver
*s
= chr
->opaque
;
2909 if (!s
->connected
) {
2913 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2915 /* connection closed */
2916 tcp_chr_disconnect(chr
);
2923 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2925 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2928 chr
->avail_connections
= 1;
2935 static void tcp_chr_connect(void *opaque
)
2937 CharDriverState
*chr
= opaque
;
2938 TCPCharDriver
*s
= chr
->opaque
;
2939 struct sockaddr_storage ss
, ps
;
2940 socklen_t ss_len
= sizeof(ss
), ps_len
= sizeof(ps
);
2942 memset(&ss
, 0, ss_len
);
2943 if (getsockname(s
->fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2944 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2945 "Error in getsockname: %s\n", strerror(errno
));
2946 } else if (getpeername(s
->fd
, (struct sockaddr
*) &ps
, &ps_len
) != 0) {
2947 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2948 "Error in getpeername: %s\n", strerror(errno
));
2950 sockaddr_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2951 &ss
, ss_len
, &ps
, ps_len
,
2952 s
->is_listen
, s
->is_telnet
);
2957 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2960 qemu_chr_be_generic_open(chr
);
2963 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2965 TCPCharDriver
*s
= chr
->opaque
;
2967 remove_fd_in_watch(chr
);
2969 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2974 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2975 static void tcp_chr_telnet_init(int fd
)
2978 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2979 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2980 send(fd
, (char *)buf
, 3, 0);
2981 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2982 send(fd
, (char *)buf
, 3, 0);
2983 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2984 send(fd
, (char *)buf
, 3, 0);
2985 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2986 send(fd
, (char *)buf
, 3, 0);
2989 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2991 TCPCharDriver
*s
= chr
->opaque
;
2995 qemu_set_nonblock(fd
);
2997 socket_set_nodelay(fd
);
2999 s
->chan
= io_channel_from_socket(fd
);
3000 if (s
->listen_tag
) {
3001 g_source_remove(s
->listen_tag
);
3004 tcp_chr_connect(chr
);
3009 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
3011 CharDriverState
*chr
= opaque
;
3012 TCPCharDriver
*s
= chr
->opaque
;
3013 struct sockaddr_in saddr
;
3015 struct sockaddr_un uaddr
;
3017 struct sockaddr
*addr
;
3024 len
= sizeof(uaddr
);
3025 addr
= (struct sockaddr
*)&uaddr
;
3029 len
= sizeof(saddr
);
3030 addr
= (struct sockaddr
*)&saddr
;
3032 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
3033 if (fd
< 0 && errno
!= EINTR
) {
3036 } else if (fd
>= 0) {
3037 if (s
->do_telnetopt
)
3038 tcp_chr_telnet_init(fd
);
3042 if (tcp_chr_add_client(chr
, fd
) < 0)
3048 static void tcp_chr_close(CharDriverState
*chr
)
3050 TCPCharDriver
*s
= chr
->opaque
;
3053 if (s
->reconnect_timer
) {
3054 g_source_remove(s
->reconnect_timer
);
3055 s
->reconnect_timer
= 0;
3057 qapi_free_SocketAddress(s
->addr
);
3059 remove_fd_in_watch(chr
);
3061 g_io_channel_unref(s
->chan
);
3065 if (s
->listen_fd
>= 0) {
3066 if (s
->listen_tag
) {
3067 g_source_remove(s
->listen_tag
);
3070 if (s
->listen_chan
) {
3071 g_io_channel_unref(s
->listen_chan
);
3073 closesocket(s
->listen_fd
);
3075 if (s
->read_msgfds_num
) {
3076 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3077 close(s
->read_msgfds
[i
]);
3079 g_free(s
->read_msgfds
);
3081 if (s
->write_msgfds_num
) {
3082 g_free(s
->write_msgfds
);
3085 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3088 static void qemu_chr_finish_socket_connection(CharDriverState
*chr
, int fd
)
3090 TCPCharDriver
*s
= chr
->opaque
;
3094 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
3095 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
3096 tcp_chr_accept
, chr
);
3100 socket_set_nodelay(fd
);
3101 s
->chan
= io_channel_from_socket(s
->fd
);
3102 tcp_chr_connect(chr
);
3106 static void qemu_chr_socket_connected(int fd
, Error
*err
, void *opaque
)
3108 CharDriverState
*chr
= opaque
;
3109 TCPCharDriver
*s
= chr
->opaque
;
3112 check_report_connect_error(chr
, err
);
3116 s
->connect_err_reported
= false;
3117 qemu_chr_finish_socket_connection(chr
, fd
);
3120 static bool qemu_chr_open_socket_fd(CharDriverState
*chr
, Error
**errp
)
3122 TCPCharDriver
*s
= chr
->opaque
;
3126 fd
= socket_listen(s
->addr
, errp
);
3127 } else if (s
->reconnect_time
) {
3128 fd
= socket_connect(s
->addr
, errp
, qemu_chr_socket_connected
, chr
);
3131 fd
= socket_connect(s
->addr
, errp
, NULL
, NULL
);
3137 qemu_chr_finish_socket_connection(chr
, fd
);
3141 /*********************************************************/
3142 /* Ring buffer chardev */
3149 } RingBufCharDriver
;
3151 static size_t ringbuf_count(const CharDriverState
*chr
)
3153 const RingBufCharDriver
*d
= chr
->opaque
;
3155 return d
->prod
- d
->cons
;
3158 /* Called with chr_write_lock held. */
3159 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3161 RingBufCharDriver
*d
= chr
->opaque
;
3164 if (!buf
|| (len
< 0)) {
3168 for (i
= 0; i
< len
; i
++ ) {
3169 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3170 if (d
->prod
- d
->cons
> d
->size
) {
3171 d
->cons
= d
->prod
- d
->size
;
3178 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3180 RingBufCharDriver
*d
= chr
->opaque
;
3183 qemu_mutex_lock(&chr
->chr_write_lock
);
3184 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3185 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3187 qemu_mutex_unlock(&chr
->chr_write_lock
);
3192 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3194 RingBufCharDriver
*d
= chr
->opaque
;
3201 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3204 CharDriverState
*chr
;
3205 RingBufCharDriver
*d
;
3207 chr
= qemu_chr_alloc();
3208 d
= g_malloc(sizeof(*d
));
3210 d
->size
= opts
->has_size
? opts
->size
: 65536;
3212 /* The size must be power of 2 */
3213 if (d
->size
& (d
->size
- 1)) {
3214 error_setg(errp
, "size of ringbuf chardev must be power of two");
3220 d
->cbuf
= g_malloc0(d
->size
);
3223 chr
->chr_write
= ringbuf_chr_write
;
3224 chr
->chr_close
= ringbuf_chr_close
;
3234 bool chr_is_ringbuf(const CharDriverState
*chr
)
3236 return chr
->chr_write
== ringbuf_chr_write
;
3239 void qmp_ringbuf_write(const char *device
, const char *data
,
3240 bool has_format
, enum DataFormat format
,
3243 CharDriverState
*chr
;
3244 const uint8_t *write_data
;
3248 chr
= qemu_chr_find(device
);
3250 error_setg(errp
, "Device '%s' not found", device
);
3254 if (!chr_is_ringbuf(chr
)) {
3255 error_setg(errp
,"%s is not a ringbuf device", device
);
3259 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3260 write_data
= g_base64_decode(data
, &write_count
);
3262 write_data
= (uint8_t *)data
;
3263 write_count
= strlen(data
);
3266 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3268 if (write_data
!= (uint8_t *)data
) {
3269 g_free((void *)write_data
);
3273 error_setg(errp
, "Failed to write to device %s", device
);
3278 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3279 bool has_format
, enum DataFormat format
,
3282 CharDriverState
*chr
;
3287 chr
= qemu_chr_find(device
);
3289 error_setg(errp
, "Device '%s' not found", device
);
3293 if (!chr_is_ringbuf(chr
)) {
3294 error_setg(errp
,"%s is not a ringbuf device", device
);
3299 error_setg(errp
, "size must be greater than zero");
3303 count
= ringbuf_count(chr
);
3304 size
= size
> count
? count
: size
;
3305 read_data
= g_malloc(size
+ 1);
3307 ringbuf_chr_read(chr
, read_data
, size
);
3309 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3310 data
= g_base64_encode(read_data
, size
);
3314 * FIXME should read only complete, valid UTF-8 characters up
3315 * to @size bytes. Invalid sequences should be replaced by a
3316 * suitable replacement character. Except when (and only
3317 * when) ring buffer lost characters since last read, initial
3318 * continuation characters should be dropped.
3320 read_data
[size
] = 0;
3321 data
= (char *)read_data
;
3327 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3329 char host
[65], port
[33], width
[8], height
[8];
3333 Error
*local_err
= NULL
;
3335 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3337 error_report_err(local_err
);
3341 if (strstart(filename
, "mon:", &p
)) {
3343 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3344 if (strcmp(filename
, "stdio") == 0) {
3345 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3346 * but pass it to the guest. Handle this only for compat syntax,
3347 * for -chardev syntax we have special option for this.
3348 * This is what -nographic did, redirecting+muxing serial+monitor
3349 * to stdio causing Ctrl+C to be passed to guest. */
3350 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3354 if (strcmp(filename
, "null") == 0 ||
3355 strcmp(filename
, "pty") == 0 ||
3356 strcmp(filename
, "msmouse") == 0 ||
3357 strcmp(filename
, "braille") == 0 ||
3358 strcmp(filename
, "testdev") == 0 ||
3359 strcmp(filename
, "stdio") == 0) {
3360 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3363 if (strstart(filename
, "vc", &p
)) {
3364 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3366 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3368 qemu_opt_set(opts
, "width", width
, &error_abort
);
3369 qemu_opt_set(opts
, "height", height
, &error_abort
);
3370 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3372 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3373 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3380 if (strcmp(filename
, "con:") == 0) {
3381 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3384 if (strstart(filename
, "COM", NULL
)) {
3385 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3386 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3389 if (strstart(filename
, "file:", &p
)) {
3390 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3391 qemu_opt_set(opts
, "path", p
, &error_abort
);
3394 if (strstart(filename
, "pipe:", &p
)) {
3395 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3396 qemu_opt_set(opts
, "path", p
, &error_abort
);
3399 if (strstart(filename
, "tcp:", &p
) ||
3400 strstart(filename
, "telnet:", &p
)) {
3401 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3403 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3406 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3407 qemu_opt_set(opts
, "host", host
, &error_abort
);
3408 qemu_opt_set(opts
, "port", port
, &error_abort
);
3409 if (p
[pos
] == ',') {
3410 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3412 error_report_err(local_err
);
3416 if (strstart(filename
, "telnet:", &p
))
3417 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3420 if (strstart(filename
, "udp:", &p
)) {
3421 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3422 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3424 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3428 qemu_opt_set(opts
, "host", host
, &error_abort
);
3429 qemu_opt_set(opts
, "port", port
, &error_abort
);
3430 if (p
[pos
] == '@') {
3432 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3434 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3438 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3439 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3443 if (strstart(filename
, "unix:", &p
)) {
3444 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3445 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3447 error_report_err(local_err
);
3452 if (strstart(filename
, "/dev/parport", NULL
) ||
3453 strstart(filename
, "/dev/ppi", NULL
)) {
3454 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3455 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3458 if (strstart(filename
, "/dev/", NULL
)) {
3459 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3460 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3465 qemu_opts_del(opts
);
3469 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3472 const char *path
= qemu_opt_get(opts
, "path");
3475 error_setg(errp
, "chardev: file: no filename given");
3478 backend
->file
= g_new0(ChardevFile
, 1);
3479 backend
->file
->out
= g_strdup(path
);
3482 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3485 backend
->stdio
= g_new0(ChardevStdio
, 1);
3486 backend
->stdio
->has_signal
= true;
3487 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3490 #ifdef HAVE_CHARDEV_SERIAL
3491 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3494 const char *device
= qemu_opt_get(opts
, "path");
3496 if (device
== NULL
) {
3497 error_setg(errp
, "chardev: serial/tty: no device path given");
3500 backend
->serial
= g_new0(ChardevHostdev
, 1);
3501 backend
->serial
->device
= g_strdup(device
);
3505 #ifdef HAVE_CHARDEV_PARPORT
3506 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3509 const char *device
= qemu_opt_get(opts
, "path");
3511 if (device
== NULL
) {
3512 error_setg(errp
, "chardev: parallel: no device path given");
3515 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3516 backend
->parallel
->device
= g_strdup(device
);
3520 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3523 const char *device
= qemu_opt_get(opts
, "path");
3525 if (device
== NULL
) {
3526 error_setg(errp
, "chardev: pipe: no device path given");
3529 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3530 backend
->pipe
->device
= g_strdup(device
);
3533 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3538 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3540 val
= qemu_opt_get_size(opts
, "size", 0);
3542 backend
->ringbuf
->has_size
= true;
3543 backend
->ringbuf
->size
= val
;
3547 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3550 const char *chardev
= qemu_opt_get(opts
, "chardev");
3552 if (chardev
== NULL
) {
3553 error_setg(errp
, "chardev: mux: no chardev given");
3556 backend
->mux
= g_new0(ChardevMux
, 1);
3557 backend
->mux
->chardev
= g_strdup(chardev
);
3560 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3563 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3564 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3565 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3566 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3567 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3568 const char *path
= qemu_opt_get(opts
, "path");
3569 const char *host
= qemu_opt_get(opts
, "host");
3570 const char *port
= qemu_opt_get(opts
, "port");
3571 SocketAddress
*addr
;
3575 error_setg(errp
, "chardev: socket: no host given");
3579 error_setg(errp
, "chardev: socket: no port given");
3584 backend
->socket
= g_new0(ChardevSocket
, 1);
3586 backend
->socket
->has_nodelay
= true;
3587 backend
->socket
->nodelay
= do_nodelay
;
3588 backend
->socket
->has_server
= true;
3589 backend
->socket
->server
= is_listen
;
3590 backend
->socket
->has_telnet
= true;
3591 backend
->socket
->telnet
= is_telnet
;
3592 backend
->socket
->has_wait
= true;
3593 backend
->socket
->wait
= is_waitconnect
;
3594 backend
->socket
->has_reconnect
= true;
3595 backend
->socket
->reconnect
= reconnect
;
3597 addr
= g_new0(SocketAddress
, 1);
3599 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
3600 addr
->q_unix
= g_new0(UnixSocketAddress
, 1);
3601 addr
->q_unix
->path
= g_strdup(path
);
3603 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3604 addr
->inet
= g_new0(InetSocketAddress
, 1);
3605 addr
->inet
->host
= g_strdup(host
);
3606 addr
->inet
->port
= g_strdup(port
);
3607 addr
->inet
->has_to
= qemu_opt_get(opts
, "to");
3608 addr
->inet
->to
= qemu_opt_get_number(opts
, "to", 0);
3609 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3610 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3611 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3612 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3614 backend
->socket
->addr
= addr
;
3617 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
3620 const char *host
= qemu_opt_get(opts
, "host");
3621 const char *port
= qemu_opt_get(opts
, "port");
3622 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
3623 const char *localport
= qemu_opt_get(opts
, "localport");
3624 bool has_local
= false;
3625 SocketAddress
*addr
;
3627 if (host
== NULL
|| strlen(host
) == 0) {
3630 if (port
== NULL
|| strlen(port
) == 0) {
3631 error_setg(errp
, "chardev: udp: remote port not specified");
3634 if (localport
== NULL
|| strlen(localport
) == 0) {
3639 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
3645 backend
->udp
= g_new0(ChardevUdp
, 1);
3647 addr
= g_new0(SocketAddress
, 1);
3648 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3649 addr
->inet
= g_new0(InetSocketAddress
, 1);
3650 addr
->inet
->host
= g_strdup(host
);
3651 addr
->inet
->port
= g_strdup(port
);
3652 addr
->inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3653 addr
->inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3654 addr
->inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3655 addr
->inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3656 backend
->udp
->remote
= addr
;
3659 backend
->udp
->has_local
= true;
3660 addr
= g_new0(SocketAddress
, 1);
3661 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
3662 addr
->inet
= g_new0(InetSocketAddress
, 1);
3663 addr
->inet
->host
= g_strdup(localaddr
);
3664 addr
->inet
->port
= g_strdup(localport
);
3665 backend
->udp
->local
= addr
;
3669 typedef struct CharDriver
{
3671 ChardevBackendKind kind
;
3672 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3673 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3674 ChardevReturn
*ret
, Error
**errp
);
3677 static GSList
*backends
;
3679 void register_char_driver(const char *name
, ChardevBackendKind kind
,
3680 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
),
3681 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3682 ChardevReturn
*ret
, Error
**errp
))
3686 s
= g_malloc0(sizeof(*s
));
3687 s
->name
= g_strdup(name
);
3692 backends
= g_slist_append(backends
, s
);
3695 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3696 void (*init
)(struct CharDriverState
*s
),
3699 Error
*local_err
= NULL
;
3701 CharDriverState
*chr
;
3703 ChardevReturn
*ret
= NULL
;
3704 ChardevBackend
*backend
;
3705 const char *id
= qemu_opts_id(opts
);
3709 error_setg(errp
, "chardev: no id specified");
3713 if (qemu_opt_get(opts
, "backend") == NULL
) {
3714 error_setg(errp
, "chardev: \"%s\" missing backend",
3715 qemu_opts_id(opts
));
3718 for (i
= backends
; i
; i
= i
->next
) {
3721 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3726 error_setg(errp
, "chardev: backend \"%s\" not found",
3727 qemu_opt_get(opts
, "backend"));
3731 backend
= g_new0(ChardevBackend
, 1);
3733 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3734 bid
= g_strdup_printf("%s-base", id
);
3738 backend
->kind
= cd
->kind
;
3740 cd
->parse(opts
, backend
, &local_err
);
3742 error_propagate(errp
, local_err
);
3746 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3752 qapi_free_ChardevBackend(backend
);
3753 qapi_free_ChardevReturn(ret
);
3754 backend
= g_new0(ChardevBackend
, 1);
3755 backend
->mux
= g_new0(ChardevMux
, 1);
3756 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3757 backend
->mux
->chardev
= g_strdup(bid
);
3758 ret
= qmp_chardev_add(id
, backend
, errp
);
3760 chr
= qemu_chr_find(bid
);
3761 qemu_chr_delete(chr
);
3767 chr
= qemu_chr_find(id
);
3771 qapi_free_ChardevBackend(backend
);
3772 qapi_free_ChardevReturn(ret
);
3777 qemu_opts_del(opts
);
3781 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3784 CharDriverState
*chr
;
3788 if (strstart(filename
, "chardev:", &p
)) {
3789 return qemu_chr_find(p
);
3792 opts
= qemu_chr_parse_compat(label
, filename
);
3796 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3798 error_report_err(err
);
3800 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3801 qemu_chr_fe_claim_no_fail(chr
);
3802 monitor_init(chr
, MONITOR_USE_READLINE
);
3807 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3809 if (chr
->chr_set_echo
) {
3810 chr
->chr_set_echo(chr
, echo
);
3814 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3816 if (chr
->fe_open
== fe_open
) {
3819 chr
->fe_open
= fe_open
;
3820 if (chr
->chr_set_fe_open
) {
3821 chr
->chr_set_fe_open(chr
, fe_open
);
3825 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3827 if (chr
->chr_fe_event
) {
3828 chr
->chr_fe_event(chr
, event
);
3832 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3833 GIOFunc func
, void *user_data
)
3838 if (s
->chr_add_watch
== NULL
) {
3842 src
= s
->chr_add_watch(s
, cond
);
3847 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3848 tag
= g_source_attach(src
, NULL
);
3849 g_source_unref(src
);
3854 int qemu_chr_fe_claim(CharDriverState
*s
)
3856 if (s
->avail_connections
< 1) {
3859 s
->avail_connections
--;
3863 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3865 if (qemu_chr_fe_claim(s
) != 0) {
3866 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3867 __func__
, s
->label
);
3872 void qemu_chr_fe_release(CharDriverState
*s
)
3874 s
->avail_connections
++;
3877 void qemu_chr_delete(CharDriverState
*chr
)
3879 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3880 if (chr
->chr_close
) {
3881 chr
->chr_close(chr
);
3883 g_free(chr
->filename
);
3885 qemu_opts_del(chr
->opts
);
3889 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3891 ChardevInfoList
*chr_list
= NULL
;
3892 CharDriverState
*chr
;
3894 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3895 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3896 info
->value
= g_malloc0(sizeof(*info
->value
));
3897 info
->value
->label
= g_strdup(chr
->label
);
3898 info
->value
->filename
= g_strdup(chr
->filename
);
3899 info
->value
->frontend_open
= chr
->fe_open
;
3901 info
->next
= chr_list
;
3908 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3910 ChardevBackendInfoList
*backend_list
= NULL
;
3911 CharDriver
*c
= NULL
;
3914 for (i
= backends
; i
; i
= i
->next
) {
3915 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3917 info
->value
= g_malloc0(sizeof(*info
->value
));
3918 info
->value
->name
= g_strdup(c
->name
);
3920 info
->next
= backend_list
;
3921 backend_list
= info
;
3924 return backend_list
;
3927 CharDriverState
*qemu_chr_find(const char *name
)
3929 CharDriverState
*chr
;
3931 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3932 if (strcmp(chr
->label
, name
) != 0)
3939 /* Get a character (serial) device interface. */
3940 CharDriverState
*qemu_char_get_next_serial(void)
3942 static int next_serial
;
3943 CharDriverState
*chr
;
3945 /* FIXME: This function needs to go away: use chardev properties! */
3947 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3948 chr
= serial_hds
[next_serial
++];
3949 qemu_chr_fe_claim_no_fail(chr
);
3955 QemuOptsList qemu_chardev_opts
= {
3957 .implied_opt_name
= "backend",
3958 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3962 .type
= QEMU_OPT_STRING
,
3965 .type
= QEMU_OPT_STRING
,
3968 .type
= QEMU_OPT_STRING
,
3971 .type
= QEMU_OPT_STRING
,
3973 .name
= "localaddr",
3974 .type
= QEMU_OPT_STRING
,
3976 .name
= "localport",
3977 .type
= QEMU_OPT_STRING
,
3980 .type
= QEMU_OPT_NUMBER
,
3983 .type
= QEMU_OPT_BOOL
,
3986 .type
= QEMU_OPT_BOOL
,
3989 .type
= QEMU_OPT_BOOL
,
3992 .type
= QEMU_OPT_BOOL
,
3995 .type
= QEMU_OPT_BOOL
,
3997 .name
= "reconnect",
3998 .type
= QEMU_OPT_NUMBER
,
4001 .type
= QEMU_OPT_BOOL
,
4004 .type
= QEMU_OPT_NUMBER
,
4007 .type
= QEMU_OPT_NUMBER
,
4010 .type
= QEMU_OPT_NUMBER
,
4013 .type
= QEMU_OPT_NUMBER
,
4016 .type
= QEMU_OPT_BOOL
,
4019 .type
= QEMU_OPT_BOOL
,
4022 .type
= QEMU_OPT_STRING
,
4025 .type
= QEMU_OPT_NUMBER
,
4028 .type
= QEMU_OPT_SIZE
,
4031 .type
= QEMU_OPT_STRING
,
4033 { /* end of list */ }
4039 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4040 ChardevBackend
*backend
,
4044 ChardevFile
*file
= backend
->file
;
4048 error_setg(errp
, "input file not supported");
4052 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
4053 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4054 if (out
== INVALID_HANDLE_VALUE
) {
4055 error_setg(errp
, "open %s failed", file
->out
);
4058 return qemu_chr_open_win_file(out
);
4061 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4062 ChardevBackend
*backend
,
4066 ChardevHostdev
*serial
= backend
->serial
;
4067 return qemu_chr_open_win_path(serial
->device
, errp
);
4072 static int qmp_chardev_open_file_source(char *src
, int flags
,
4077 TFR(fd
= qemu_open(src
, flags
, 0666));
4079 error_setg_file_open(errp
, errno
, src
);
4084 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4085 ChardevBackend
*backend
,
4089 ChardevFile
*file
= backend
->file
;
4090 int flags
, in
= -1, out
;
4092 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
4093 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4100 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4107 return qemu_chr_open_fd(in
, out
);
4110 #ifdef HAVE_CHARDEV_SERIAL
4111 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4112 ChardevBackend
*backend
,
4116 ChardevHostdev
*serial
= backend
->serial
;
4119 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4123 qemu_set_nonblock(fd
);
4124 return qemu_chr_open_tty_fd(fd
);
4128 #ifdef HAVE_CHARDEV_PARPORT
4129 static CharDriverState
*qmp_chardev_open_parallel(const char *id
,
4130 ChardevBackend
*backend
,
4134 ChardevHostdev
*parallel
= backend
->parallel
;
4137 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4141 return qemu_chr_open_pp_fd(fd
, errp
);
4147 static void socket_try_connect(CharDriverState
*chr
)
4151 if (!qemu_chr_open_socket_fd(chr
, &err
)) {
4152 check_report_connect_error(chr
, err
);
4156 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4158 CharDriverState
*chr
= opaque
;
4159 TCPCharDriver
*s
= chr
->opaque
;
4161 s
->reconnect_timer
= 0;
4167 socket_try_connect(chr
);
4172 static CharDriverState
*qmp_chardev_open_socket(const char *id
,
4173 ChardevBackend
*backend
,
4177 CharDriverState
*chr
;
4179 ChardevSocket
*sock
= backend
->socket
;
4180 SocketAddress
*addr
= sock
->addr
;
4181 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4182 bool is_listen
= sock
->has_server
? sock
->server
: true;
4183 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4184 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4185 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4187 chr
= qemu_chr_alloc();
4188 s
= g_new0(TCPCharDriver
, 1);
4192 s
->is_unix
= addr
->kind
== SOCKET_ADDRESS_KIND_UNIX
;
4193 s
->is_listen
= is_listen
;
4194 s
->is_telnet
= is_telnet
;
4195 s
->do_nodelay
= do_nodelay
;
4196 qapi_copy_SocketAddress(&s
->addr
, sock
->addr
);
4199 chr
->chr_write
= tcp_chr_write
;
4200 chr
->chr_sync_read
= tcp_chr_sync_read
;
4201 chr
->chr_close
= tcp_chr_close
;
4202 chr
->get_msgfds
= tcp_get_msgfds
;
4203 chr
->set_msgfds
= tcp_set_msgfds
;
4204 chr
->chr_add_client
= tcp_chr_add_client
;
4205 chr
->chr_add_watch
= tcp_chr_add_watch
;
4206 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4207 /* be isn't opened until we get a connection */
4208 chr
->explicit_be_open
= true;
4210 chr
->filename
= g_malloc(CHR_MAX_FILENAME_SIZE
);
4211 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
, "disconnected:",
4212 addr
, is_listen
, is_telnet
);
4216 s
->do_telnetopt
= 1;
4218 } else if (reconnect
> 0) {
4219 s
->reconnect_time
= reconnect
;
4222 if (s
->reconnect_time
) {
4223 socket_try_connect(chr
);
4224 } else if (!qemu_chr_open_socket_fd(chr
, errp
)) {
4226 g_free(chr
->filename
);
4231 if (is_listen
&& is_waitconnect
) {
4232 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
4234 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
4235 qemu_set_nonblock(s
->listen_fd
);
4241 static CharDriverState
*qmp_chardev_open_udp(const char *id
,
4242 ChardevBackend
*backend
,
4246 ChardevUdp
*udp
= backend
->udp
;
4249 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
4253 return qemu_chr_open_udp_fd(fd
);
4256 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4259 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4260 CharDriverState
*chr
= NULL
;
4261 Error
*local_err
= NULL
;
4265 chr
= qemu_chr_find(id
);
4267 error_setg(errp
, "Chardev '%s' already exists", id
);
4272 for (i
= backends
; i
; i
= i
->next
) {
4275 if (cd
->kind
== backend
->kind
&& cd
->create
) {
4276 chr
= cd
->create(id
, backend
, ret
, &local_err
);
4278 error_propagate(errp
, local_err
);
4286 switch (backend
->kind
) {
4287 case CHARDEV_BACKEND_KIND_FILE
:
4290 case CHARDEV_BACKEND_KIND_SERIAL
:
4293 case CHARDEV_BACKEND_KIND_PARALLEL
:
4296 case CHARDEV_BACKEND_KIND_PIPE
:
4299 case CHARDEV_BACKEND_KIND_SOCKET
:
4302 case CHARDEV_BACKEND_KIND_UDP
:
4305 case CHARDEV_BACKEND_KIND_PTY
:
4308 case CHARDEV_BACKEND_KIND_NULL
:
4311 case CHARDEV_BACKEND_KIND_MUX
:
4314 case CHARDEV_BACKEND_KIND_MSMOUSE
:
4315 chr
= qemu_chr_open_msmouse();
4317 #ifdef CONFIG_BRLAPI
4318 case CHARDEV_BACKEND_KIND_BRAILLE
:
4319 chr
= chr_baum_init();
4322 case CHARDEV_BACKEND_KIND_TESTDEV
:
4323 chr
= chr_testdev_init();
4325 case CHARDEV_BACKEND_KIND_STDIO
:
4326 chr
= qemu_chr_open_stdio(backend
->stdio
);
4329 case CHARDEV_BACKEND_KIND_CONSOLE
:
4330 chr
= qemu_chr_open_win_con();
4334 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4335 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
4337 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4338 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
4341 case CHARDEV_BACKEND_KIND_VC
:
4342 chr
= vc_init(backend
->vc
);
4344 case CHARDEV_BACKEND_KIND_RINGBUF
:
4345 case CHARDEV_BACKEND_KIND_MEMORY
:
4346 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, &local_err
);
4349 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4354 * Character backend open hasn't been fully converted to the Error
4355 * API. Some opens fail without setting an error. Set a generic
4357 * TODO full conversion to Error API
4361 error_propagate(errp
, local_err
);
4363 error_setg(errp
, "Failed to create chardev");
4369 chr
->label
= g_strdup(id
);
4370 chr
->avail_connections
=
4371 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4372 if (!chr
->filename
) {
4373 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4375 if (!chr
->explicit_be_open
) {
4376 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4378 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4386 void qmp_chardev_remove(const char *id
, Error
**errp
)
4388 CharDriverState
*chr
;
4390 chr
= qemu_chr_find(id
);
4392 error_setg(errp
, "Chardev '%s' not found", id
);
4395 if (chr
->chr_can_read
|| chr
->chr_read
||
4396 chr
->chr_event
|| chr
->handler_opaque
) {
4397 error_setg(errp
, "Chardev '%s' is busy", id
);
4400 qemu_chr_delete(chr
);
4403 static void register_types(void)
4405 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
,
4406 qemu_chr_open_null
);
4407 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4408 qemu_chr_parse_socket
, qmp_chardev_open_socket
);
4409 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
,
4410 qmp_chardev_open_udp
);
4411 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4412 qemu_chr_parse_ringbuf
, NULL
);
4413 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4414 qemu_chr_parse_file_out
, qmp_chardev_open_file
);
4415 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4416 qemu_chr_parse_stdio
, NULL
);
4417 #if defined HAVE_CHARDEV_SERIAL
4418 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4419 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4420 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4421 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4423 #ifdef HAVE_CHARDEV_PARPORT
4424 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4425 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4426 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4427 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4429 #ifdef HAVE_CHARDEV_PTY
4430 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
,
4433 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
,
4435 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4436 qemu_chr_parse_pipe
, qemu_chr_open_pipe
);
4437 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
,
4439 /* Bug-compatibility: */
4440 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4441 qemu_chr_parse_ringbuf
, NULL
);
4442 /* this must be done after machine init, since we register FEs with muxes
4443 * as part of realize functions like serial_isa_realizefn when -nographic
4446 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4449 type_init(register_types
);