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/osdep.h"
25 #include "qemu-common.h"
26 #include "qemu/cutils.h"
27 #include "monitor/monitor.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/block-backend.h"
30 #include "qemu/error-report.h"
31 #include "qemu/timer.h"
32 #include "sysemu/char.h"
34 #include "qmp-commands.h"
35 #include "qapi/clone-visitor.h"
36 #include "qapi-visit.h"
37 #include "qemu/base64.h"
38 #include "io/channel-socket.h"
39 #include "io/channel-file.h"
40 #include "io/channel-tls.h"
41 #include "sysemu/replay.h"
42 #include "qemu/help_option.h"
47 #include <sys/times.h>
50 #include <sys/ioctl.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <arpa/inet.h>
57 #include <sys/select.h>
59 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
60 #include <dev/ppbus/ppi.h>
61 #include <dev/ppbus/ppbconf.h>
62 #elif defined(__DragonFly__)
63 #include <dev/misc/ppi/ppi.h>
64 #include <bus/ppbus/ppbconf.h>
68 #include <linux/ppdev.h>
69 #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 TCP_MAX_FDS 16
92 typedef struct MuxDriver MuxDriver
;
94 /***********************************************************/
95 /* Socket address helpers */
97 static char *SocketAddress_to_str(const char *prefix
, SocketAddress
*addr
,
98 bool is_listen
, bool is_telnet
)
100 switch (addr
->type
) {
101 case SOCKET_ADDRESS_KIND_INET
:
102 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
103 is_telnet
? "telnet" : "tcp",
104 addr
->u
.inet
.data
->host
,
105 addr
->u
.inet
.data
->port
,
106 is_listen
? ",server" : "");
108 case SOCKET_ADDRESS_KIND_UNIX
:
109 return g_strdup_printf("%sunix:%s%s", prefix
,
110 addr
->u
.q_unix
.data
->path
,
111 is_listen
? ",server" : "");
113 case SOCKET_ADDRESS_KIND_FD
:
114 return g_strdup_printf("%sfd:%s%s", prefix
, addr
->u
.fd
.data
->str
,
115 is_listen
? ",server" : "");
122 static char *sockaddr_to_str(struct sockaddr_storage
*ss
, socklen_t ss_len
,
123 struct sockaddr_storage
*ps
, socklen_t ps_len
,
124 bool is_listen
, bool is_telnet
)
126 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
127 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
128 const char *left
= "", *right
= "";
130 switch (ss
->ss_family
) {
133 return g_strdup_printf("unix:%s%s",
134 ((struct sockaddr_un
*)(ss
))->sun_path
,
135 is_listen
? ",server" : "");
142 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
143 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
144 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
145 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
146 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
147 is_telnet
? "telnet" : "tcp",
148 left
, shost
, right
, sserv
,
149 is_listen
? ",server" : "",
150 left
, phost
, right
, pserv
);
153 return g_strdup_printf("unknown");
157 /***********************************************************/
158 /* character device */
160 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
161 QTAILQ_HEAD_INITIALIZER(chardevs
);
163 static void qemu_chr_free_common(CharDriverState
*chr
);
165 CharDriverState
*qemu_chr_alloc(ChardevCommon
*backend
, Error
**errp
)
167 CharDriverState
*chr
= g_malloc0(sizeof(CharDriverState
));
168 qemu_mutex_init(&chr
->chr_write_lock
);
170 if (backend
->has_logfile
) {
171 int flags
= O_WRONLY
| O_CREAT
;
172 if (backend
->has_logappend
&&
173 backend
->logappend
) {
178 chr
->logfd
= qemu_open(backend
->logfile
, flags
, 0666);
179 if (chr
->logfd
< 0) {
180 error_setg_errno(errp
, errno
,
181 "Unable to open logfile %s",
193 void qemu_chr_be_event(CharDriverState
*s
, int event
)
195 CharBackend
*be
= s
->be
;
197 /* Keep track if the char device is open */
199 case CHR_EVENT_OPENED
:
202 case CHR_EVENT_CLOSED
:
207 if (!be
|| !be
->chr_event
) {
211 be
->chr_event(be
->opaque
, event
);
214 void qemu_chr_be_generic_open(CharDriverState
*s
)
216 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
220 /* Not reporting errors from writing to logfile, as logs are
221 * defined to be "best effort" only */
222 static void qemu_chr_fe_write_log(CharDriverState
*s
,
223 const uint8_t *buf
, size_t len
)
234 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
235 if (ret
== -1 && errno
== EAGAIN
) {
247 static int qemu_chr_fe_write_buffer(CharDriverState
*s
, const uint8_t *buf
, int len
, int *offset
)
252 qemu_mutex_lock(&s
->chr_write_lock
);
253 while (*offset
< len
) {
255 res
= s
->chr_write(s
, buf
+ *offset
, len
- *offset
);
256 if (res
< 0 && errno
== EAGAIN
) {
268 qemu_chr_fe_write_log(s
, buf
, *offset
);
270 qemu_mutex_unlock(&s
->chr_write_lock
);
275 int qemu_chr_fe_write(CharBackend
*be
, const uint8_t *buf
, int len
)
277 CharDriverState
*s
= be
->chr
;
284 if (s
->replay
&& replay_mode
== REPLAY_MODE_PLAY
) {
286 replay_char_write_event_load(&ret
, &offset
);
287 assert(offset
<= len
);
288 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
292 qemu_mutex_lock(&s
->chr_write_lock
);
293 ret
= s
->chr_write(s
, buf
, len
);
296 qemu_chr_fe_write_log(s
, buf
, ret
);
299 qemu_mutex_unlock(&s
->chr_write_lock
);
301 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
302 replay_char_write_event_save(ret
, ret
< 0 ? 0 : ret
);
308 static int qemu_chr_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
313 if (s
->replay
&& replay_mode
== REPLAY_MODE_PLAY
) {
314 replay_char_write_event_load(&res
, &offset
);
315 assert(offset
<= len
);
316 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
320 res
= qemu_chr_fe_write_buffer(s
, buf
, len
, &offset
);
322 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
323 replay_char_write_event_save(res
, offset
);
332 int qemu_chr_fe_write_all(CharBackend
*be
, const uint8_t *buf
, int len
)
334 CharDriverState
*s
= be
->chr
;
340 return qemu_chr_write_all(s
, buf
, len
);
343 int qemu_chr_fe_read_all(CharBackend
*be
, uint8_t *buf
, int len
)
345 CharDriverState
*s
= be
->chr
;
346 int offset
= 0, counter
= 10;
349 if (!s
|| !s
->chr_sync_read
) {
353 if (s
->replay
&& replay_mode
== REPLAY_MODE_PLAY
) {
354 return replay_char_read_all_load(buf
);
357 while (offset
< len
) {
359 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
360 if (res
== -1 && errno
== EAGAIN
) {
370 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
371 replay_char_read_all_save_error(res
);
383 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
384 replay_char_read_all_save_buf(buf
, offset
);
389 int qemu_chr_fe_ioctl(CharBackend
*be
, int cmd
, void *arg
)
391 CharDriverState
*s
= be
->chr
;
394 if (!s
|| !s
->chr_ioctl
|| s
->replay
) {
397 res
= s
->chr_ioctl(s
, cmd
, arg
);
403 int qemu_chr_be_can_write(CharDriverState
*s
)
405 CharBackend
*be
= s
->be
;
407 if (!be
|| !be
->chr_can_read
) {
411 return be
->chr_can_read(be
->opaque
);
414 void qemu_chr_be_write_impl(CharDriverState
*s
, uint8_t *buf
, int len
)
416 CharBackend
*be
= s
->be
;
418 if (be
&& be
->chr_read
) {
419 be
->chr_read(be
->opaque
, buf
, len
);
423 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
426 if (replay_mode
== REPLAY_MODE_PLAY
) {
429 replay_chr_be_write(s
, buf
, len
);
431 qemu_chr_be_write_impl(s
, buf
, len
);
435 int qemu_chr_fe_get_msgfd(CharBackend
*be
)
437 CharDriverState
*s
= be
->chr
;
439 int res
= (qemu_chr_fe_get_msgfds(be
, &fd
, 1) == 1) ? fd
: -1;
440 if (s
&& s
->replay
) {
442 "Replay: get msgfd is not supported for serial devices yet\n");
448 int qemu_chr_fe_get_msgfds(CharBackend
*be
, int *fds
, int len
)
450 CharDriverState
*s
= be
->chr
;
456 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
459 int qemu_chr_fe_set_msgfds(CharBackend
*be
, int *fds
, int num
)
461 CharDriverState
*s
= be
->chr
;
467 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
470 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
472 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
475 void qemu_chr_fe_accept_input(CharBackend
*be
)
477 CharDriverState
*s
= be
->chr
;
483 if (s
->chr_accept_input
)
484 s
->chr_accept_input(s
);
488 void qemu_chr_fe_printf(CharBackend
*be
, const char *fmt
, ...)
490 char buf
[READ_BUF_LEN
];
493 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
494 /* XXX this blocks entire thread. Rewrite to use
495 * qemu_chr_fe_write and background I/O callbacks */
496 qemu_chr_fe_write_all(be
, (uint8_t *)buf
, strlen(buf
));
500 static void remove_fd_in_watch(CharDriverState
*chr
);
501 static void mux_chr_set_handlers(CharDriverState
*chr
, GMainContext
*context
);
502 static void mux_set_focus(MuxDriver
*d
, int focus
);
504 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
509 static CharDriverState
*qemu_chr_open_null(const char *id
,
510 ChardevBackend
*backend
,
515 CharDriverState
*chr
;
516 ChardevCommon
*common
= backend
->u
.null
.data
;
518 chr
= qemu_chr_alloc(common
, errp
);
522 chr
->chr_write
= null_chr_write
;
527 /* MUX driver for serial I/O splitting */
529 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
530 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
532 CharBackend
*backends
[MAX_MUX
];
538 /* Intermediate input buffer allows to catch escape sequences even if the
539 currently active device is not accepting any input - but only until it
541 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
546 /* Protected by the CharDriverState chr_write_lock. */
548 int64_t timestamps_start
;
551 /* Called with chr_write_lock held. */
552 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
554 MuxDriver
*d
= chr
->opaque
;
556 if (!d
->timestamps
) {
557 ret
= qemu_chr_fe_write(&d
->chr
, buf
, len
);
562 for (i
= 0; i
< len
; i
++) {
568 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
569 if (d
->timestamps_start
== -1)
570 d
->timestamps_start
= ti
;
571 ti
-= d
->timestamps_start
;
573 snprintf(buf1
, sizeof(buf1
),
574 "[%02d:%02d:%02d.%03d] ",
579 /* XXX this blocks entire thread. Rewrite to use
580 * qemu_chr_fe_write and background I/O callbacks */
581 qemu_chr_fe_write_all(&d
->chr
,
582 (uint8_t *)buf1
, strlen(buf1
));
585 ret
+= qemu_chr_fe_write(&d
->chr
, buf
+ i
, 1);
586 if (buf
[i
] == '\n') {
594 static const char * const mux_help
[] = {
595 "% h print this help\n\r",
596 "% x exit emulator\n\r",
597 "% s save disk data back to file (if -snapshot)\n\r",
598 "% t toggle console timestamps\n\r",
599 "% b send break (magic sysrq)\n\r",
600 "% c switch between console and monitor\n\r",
605 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
606 static void mux_print_help(CharDriverState
*chr
)
609 char ebuf
[15] = "Escape-Char";
610 char cbuf
[50] = "\n\r";
612 if (term_escape_char
> 0 && term_escape_char
< 26) {
613 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
614 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
616 snprintf(cbuf
, sizeof(cbuf
),
617 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
620 /* XXX this blocks entire thread. Rewrite to use
621 * qemu_chr_fe_write and background I/O callbacks */
622 qemu_chr_write_all(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
623 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
624 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
625 if (mux_help
[i
][j
] == '%')
626 qemu_chr_write_all(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
628 qemu_chr_write_all(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
633 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
635 CharBackend
*be
= d
->backends
[mux_nr
];
637 if (be
&& be
->chr_event
) {
638 be
->chr_event(be
->opaque
, event
);
642 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
644 if (d
->term_got_escape
) {
645 d
->term_got_escape
= 0;
646 if (ch
== term_escape_char
)
655 const char *term
= "QEMU: Terminated\n\r";
656 qemu_chr_write_all(chr
, (uint8_t *)term
, strlen(term
));
664 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
667 assert(d
->mux_cnt
> 0); /* handler registered with first fe */
668 /* Switch to the next registered device */
669 mux_set_focus(d
, (d
->focus
+ 1) % d
->mux_cnt
);
672 d
->timestamps
= !d
->timestamps
;
673 d
->timestamps_start
= -1;
677 } else if (ch
== term_escape_char
) {
678 d
->term_got_escape
= 1;
686 static void mux_chr_accept_input(CharDriverState
*chr
)
688 MuxDriver
*d
= chr
->opaque
;
690 CharBackend
*be
= d
->backends
[m
];
692 while (be
&& d
->prod
[m
] != d
->cons
[m
] &&
693 be
->chr_can_read
&& be
->chr_can_read(be
->opaque
)) {
694 be
->chr_read(be
->opaque
,
695 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
699 static int mux_chr_can_read(void *opaque
)
701 CharDriverState
*chr
= opaque
;
702 MuxDriver
*d
= chr
->opaque
;
704 CharBackend
*be
= d
->backends
[m
];
706 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
) {
710 if (be
&& be
->chr_can_read
) {
711 return be
->chr_can_read(be
->opaque
);
717 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
719 CharDriverState
*chr
= opaque
;
720 MuxDriver
*d
= chr
->opaque
;
722 CharBackend
*be
= d
->backends
[m
];
725 mux_chr_accept_input(opaque
);
727 for (i
= 0; i
< size
; i
++)
728 if (mux_proc_byte(chr
, d
, buf
[i
])) {
729 if (d
->prod
[m
] == d
->cons
[m
] &&
730 be
&& be
->chr_can_read
&&
731 be
->chr_can_read(be
->opaque
))
732 be
->chr_read(be
->opaque
, &buf
[i
], 1);
734 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
738 static bool muxes_realized
;
740 static void mux_chr_event(void *opaque
, int event
)
742 CharDriverState
*chr
= opaque
;
743 MuxDriver
*d
= chr
->opaque
;
746 if (!muxes_realized
) {
750 /* Send the event to all registered listeners */
751 for (i
= 0; i
< d
->mux_cnt
; i
++)
752 mux_chr_send_event(d
, i
, event
);
756 * Called after processing of default and command-line-specified
757 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
758 * to a mux chardev. This is done here to ensure that
759 * output/prompts/banners are only displayed for the FE that has
760 * focus when initial command-line processing/machine init is
763 * After this point, any new FE attached to any new or existing
764 * mux will receive CHR_EVENT_OPENED notifications for the BE
767 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
769 CharDriverState
*chr
;
771 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
773 MuxDriver
*d
= chr
->opaque
;
776 /* send OPENED to all already-attached FEs */
777 for (i
= 0; i
< d
->mux_cnt
; i
++) {
778 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
780 /* mark mux as OPENED so any new FEs will immediately receive
783 qemu_chr_be_generic_open(chr
);
786 muxes_realized
= true;
789 static Notifier muxes_realize_notify
= {
790 .notify
= muxes_realize_done
,
793 static GSource
*mux_chr_add_watch(CharDriverState
*s
, GIOCondition cond
)
795 MuxDriver
*d
= s
->opaque
;
796 CharDriverState
*chr
= qemu_chr_fe_get_driver(&d
->chr
);
798 return chr
->chr_add_watch(chr
, cond
);
801 static void mux_chr_free(struct CharDriverState
*chr
)
803 MuxDriver
*d
= chr
->opaque
;
806 for (i
= 0; i
< d
->mux_cnt
; i
++) {
807 CharBackend
*be
= d
->backends
[i
];
812 qemu_chr_fe_deinit(&d
->chr
);
816 static void mux_chr_set_handlers(CharDriverState
*chr
, GMainContext
*context
)
818 MuxDriver
*d
= chr
->opaque
;
820 /* Fix up the real driver with mux routines */
821 qemu_chr_fe_set_handlers(&d
->chr
,
829 static void mux_set_focus(MuxDriver
*d
, int focus
)
832 assert(focus
< d
->mux_cnt
);
834 if (d
->focus
!= -1) {
835 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
839 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
842 static CharDriverState
*qemu_chr_open_mux(const char *id
,
843 ChardevBackend
*backend
,
848 ChardevMux
*mux
= backend
->u
.mux
.data
;
849 CharDriverState
*chr
, *drv
;
851 ChardevCommon
*common
= qapi_ChardevMux_base(mux
);
853 drv
= qemu_chr_find(mux
->chardev
);
855 error_setg(errp
, "mux: base chardev %s not found", mux
->chardev
);
859 chr
= qemu_chr_alloc(common
, errp
);
863 d
= g_new0(MuxDriver
, 1);
867 chr
->chr_free
= mux_chr_free
;
868 chr
->chr_write
= mux_chr_write
;
869 chr
->chr_accept_input
= mux_chr_accept_input
;
870 /* Frontend guest-open / -close notification is not support with muxes */
871 chr
->chr_set_fe_open
= NULL
;
872 if (drv
->chr_add_watch
) {
873 chr
->chr_add_watch
= mux_chr_add_watch
;
875 /* only default to opened state if we've realized the initial
878 *be_opened
= muxes_realized
;
880 if (!qemu_chr_fe_init(&d
->chr
, drv
, errp
)) {
888 CharDriverState
*qemu_chr_fe_get_driver(CharBackend
*be
)
893 bool qemu_chr_fe_init(CharBackend
*b
, CharDriverState
*s
, Error
**errp
)
898 MuxDriver
*d
= s
->opaque
;
900 if (d
->mux_cnt
>= MAX_MUX
) {
904 d
->backends
[d
->mux_cnt
] = b
;
918 error_setg(errp
, QERR_DEVICE_IN_USE
, s
->label
);
922 static bool qemu_chr_is_busy(CharDriverState
*s
)
925 MuxDriver
*d
= s
->opaque
;
926 return d
->mux_cnt
>= 0;
928 return s
->be
!= NULL
;
932 void qemu_chr_fe_deinit(CharBackend
*b
)
937 qemu_chr_fe_set_handlers(b
, NULL
, NULL
, NULL
, NULL
, NULL
, true);
939 if (b
->chr
->is_mux
) {
940 MuxDriver
*d
= b
->chr
->opaque
;
941 d
->backends
[b
->tag
] = NULL
;
947 void qemu_chr_fe_set_handlers(CharBackend
*b
,
948 IOCanReadHandler
*fd_can_read
,
949 IOReadHandler
*fd_read
,
950 IOEventHandler
*fd_event
,
952 GMainContext
*context
,
963 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
965 remove_fd_in_watch(s
);
969 b
->chr_can_read
= fd_can_read
;
970 b
->chr_read
= fd_read
;
971 b
->chr_event
= fd_event
;
973 if (s
->chr_update_read_handler
) {
974 s
->chr_update_read_handler(s
, context
);
978 qemu_chr_fe_set_open(b
, fe_open
);
982 qemu_chr_fe_take_focus(b
);
983 /* We're connecting to an already opened device, so let's make sure we
984 also get the open event */
986 qemu_chr_be_generic_open(s
);
991 mux_chr_set_handlers(s
, context
);
995 void qemu_chr_fe_take_focus(CharBackend
*b
)
1001 if (b
->chr
->is_mux
) {
1002 mux_set_focus(b
->chr
->opaque
, b
->tag
);
1006 typedef struct IOWatchPoll
1013 IOCanReadHandler
*fd_can_read
;
1014 GSourceFunc fd_read
;
1016 GMainContext
*context
;
1019 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
1021 return container_of(source
, IOWatchPoll
, parent
);
1024 static gboolean
io_watch_poll_prepare(GSource
*source
,
1027 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
1028 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
1029 bool was_active
= iwp
->src
!= NULL
;
1030 if (was_active
== now_active
) {
1035 iwp
->src
= qio_channel_create_watch(
1036 iwp
->ioc
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
);
1037 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
1038 g_source_attach(iwp
->src
, iwp
->context
);
1040 g_source_destroy(iwp
->src
);
1041 g_source_unref(iwp
->src
);
1047 static gboolean
io_watch_poll_check(GSource
*source
)
1052 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
1058 static void io_watch_poll_finalize(GSource
*source
)
1060 /* Due to a glib bug, removing the last reference to a source
1061 * inside a finalize callback causes recursive locking (and a
1062 * deadlock). This is not a problem inside other callbacks,
1063 * including dispatch callbacks, so we call io_remove_watch_poll
1064 * to remove this source. At this point, iwp->src must
1065 * be NULL, or we would leak it.
1067 * This would be solved much more elegantly by child sources,
1068 * but we support older glib versions that do not have them.
1070 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
1071 assert(iwp
->src
== NULL
);
1074 static GSourceFuncs io_watch_poll_funcs
= {
1075 .prepare
= io_watch_poll_prepare
,
1076 .check
= io_watch_poll_check
,
1077 .dispatch
= io_watch_poll_dispatch
,
1078 .finalize
= io_watch_poll_finalize
,
1081 /* Can only be used for read */
1082 static guint
io_add_watch_poll(CharDriverState
*chr
,
1084 IOCanReadHandler
*fd_can_read
,
1085 QIOChannelFunc fd_read
,
1087 GMainContext
*context
)
1093 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
,
1094 sizeof(IOWatchPoll
));
1095 iwp
->fd_can_read
= fd_can_read
;
1096 iwp
->opaque
= user_data
;
1098 iwp
->fd_read
= (GSourceFunc
) fd_read
;
1100 iwp
->context
= context
;
1102 name
= g_strdup_printf("chardev-iowatch-%s", chr
->label
);
1103 g_source_set_name((GSource
*)iwp
, name
);
1106 tag
= g_source_attach(&iwp
->parent
, context
);
1107 g_source_unref(&iwp
->parent
);
1111 static void io_remove_watch_poll(guint tag
)
1116 g_return_if_fail (tag
> 0);
1118 source
= g_main_context_find_source_by_id(NULL
, tag
);
1119 g_return_if_fail (source
!= NULL
);
1121 iwp
= io_watch_poll_from_source(source
);
1123 g_source_destroy(iwp
->src
);
1124 g_source_unref(iwp
->src
);
1127 g_source_destroy(&iwp
->parent
);
1130 static void remove_fd_in_watch(CharDriverState
*chr
)
1132 if (chr
->fd_in_tag
) {
1133 io_remove_watch_poll(chr
->fd_in_tag
);
1139 static int io_channel_send_full(QIOChannel
*ioc
,
1140 const void *buf
, size_t len
,
1141 int *fds
, size_t nfds
)
1145 while (offset
< len
) {
1147 struct iovec iov
= { .iov_base
= (char *)buf
+ offset
,
1148 .iov_len
= len
- offset
};
1150 ret
= qio_channel_writev_full(
1153 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
1160 } else if (ret
< 0) {
1173 static int io_channel_send(QIOChannel
*ioc
, const void *buf
, size_t len
)
1175 return io_channel_send_full(ioc
, buf
, len
, NULL
, 0);
1179 typedef struct FDCharDriver
{
1180 CharDriverState
*chr
;
1181 QIOChannel
*ioc_in
, *ioc_out
;
1185 /* Called with chr_write_lock held. */
1186 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1188 FDCharDriver
*s
= chr
->opaque
;
1190 return io_channel_send(s
->ioc_out
, buf
, len
);
1193 static gboolean
fd_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1195 CharDriverState
*chr
= opaque
;
1196 FDCharDriver
*s
= chr
->opaque
;
1198 uint8_t buf
[READ_BUF_LEN
];
1202 if (len
> s
->max_size
) {
1209 ret
= qio_channel_read(
1210 chan
, (gchar
*)buf
, len
, NULL
);
1212 remove_fd_in_watch(chr
);
1213 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1217 qemu_chr_be_write(chr
, buf
, ret
);
1223 static int fd_chr_read_poll(void *opaque
)
1225 CharDriverState
*chr
= opaque
;
1226 FDCharDriver
*s
= chr
->opaque
;
1228 s
->max_size
= qemu_chr_be_can_write(chr
);
1232 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1234 FDCharDriver
*s
= chr
->opaque
;
1235 return qio_channel_create_watch(s
->ioc_out
, cond
);
1238 static void fd_chr_update_read_handler(CharDriverState
*chr
,
1239 GMainContext
*context
)
1241 FDCharDriver
*s
= chr
->opaque
;
1243 remove_fd_in_watch(chr
);
1245 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc_in
,
1252 static void fd_chr_free(struct CharDriverState
*chr
)
1254 FDCharDriver
*s
= chr
->opaque
;
1256 remove_fd_in_watch(chr
);
1258 object_unref(OBJECT(s
->ioc_in
));
1261 object_unref(OBJECT(s
->ioc_out
));
1265 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1268 /* open a character device to a unix fd */
1269 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
,
1270 ChardevCommon
*backend
, Error
**errp
)
1272 CharDriverState
*chr
;
1276 chr
= qemu_chr_alloc(backend
, errp
);
1280 s
= g_new0(FDCharDriver
, 1);
1281 s
->ioc_in
= QIO_CHANNEL(qio_channel_file_new_fd(fd_in
));
1282 name
= g_strdup_printf("chardev-file-in-%s", chr
->label
);
1283 qio_channel_set_name(QIO_CHANNEL(s
->ioc_in
), name
);
1285 s
->ioc_out
= QIO_CHANNEL(qio_channel_file_new_fd(fd_out
));
1286 name
= g_strdup_printf("chardev-file-out-%s", chr
->label
);
1287 qio_channel_set_name(QIO_CHANNEL(s
->ioc_out
), name
);
1289 qemu_set_nonblock(fd_out
);
1292 chr
->chr_add_watch
= fd_chr_add_watch
;
1293 chr
->chr_write
= fd_chr_write
;
1294 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1295 chr
->chr_free
= fd_chr_free
;
1300 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
1301 ChardevBackend
*backend
,
1306 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
1310 const char *filename
= opts
->device
;
1311 ChardevCommon
*common
= qapi_ChardevHostdev_base(opts
);
1314 filename_in
= g_strdup_printf("%s.in", filename
);
1315 filename_out
= g_strdup_printf("%s.out", filename
);
1316 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1317 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1318 g_free(filename_in
);
1319 g_free(filename_out
);
1320 if (fd_in
< 0 || fd_out
< 0) {
1325 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1327 error_setg_file_open(errp
, errno
, filename
);
1331 return qemu_chr_open_fd(fd_in
, fd_out
, common
, errp
);
1334 /* init terminal so that we can grab keys */
1335 static struct termios oldtty
;
1336 static int old_fd0_flags
;
1337 static bool stdio_in_use
;
1338 static bool stdio_allow_signal
;
1339 static bool stdio_echo_state
;
1341 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
);
1343 static void term_exit(void)
1345 tcsetattr (0, TCSANOW
, &oldtty
);
1346 fcntl(0, F_SETFL
, old_fd0_flags
);
1349 static void term_stdio_handler(int sig
)
1351 /* restore echo after resume from suspend. */
1352 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
1355 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1359 stdio_echo_state
= echo
;
1362 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1363 |INLCR
|IGNCR
|ICRNL
|IXON
);
1364 tty
.c_oflag
|= OPOST
;
1365 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1366 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1369 tty
.c_cc
[VTIME
] = 0;
1371 if (!stdio_allow_signal
)
1372 tty
.c_lflag
&= ~ISIG
;
1374 tcsetattr (0, TCSANOW
, &tty
);
1377 static void qemu_chr_free_stdio(struct CharDriverState
*chr
)
1383 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
1384 ChardevBackend
*backend
,
1389 ChardevStdio
*opts
= backend
->u
.stdio
.data
;
1390 CharDriverState
*chr
;
1391 struct sigaction act
;
1392 ChardevCommon
*common
= qapi_ChardevStdio_base(opts
);
1394 if (is_daemonized()) {
1395 error_setg(errp
, "cannot use stdio with -daemonize");
1400 error_setg(errp
, "cannot use stdio by multiple character devices");
1404 stdio_in_use
= true;
1405 old_fd0_flags
= fcntl(0, F_GETFL
);
1406 tcgetattr(0, &oldtty
);
1407 qemu_set_nonblock(0);
1410 memset(&act
, 0, sizeof(act
));
1411 act
.sa_handler
= term_stdio_handler
;
1412 sigaction(SIGCONT
, &act
, NULL
);
1414 chr
= qemu_chr_open_fd(0, 1, common
, errp
);
1418 chr
->chr_free
= qemu_chr_free_stdio
;
1419 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1420 if (opts
->has_signal
) {
1421 stdio_allow_signal
= opts
->signal
;
1423 qemu_chr_set_echo_stdio(chr
, false);
1428 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1429 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1430 || defined(__GLIBC__)
1432 #define HAVE_CHARDEV_SERIAL 1
1433 #define HAVE_CHARDEV_PTY 1
1439 /* Protected by the CharDriverState chr_write_lock. */
1445 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1446 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1448 static gboolean
pty_chr_timer(gpointer opaque
)
1450 struct CharDriverState
*chr
= opaque
;
1451 PtyCharDriver
*s
= chr
->opaque
;
1453 qemu_mutex_lock(&chr
->chr_write_lock
);
1456 if (!s
->connected
) {
1458 pty_chr_update_read_handler_locked(chr
);
1460 qemu_mutex_unlock(&chr
->chr_write_lock
);
1464 /* Called with chr_write_lock held. */
1465 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1467 PtyCharDriver
*s
= chr
->opaque
;
1471 g_source_remove(s
->timer_tag
);
1476 name
= g_strdup_printf("pty-timer-secs-%s", chr
->label
);
1477 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1479 name
= g_strdup_printf("pty-timer-ms-%s", chr
->label
);
1480 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1482 g_source_set_name_by_id(s
->timer_tag
, name
);
1486 /* Called with chr_write_lock held. */
1487 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1489 PtyCharDriver
*s
= chr
->opaque
;
1492 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(s
->ioc
);
1495 pfd
.events
= G_IO_OUT
;
1498 rc
= g_poll(&pfd
, 1, 0);
1499 } while (rc
== -1 && errno
== EINTR
);
1502 if (pfd
.revents
& G_IO_HUP
) {
1503 pty_chr_state(chr
, 0);
1505 pty_chr_state(chr
, 1);
1509 static void pty_chr_update_read_handler(CharDriverState
*chr
,
1510 GMainContext
*context
)
1512 qemu_mutex_lock(&chr
->chr_write_lock
);
1513 pty_chr_update_read_handler_locked(chr
);
1514 qemu_mutex_unlock(&chr
->chr_write_lock
);
1517 /* Called with chr_write_lock held. */
1518 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1520 PtyCharDriver
*s
= chr
->opaque
;
1522 if (!s
->connected
) {
1523 /* guest sends data, check for (re-)connect */
1524 pty_chr_update_read_handler_locked(chr
);
1525 if (!s
->connected
) {
1529 return io_channel_send(s
->ioc
, buf
, len
);
1532 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1534 PtyCharDriver
*s
= chr
->opaque
;
1535 if (!s
->connected
) {
1538 return qio_channel_create_watch(s
->ioc
, cond
);
1541 static int pty_chr_read_poll(void *opaque
)
1543 CharDriverState
*chr
= opaque
;
1544 PtyCharDriver
*s
= chr
->opaque
;
1546 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1547 return s
->read_bytes
;
1550 static gboolean
pty_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1552 CharDriverState
*chr
= opaque
;
1553 PtyCharDriver
*s
= chr
->opaque
;
1555 uint8_t buf
[READ_BUF_LEN
];
1559 if (len
> s
->read_bytes
)
1560 len
= s
->read_bytes
;
1564 ret
= qio_channel_read(s
->ioc
, (char *)buf
, len
, NULL
);
1566 pty_chr_state(chr
, 0);
1569 pty_chr_state(chr
, 1);
1570 qemu_chr_be_write(chr
, buf
, ret
);
1575 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1577 CharDriverState
*chr
= opaque
;
1578 PtyCharDriver
*s
= chr
->opaque
;
1581 qemu_chr_be_generic_open(chr
);
1585 /* Called with chr_write_lock held. */
1586 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1588 PtyCharDriver
*s
= chr
->opaque
;
1592 g_source_remove(s
->open_tag
);
1595 remove_fd_in_watch(chr
);
1597 /* (re-)connect poll interval for idle guests: once per second.
1598 * We check more frequently in case the guests sends data to
1599 * the virtual device linked to our pty. */
1600 pty_chr_rearm_timer(chr
, 1000);
1603 g_source_remove(s
->timer_tag
);
1606 if (!s
->connected
) {
1607 g_assert(s
->open_tag
== 0);
1609 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1611 if (!chr
->fd_in_tag
) {
1612 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
1620 static void pty_chr_free(struct CharDriverState
*chr
)
1622 PtyCharDriver
*s
= chr
->opaque
;
1624 qemu_mutex_lock(&chr
->chr_write_lock
);
1625 pty_chr_state(chr
, 0);
1626 object_unref(OBJECT(s
->ioc
));
1628 g_source_remove(s
->timer_tag
);
1631 qemu_mutex_unlock(&chr
->chr_write_lock
);
1633 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1636 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1637 ChardevBackend
*backend
,
1642 CharDriverState
*chr
;
1644 int master_fd
, slave_fd
;
1645 char pty_name
[PATH_MAX
];
1646 ChardevCommon
*common
= backend
->u
.pty
.data
;
1649 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1650 if (master_fd
< 0) {
1651 error_setg_errno(errp
, errno
, "Failed to create PTY");
1656 qemu_set_nonblock(master_fd
);
1658 chr
= qemu_chr_alloc(common
, errp
);
1664 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1665 ret
->pty
= g_strdup(pty_name
);
1666 ret
->has_pty
= true;
1668 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1671 s
= g_new0(PtyCharDriver
, 1);
1673 chr
->chr_write
= pty_chr_write
;
1674 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1675 chr
->chr_free
= pty_chr_free
;
1676 chr
->chr_add_watch
= pty_chr_add_watch
;
1679 s
->ioc
= QIO_CHANNEL(qio_channel_file_new_fd(master_fd
));
1680 name
= g_strdup_printf("chardev-pty-%s", chr
->label
);
1681 qio_channel_set_name(QIO_CHANNEL(s
->ioc
), name
);
1688 static void tty_serial_init(int fd
, int speed
,
1689 int parity
, int data_bits
, int stop_bits
)
1695 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1696 speed
, parity
, data_bits
, stop_bits
);
1698 tcgetattr (fd
, &tty
);
1700 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1701 speed
= speed
* 10 / 11;
1718 /* Non-Posix values follow. They may be unsupported on some systems. */
1720 check_speed(115200);
1722 check_speed(230400);
1725 check_speed(460800);
1728 check_speed(500000);
1731 check_speed(576000);
1734 check_speed(921600);
1737 check_speed(1000000);
1740 check_speed(1152000);
1743 check_speed(1500000);
1746 check_speed(2000000);
1749 check_speed(2500000);
1752 check_speed(3000000);
1755 check_speed(3500000);
1758 check_speed(4000000);
1763 cfsetispeed(&tty
, spd
);
1764 cfsetospeed(&tty
, spd
);
1766 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1767 |INLCR
|IGNCR
|ICRNL
|IXON
);
1768 tty
.c_oflag
|= OPOST
;
1769 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1770 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1791 tty
.c_cflag
|= PARENB
;
1794 tty
.c_cflag
|= PARENB
| PARODD
;
1798 tty
.c_cflag
|= CSTOPB
;
1800 tcsetattr (fd
, TCSANOW
, &tty
);
1803 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1805 FDCharDriver
*s
= chr
->opaque
;
1806 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(s
->ioc_in
);
1809 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1811 QEMUSerialSetParams
*ssp
= arg
;
1812 tty_serial_init(fioc
->fd
,
1813 ssp
->speed
, ssp
->parity
,
1814 ssp
->data_bits
, ssp
->stop_bits
);
1817 case CHR_IOCTL_SERIAL_SET_BREAK
:
1819 int enable
= *(int *)arg
;
1821 tcsendbreak(fioc
->fd
, 1);
1825 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1828 int *targ
= (int *)arg
;
1829 ioctl(fioc
->fd
, TIOCMGET
, &sarg
);
1831 if (sarg
& TIOCM_CTS
)
1832 *targ
|= CHR_TIOCM_CTS
;
1833 if (sarg
& TIOCM_CAR
)
1834 *targ
|= CHR_TIOCM_CAR
;
1835 if (sarg
& TIOCM_DSR
)
1836 *targ
|= CHR_TIOCM_DSR
;
1837 if (sarg
& TIOCM_RI
)
1838 *targ
|= CHR_TIOCM_RI
;
1839 if (sarg
& TIOCM_DTR
)
1840 *targ
|= CHR_TIOCM_DTR
;
1841 if (sarg
& TIOCM_RTS
)
1842 *targ
|= CHR_TIOCM_RTS
;
1845 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1847 int sarg
= *(int *)arg
;
1849 ioctl(fioc
->fd
, TIOCMGET
, &targ
);
1850 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1851 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1852 if (sarg
& CHR_TIOCM_CTS
)
1854 if (sarg
& CHR_TIOCM_CAR
)
1856 if (sarg
& CHR_TIOCM_DSR
)
1858 if (sarg
& CHR_TIOCM_RI
)
1860 if (sarg
& CHR_TIOCM_DTR
)
1862 if (sarg
& CHR_TIOCM_RTS
)
1864 ioctl(fioc
->fd
, TIOCMSET
, &targ
);
1873 static void qemu_chr_free_tty(CharDriverState
*chr
)
1878 static CharDriverState
*qemu_chr_open_tty_fd(int fd
,
1879 ChardevCommon
*backend
,
1883 CharDriverState
*chr
;
1885 tty_serial_init(fd
, 115200, 'N', 8, 1);
1886 chr
= qemu_chr_open_fd(fd
, fd
, backend
, errp
);
1890 chr
->chr_ioctl
= tty_serial_ioctl
;
1891 chr
->chr_free
= qemu_chr_free_tty
;
1894 #endif /* __linux__ || __sun__ */
1896 #if defined(__linux__)
1898 #define HAVE_CHARDEV_PARPORT 1
1903 } ParallelCharDriver
;
1905 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1907 if (s
->mode
!= mode
) {
1909 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1916 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1918 ParallelCharDriver
*drv
= chr
->opaque
;
1923 case CHR_IOCTL_PP_READ_DATA
:
1924 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1926 *(uint8_t *)arg
= b
;
1928 case CHR_IOCTL_PP_WRITE_DATA
:
1929 b
= *(uint8_t *)arg
;
1930 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1933 case CHR_IOCTL_PP_READ_CONTROL
:
1934 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1936 /* Linux gives only the lowest bits, and no way to know data
1937 direction! For better compatibility set the fixed upper
1939 *(uint8_t *)arg
= b
| 0xc0;
1941 case CHR_IOCTL_PP_WRITE_CONTROL
:
1942 b
= *(uint8_t *)arg
;
1943 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1946 case CHR_IOCTL_PP_READ_STATUS
:
1947 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1949 *(uint8_t *)arg
= b
;
1951 case CHR_IOCTL_PP_DATA_DIR
:
1952 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1955 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1956 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1957 struct ParallelIOArg
*parg
= arg
;
1958 int n
= read(fd
, parg
->buffer
, parg
->count
);
1959 if (n
!= parg
->count
) {
1964 case CHR_IOCTL_PP_EPP_READ
:
1965 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1966 struct ParallelIOArg
*parg
= arg
;
1967 int n
= read(fd
, parg
->buffer
, parg
->count
);
1968 if (n
!= parg
->count
) {
1973 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1974 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1975 struct ParallelIOArg
*parg
= arg
;
1976 int n
= write(fd
, parg
->buffer
, parg
->count
);
1977 if (n
!= parg
->count
) {
1982 case CHR_IOCTL_PP_EPP_WRITE
:
1983 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1984 struct ParallelIOArg
*parg
= arg
;
1985 int n
= write(fd
, parg
->buffer
, parg
->count
);
1986 if (n
!= parg
->count
) {
1997 static void pp_free(CharDriverState
*chr
)
1999 ParallelCharDriver
*drv
= chr
->opaque
;
2002 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
2003 ioctl(fd
, PPRELEASE
);
2006 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2009 static CharDriverState
*qemu_chr_open_pp_fd(int fd
,
2010 ChardevCommon
*backend
,
2014 CharDriverState
*chr
;
2015 ParallelCharDriver
*drv
;
2017 if (ioctl(fd
, PPCLAIM
) < 0) {
2018 error_setg_errno(errp
, errno
, "not a parallel port");
2023 chr
= qemu_chr_alloc(backend
, errp
);
2028 drv
= g_new0(ParallelCharDriver
, 1);
2030 chr
->chr_write
= null_chr_write
;
2031 chr
->chr_ioctl
= pp_ioctl
;
2032 chr
->chr_free
= pp_free
;
2035 drv
->mode
= IEEE1284_MODE_COMPAT
;
2039 #endif /* __linux__ */
2041 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
2043 #define HAVE_CHARDEV_PARPORT 1
2045 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
2047 int fd
= (int)(intptr_t)chr
->opaque
;
2051 case CHR_IOCTL_PP_READ_DATA
:
2052 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
2054 *(uint8_t *)arg
= b
;
2056 case CHR_IOCTL_PP_WRITE_DATA
:
2057 b
= *(uint8_t *)arg
;
2058 if (ioctl(fd
, PPISDATA
, &b
) < 0)
2061 case CHR_IOCTL_PP_READ_CONTROL
:
2062 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
2064 *(uint8_t *)arg
= b
;
2066 case CHR_IOCTL_PP_WRITE_CONTROL
:
2067 b
= *(uint8_t *)arg
;
2068 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
2071 case CHR_IOCTL_PP_READ_STATUS
:
2072 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
2074 *(uint8_t *)arg
= b
;
2082 static CharDriverState
*qemu_chr_open_pp_fd(int fd
,
2083 ChardevCommon
*backend
,
2087 CharDriverState
*chr
;
2089 chr
= qemu_chr_alloc(backend
, errp
);
2093 chr
->opaque
= (void *)(intptr_t)fd
;
2094 chr
->chr_write
= null_chr_write
;
2095 chr
->chr_ioctl
= pp_ioctl
;
2103 #define HAVE_CHARDEV_SERIAL 1
2107 HANDLE hcom
, hrecv
, hsend
;
2112 /* Protected by the CharDriverState chr_write_lock. */
2118 HANDLE hInputReadyEvent
;
2119 HANDLE hInputDoneEvent
;
2120 HANDLE hInputThread
;
2121 uint8_t win_stdio_buf
;
2122 } WinStdioCharState
;
2124 #define NSENDBUF 2048
2125 #define NRECVBUF 2048
2126 #define MAXCONNECT 1
2127 #define NTIMEOUT 5000
2129 static int win_chr_poll(void *opaque
);
2130 static int win_chr_pipe_poll(void *opaque
);
2132 static void win_chr_free(CharDriverState
*chr
)
2134 WinCharState
*s
= chr
->opaque
;
2137 CloseHandle(s
->hsend
);
2141 CloseHandle(s
->hrecv
);
2145 CloseHandle(s
->hcom
);
2149 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
2151 qemu_del_polling_cb(win_chr_poll
, chr
);
2153 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2156 static int win_chr_init(CharDriverState
*chr
, const char *filename
, Error
**errp
)
2158 WinCharState
*s
= chr
->opaque
;
2160 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2165 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2167 error_setg(errp
, "Failed CreateEvent");
2170 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2172 error_setg(errp
, "Failed CreateEvent");
2176 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2177 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2178 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2179 error_setg(errp
, "Failed CreateFile (%lu)", GetLastError());
2184 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2185 error_setg(errp
, "Failed SetupComm");
2189 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2190 size
= sizeof(COMMCONFIG
);
2191 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2192 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2193 CommConfigDialog(filename
, NULL
, &comcfg
);
2195 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2196 error_setg(errp
, "Failed SetCommState");
2200 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2201 error_setg(errp
, "Failed SetCommMask");
2205 cto
.ReadIntervalTimeout
= MAXDWORD
;
2206 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2207 error_setg(errp
, "Failed SetCommTimeouts");
2211 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2212 error_setg(errp
, "Failed ClearCommError");
2215 qemu_add_polling_cb(win_chr_poll
, chr
);
2223 /* Called with chr_write_lock held. */
2224 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2226 WinCharState
*s
= chr
->opaque
;
2227 DWORD len
, ret
, size
, err
;
2230 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2231 s
->osend
.hEvent
= s
->hsend
;
2234 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2236 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2238 err
= GetLastError();
2239 if (err
== ERROR_IO_PENDING
) {
2240 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2258 static int win_chr_read_poll(CharDriverState
*chr
)
2260 WinCharState
*s
= chr
->opaque
;
2262 s
->max_size
= qemu_chr_be_can_write(chr
);
2266 static void win_chr_readfile(CharDriverState
*chr
)
2268 WinCharState
*s
= chr
->opaque
;
2270 uint8_t buf
[READ_BUF_LEN
];
2273 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2274 s
->orecv
.hEvent
= s
->hrecv
;
2275 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2277 err
= GetLastError();
2278 if (err
== ERROR_IO_PENDING
) {
2279 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2284 qemu_chr_be_write(chr
, buf
, size
);
2288 static void win_chr_read(CharDriverState
*chr
)
2290 WinCharState
*s
= chr
->opaque
;
2292 if (s
->len
> s
->max_size
)
2293 s
->len
= s
->max_size
;
2297 win_chr_readfile(chr
);
2300 static int win_chr_poll(void *opaque
)
2302 CharDriverState
*chr
= opaque
;
2303 WinCharState
*s
= chr
->opaque
;
2307 ClearCommError(s
->hcom
, &comerr
, &status
);
2308 if (status
.cbInQue
> 0) {
2309 s
->len
= status
.cbInQue
;
2310 win_chr_read_poll(chr
);
2317 static CharDriverState
*qemu_chr_open_win_path(const char *filename
,
2318 ChardevCommon
*backend
,
2321 CharDriverState
*chr
;
2324 chr
= qemu_chr_alloc(backend
, errp
);
2328 s
= g_new0(WinCharState
, 1);
2330 chr
->chr_write
= win_chr_write
;
2331 chr
->chr_free
= win_chr_free
;
2333 if (win_chr_init(chr
, filename
, errp
) < 0) {
2335 qemu_chr_free_common(chr
);
2341 static int win_chr_pipe_poll(void *opaque
)
2343 CharDriverState
*chr
= opaque
;
2344 WinCharState
*s
= chr
->opaque
;
2347 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2350 win_chr_read_poll(chr
);
2357 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
,
2360 WinCharState
*s
= chr
->opaque
;
2368 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2370 error_setg(errp
, "Failed CreateEvent");
2373 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2375 error_setg(errp
, "Failed CreateEvent");
2379 openname
= g_strdup_printf("\\\\.\\pipe\\%s", filename
);
2380 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2381 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2383 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2385 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2386 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
2391 ZeroMemory(&ov
, sizeof(ov
));
2392 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2393 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2395 error_setg(errp
, "Failed ConnectNamedPipe");
2399 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2401 error_setg(errp
, "Failed GetOverlappedResult");
2403 CloseHandle(ov
.hEvent
);
2410 CloseHandle(ov
.hEvent
);
2413 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2422 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
2423 ChardevBackend
*backend
,
2428 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
2429 const char *filename
= opts
->device
;
2430 CharDriverState
*chr
;
2432 ChardevCommon
*common
= qapi_ChardevHostdev_base(opts
);
2434 chr
= qemu_chr_alloc(common
, errp
);
2438 s
= g_new0(WinCharState
, 1);
2440 chr
->chr_write
= win_chr_write
;
2441 chr
->chr_free
= win_chr_free
;
2443 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
2445 qemu_chr_free_common(chr
);
2451 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
,
2452 ChardevCommon
*backend
,
2455 CharDriverState
*chr
;
2458 chr
= qemu_chr_alloc(backend
, errp
);
2462 s
= g_new0(WinCharState
, 1);
2465 chr
->chr_write
= win_chr_write
;
2469 static CharDriverState
*qemu_chr_open_win_con(const char *id
,
2470 ChardevBackend
*backend
,
2475 ChardevCommon
*common
= backend
->u
.console
.data
;
2476 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
),
2480 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2482 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2489 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2499 static void win_stdio_wait_func(void *opaque
)
2501 CharDriverState
*chr
= opaque
;
2502 WinStdioCharState
*stdio
= chr
->opaque
;
2503 INPUT_RECORD buf
[4];
2508 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2511 /* Avoid error storm */
2512 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2516 for (i
= 0; i
< dwSize
; i
++) {
2517 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2519 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2521 if (kev
->uChar
.AsciiChar
!= 0) {
2522 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2523 if (qemu_chr_be_can_write(chr
)) {
2524 uint8_t c
= kev
->uChar
.AsciiChar
;
2525 qemu_chr_be_write(chr
, &c
, 1);
2533 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2535 CharDriverState
*chr
= param
;
2536 WinStdioCharState
*stdio
= chr
->opaque
;
2542 /* Wait for one byte */
2543 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2545 /* Exit in case of error, continue if nothing read */
2553 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2554 if (stdio
->win_stdio_buf
== '\r') {
2558 /* Signal the main thread and wait until the byte was eaten */
2559 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2562 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2568 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2572 static void win_stdio_thread_wait_func(void *opaque
)
2574 CharDriverState
*chr
= opaque
;
2575 WinStdioCharState
*stdio
= chr
->opaque
;
2577 if (qemu_chr_be_can_write(chr
)) {
2578 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2581 SetEvent(stdio
->hInputDoneEvent
);
2584 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2586 WinStdioCharState
*stdio
= chr
->opaque
;
2589 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2592 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2594 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2598 static void win_stdio_free(CharDriverState
*chr
)
2600 WinStdioCharState
*stdio
= chr
->opaque
;
2602 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2603 CloseHandle(stdio
->hInputReadyEvent
);
2605 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2606 CloseHandle(stdio
->hInputDoneEvent
);
2608 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2609 TerminateThread(stdio
->hInputThread
, 0);
2612 g_free(chr
->opaque
);
2615 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
2616 ChardevBackend
*backend
,
2621 CharDriverState
*chr
;
2622 WinStdioCharState
*stdio
;
2625 ChardevCommon
*common
= qapi_ChardevStdio_base(backend
->u
.stdio
.data
);
2627 chr
= qemu_chr_alloc(common
, errp
);
2631 stdio
= g_new0(WinStdioCharState
, 1);
2633 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2634 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2635 error_setg(errp
, "cannot open stdio: invalid handle");
2639 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2641 chr
->opaque
= stdio
;
2642 chr
->chr_write
= win_stdio_write
;
2643 chr
->chr_free
= win_stdio_free
;
2646 if (qemu_add_wait_object(stdio
->hStdIn
,
2647 win_stdio_wait_func
, chr
)) {
2648 error_setg(errp
, "qemu_add_wait_object: failed");
2654 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2655 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2656 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2657 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2658 error_setg(errp
, "cannot create event");
2661 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2662 win_stdio_thread_wait_func
, chr
)) {
2663 error_setg(errp
, "qemu_add_wait_object: failed");
2666 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2669 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
2670 error_setg(errp
, "cannot create stdio thread");
2675 dwMode
|= ENABLE_LINE_INPUT
;
2678 /* set the terminal in raw mode */
2679 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2680 dwMode
|= ENABLE_PROCESSED_INPUT
;
2683 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2685 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2686 qemu_chr_set_echo_win_stdio(chr
, false);
2691 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2693 CloseHandle(stdio
->hInputReadyEvent
);
2694 CloseHandle(stdio
->hInputDoneEvent
);
2696 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2699 #endif /* !_WIN32 */
2701 /***********************************************************/
2702 /* UDP Net console */
2706 uint8_t buf
[READ_BUF_LEN
];
2712 /* Called with chr_write_lock held. */
2713 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2715 NetCharDriver
*s
= chr
->opaque
;
2717 return qio_channel_write(
2718 s
->ioc
, (const char *)buf
, len
, NULL
);
2721 static int udp_chr_read_poll(void *opaque
)
2723 CharDriverState
*chr
= opaque
;
2724 NetCharDriver
*s
= chr
->opaque
;
2726 s
->max_size
= qemu_chr_be_can_write(chr
);
2728 /* If there were any stray characters in the queue process them
2731 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2732 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2734 s
->max_size
= qemu_chr_be_can_write(chr
);
2739 static gboolean
udp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2741 CharDriverState
*chr
= opaque
;
2742 NetCharDriver
*s
= chr
->opaque
;
2745 if (s
->max_size
== 0) {
2748 ret
= qio_channel_read(
2749 s
->ioc
, (char *)s
->buf
, sizeof(s
->buf
), NULL
);
2751 remove_fd_in_watch(chr
);
2757 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2758 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2760 s
->max_size
= qemu_chr_be_can_write(chr
);
2766 static void udp_chr_update_read_handler(CharDriverState
*chr
,
2767 GMainContext
*context
)
2769 NetCharDriver
*s
= chr
->opaque
;
2771 remove_fd_in_watch(chr
);
2773 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
2780 static void udp_chr_free(CharDriverState
*chr
)
2782 NetCharDriver
*s
= chr
->opaque
;
2784 remove_fd_in_watch(chr
);
2786 object_unref(OBJECT(s
->ioc
));
2789 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2792 static CharDriverState
*qemu_chr_open_udp(QIOChannelSocket
*sioc
,
2793 ChardevCommon
*backend
,
2797 CharDriverState
*chr
= NULL
;
2798 NetCharDriver
*s
= NULL
;
2800 chr
= qemu_chr_alloc(backend
, errp
);
2804 s
= g_new0(NetCharDriver
, 1);
2806 s
->ioc
= QIO_CHANNEL(sioc
);
2810 chr
->chr_write
= udp_chr_write
;
2811 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2812 chr
->chr_free
= udp_chr_free
;
2813 /* be isn't opened until we get a connection */
2818 /***********************************************************/
2819 /* TCP Net console */
2822 QIOChannel
*ioc
; /* Client I/O channel */
2823 QIOChannelSocket
*sioc
; /* Client master channel */
2824 QIOChannelSocket
*listen_ioc
;
2826 QCryptoTLSCreds
*tls_creds
;
2833 size_t read_msgfds_num
;
2835 size_t write_msgfds_num
;
2837 SocketAddress
*addr
;
2841 guint reconnect_timer
;
2842 int64_t reconnect_time
;
2843 bool connect_err_reported
;
2846 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2848 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2850 TCPCharDriver
*s
= chr
->opaque
;
2852 assert(s
->connected
== 0);
2853 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2854 socket_reconnect_timeout
, chr
);
2855 name
= g_strdup_printf("chardev-socket-reconnect-%s", chr
->label
);
2856 g_source_set_name_by_id(s
->reconnect_timer
, name
);
2860 static void check_report_connect_error(CharDriverState
*chr
,
2863 TCPCharDriver
*s
= chr
->opaque
;
2865 if (!s
->connect_err_reported
) {
2866 error_report("Unable to connect character device %s: %s",
2867 chr
->label
, error_get_pretty(err
));
2868 s
->connect_err_reported
= true;
2870 qemu_chr_socket_restart_timer(chr
);
2873 static gboolean
tcp_chr_accept(QIOChannel
*chan
,
2877 /* Called with chr_write_lock held. */
2878 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2880 TCPCharDriver
*s
= chr
->opaque
;
2882 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
2884 s
->write_msgfds_num
);
2886 /* free the written msgfds, no matter what */
2887 if (s
->write_msgfds_num
) {
2888 g_free(s
->write_msgfds
);
2889 s
->write_msgfds
= 0;
2890 s
->write_msgfds_num
= 0;
2895 /* XXX: indicate an error ? */
2900 static int tcp_chr_read_poll(void *opaque
)
2902 CharDriverState
*chr
= opaque
;
2903 TCPCharDriver
*s
= chr
->opaque
;
2906 s
->max_size
= qemu_chr_be_can_write(chr
);
2911 #define IAC_BREAK 243
2912 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2914 uint8_t *buf
, int *size
)
2916 /* Handle any telnet client's basic IAC options to satisfy char by
2917 * char mode with no echo. All IAC options will be removed from
2918 * the buf and the do_telnetopt variable will be used to track the
2919 * state of the width of the IAC information.
2921 * IAC commands come in sets of 3 bytes with the exception of the
2922 * "IAC BREAK" command and the double IAC.
2928 for (i
= 0; i
< *size
; i
++) {
2929 if (s
->do_telnetopt
> 1) {
2930 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2931 /* Double IAC means send an IAC */
2935 s
->do_telnetopt
= 1;
2937 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2938 /* Handle IAC break commands by sending a serial break */
2939 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2944 if (s
->do_telnetopt
>= 4) {
2945 s
->do_telnetopt
= 1;
2948 if ((unsigned char)buf
[i
] == IAC
) {
2949 s
->do_telnetopt
= 2;
2960 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2962 TCPCharDriver
*s
= chr
->opaque
;
2963 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2965 assert(num
<= TCP_MAX_FDS
);
2970 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2972 /* Close unused fds */
2973 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2974 close(s
->read_msgfds
[i
]);
2977 g_free(s
->read_msgfds
);
2979 s
->read_msgfds_num
= 0;
2985 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2987 TCPCharDriver
*s
= chr
->opaque
;
2989 /* clear old pending fd array */
2990 g_free(s
->write_msgfds
);
2991 s
->write_msgfds
= NULL
;
2992 s
->write_msgfds_num
= 0;
2994 if (!s
->connected
||
2995 !qio_channel_has_feature(s
->ioc
,
2996 QIO_CHANNEL_FEATURE_FD_PASS
)) {
3001 s
->write_msgfds
= g_new(int, num
);
3002 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
3005 s
->write_msgfds_num
= num
;
3010 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
3012 TCPCharDriver
*s
= chr
->opaque
;
3013 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
3017 size_t msgfds_num
= 0;
3019 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
3020 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
3021 &msgfds
, &msgfds_num
,
3024 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
3029 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
3032 } else if (ret
== -1) {
3037 /* close and clean read_msgfds */
3038 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3039 close(s
->read_msgfds
[i
]);
3042 if (s
->read_msgfds_num
) {
3043 g_free(s
->read_msgfds
);
3046 s
->read_msgfds
= msgfds
;
3047 s
->read_msgfds_num
= msgfds_num
;
3050 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3051 int fd
= s
->read_msgfds
[i
];
3056 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
3059 #ifndef MSG_CMSG_CLOEXEC
3060 qemu_set_cloexec(fd
);
3067 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
3069 TCPCharDriver
*s
= chr
->opaque
;
3070 return qio_channel_create_watch(s
->ioc
, cond
);
3073 static void tcp_chr_free_connection(CharDriverState
*chr
)
3075 TCPCharDriver
*s
= chr
->opaque
;
3078 if (!s
->connected
) {
3082 if (s
->read_msgfds_num
) {
3083 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3084 close(s
->read_msgfds
[i
]);
3086 g_free(s
->read_msgfds
);
3087 s
->read_msgfds
= NULL
;
3088 s
->read_msgfds_num
= 0;
3091 tcp_set_msgfds(chr
, NULL
, 0);
3092 remove_fd_in_watch(chr
);
3093 object_unref(OBJECT(s
->sioc
));
3095 object_unref(OBJECT(s
->ioc
));
3097 g_free(chr
->filename
);
3098 chr
->filename
= NULL
;
3102 static void tcp_chr_disconnect(CharDriverState
*chr
)
3104 TCPCharDriver
*s
= chr
->opaque
;
3106 if (!s
->connected
) {
3110 tcp_chr_free_connection(chr
);
3112 if (s
->listen_ioc
) {
3113 s
->listen_tag
= qio_channel_add_watch(
3114 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, tcp_chr_accept
, chr
, NULL
);
3116 chr
->filename
= SocketAddress_to_str("disconnected:", s
->addr
,
3117 s
->is_listen
, s
->is_telnet
);
3118 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3119 if (s
->reconnect_time
) {
3120 qemu_chr_socket_restart_timer(chr
);
3124 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
3126 CharDriverState
*chr
= opaque
;
3127 TCPCharDriver
*s
= chr
->opaque
;
3128 uint8_t buf
[READ_BUF_LEN
];
3131 if (!s
->connected
|| s
->max_size
<= 0) {
3135 if (len
> s
->max_size
)
3137 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
3138 if (size
== 0 || size
== -1) {
3139 /* connection closed */
3140 tcp_chr_disconnect(chr
);
3141 } else if (size
> 0) {
3142 if (s
->do_telnetopt
)
3143 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
3145 qemu_chr_be_write(chr
, buf
, size
);
3151 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3153 TCPCharDriver
*s
= chr
->opaque
;
3156 if (!s
->connected
) {
3160 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
3162 /* connection closed */
3163 tcp_chr_disconnect(chr
);
3169 static void tcp_chr_connect(void *opaque
)
3171 CharDriverState
*chr
= opaque
;
3172 TCPCharDriver
*s
= chr
->opaque
;
3174 g_free(chr
->filename
);
3175 chr
->filename
= sockaddr_to_str(
3176 &s
->sioc
->localAddr
, s
->sioc
->localAddrLen
,
3177 &s
->sioc
->remoteAddr
, s
->sioc
->remoteAddrLen
,
3178 s
->is_listen
, s
->is_telnet
);
3182 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
3187 qemu_chr_be_generic_open(chr
);
3190 static void tcp_chr_update_read_handler(CharDriverState
*chr
,
3191 GMainContext
*context
)
3193 TCPCharDriver
*s
= chr
->opaque
;
3195 if (!s
->connected
) {
3199 remove_fd_in_watch(chr
);
3201 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
3209 CharDriverState
*chr
;
3212 } TCPCharDriverTelnetInit
;
3214 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
3215 GIOCondition cond G_GNUC_UNUSED
,
3218 TCPCharDriverTelnetInit
*init
= user_data
;
3221 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
3223 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
3226 tcp_chr_disconnect(init
->chr
);
3230 init
->buflen
-= ret
;
3232 if (init
->buflen
== 0) {
3233 tcp_chr_connect(init
->chr
);
3237 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
3242 static void tcp_chr_telnet_init(CharDriverState
*chr
)
3244 TCPCharDriver
*s
= chr
->opaque
;
3245 TCPCharDriverTelnetInit
*init
=
3246 g_new0(TCPCharDriverTelnetInit
, 1);
3252 #define IACSET(x, a, b, c) \
3259 /* Prep the telnet negotion to put telnet in binary,
3260 * no echo, single char mode */
3261 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3262 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3263 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3264 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3268 qio_channel_add_watch(
3270 tcp_chr_telnet_init_io
,
3275 static void tcp_chr_tls_handshake(Object
*source
,
3279 CharDriverState
*chr
= user_data
;
3280 TCPCharDriver
*s
= chr
->opaque
;
3283 tcp_chr_disconnect(chr
);
3285 if (s
->do_telnetopt
) {
3286 tcp_chr_telnet_init(chr
);
3288 tcp_chr_connect(chr
);
3294 static void tcp_chr_tls_init(CharDriverState
*chr
)
3296 TCPCharDriver
*s
= chr
->opaque
;
3297 QIOChannelTLS
*tioc
;
3302 tioc
= qio_channel_tls_new_server(
3303 s
->ioc
, s
->tls_creds
,
3304 NULL
, /* XXX Use an ACL */
3307 tioc
= qio_channel_tls_new_client(
3308 s
->ioc
, s
->tls_creds
,
3309 s
->addr
->u
.inet
.data
->host
,
3314 tcp_chr_disconnect(chr
);
3317 name
= g_strdup_printf("chardev-tls-%s-%s",
3318 s
->is_listen
? "server" : "client",
3320 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
3322 object_unref(OBJECT(s
->ioc
));
3323 s
->ioc
= QIO_CHANNEL(tioc
);
3325 qio_channel_tls_handshake(tioc
,
3326 tcp_chr_tls_handshake
,
3332 static void tcp_chr_set_client_ioc_name(CharDriverState
*chr
,
3333 QIOChannelSocket
*sioc
)
3335 TCPCharDriver
*s
= chr
->opaque
;
3337 name
= g_strdup_printf("chardev-tcp-%s-%s",
3338 s
->is_listen
? "server" : "client",
3340 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
3345 static int tcp_chr_new_client(CharDriverState
*chr
, QIOChannelSocket
*sioc
)
3347 TCPCharDriver
*s
= chr
->opaque
;
3348 if (s
->ioc
!= NULL
) {
3352 s
->ioc
= QIO_CHANNEL(sioc
);
3353 object_ref(OBJECT(sioc
));
3355 object_ref(OBJECT(sioc
));
3357 qio_channel_set_blocking(s
->ioc
, false, NULL
);
3359 if (s
->do_nodelay
) {
3360 qio_channel_set_delay(s
->ioc
, false);
3362 if (s
->listen_tag
) {
3363 g_source_remove(s
->listen_tag
);
3368 tcp_chr_tls_init(chr
);
3370 if (s
->do_telnetopt
) {
3371 tcp_chr_telnet_init(chr
);
3373 tcp_chr_connect(chr
);
3381 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
3384 QIOChannelSocket
*sioc
;
3386 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
3390 tcp_chr_set_client_ioc_name(chr
, sioc
);
3391 ret
= tcp_chr_new_client(chr
, sioc
);
3392 object_unref(OBJECT(sioc
));
3396 static gboolean
tcp_chr_accept(QIOChannel
*channel
,
3400 CharDriverState
*chr
= opaque
;
3401 QIOChannelSocket
*sioc
;
3403 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel
),
3409 tcp_chr_new_client(chr
, sioc
);
3411 object_unref(OBJECT(sioc
));
3416 static int tcp_chr_wait_connected(CharDriverState
*chr
, Error
**errp
)
3418 TCPCharDriver
*s
= chr
->opaque
;
3419 QIOChannelSocket
*sioc
;
3421 /* It can't wait on s->connected, since it is set asynchronously
3422 * in TLS and telnet cases, only wait for an accepted socket */
3425 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
3427 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), true, NULL
);
3428 tcp_chr_accept(QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, chr
);
3429 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), false, NULL
);
3431 sioc
= qio_channel_socket_new();
3432 tcp_chr_set_client_ioc_name(chr
, sioc
);
3433 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
3434 object_unref(OBJECT(sioc
));
3437 tcp_chr_new_client(chr
, sioc
);
3438 object_unref(OBJECT(sioc
));
3445 static int qemu_chr_wait_connected(CharDriverState
*chr
, Error
**errp
)
3447 if (chr
->chr_wait_connected
) {
3448 return chr
->chr_wait_connected(chr
, errp
);
3454 int qemu_chr_fe_wait_connected(CharBackend
*be
, Error
**errp
)
3457 error_setg(errp
, "missing associated backend");
3461 return qemu_chr_wait_connected(be
->chr
, errp
);
3464 static void tcp_chr_free(CharDriverState
*chr
)
3466 TCPCharDriver
*s
= chr
->opaque
;
3468 tcp_chr_free_connection(chr
);
3470 if (s
->reconnect_timer
) {
3471 g_source_remove(s
->reconnect_timer
);
3472 s
->reconnect_timer
= 0;
3474 qapi_free_SocketAddress(s
->addr
);
3475 if (s
->listen_tag
) {
3476 g_source_remove(s
->listen_tag
);
3479 if (s
->listen_ioc
) {
3480 object_unref(OBJECT(s
->listen_ioc
));
3483 object_unref(OBJECT(s
->tls_creds
));
3486 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3490 static void qemu_chr_socket_connected(Object
*src
, Error
*err
, void *opaque
)
3492 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(src
);
3493 CharDriverState
*chr
= opaque
;
3494 TCPCharDriver
*s
= chr
->opaque
;
3497 check_report_connect_error(chr
, err
);
3502 s
->connect_err_reported
= false;
3503 tcp_chr_new_client(chr
, sioc
);
3504 object_unref(OBJECT(sioc
));
3508 /*********************************************************/
3509 /* Ring buffer chardev */
3516 } RingBufCharDriver
;
3518 static size_t ringbuf_count(const CharDriverState
*chr
)
3520 const RingBufCharDriver
*d
= chr
->opaque
;
3522 return d
->prod
- d
->cons
;
3525 /* Called with chr_write_lock held. */
3526 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3528 RingBufCharDriver
*d
= chr
->opaque
;
3531 if (!buf
|| (len
< 0)) {
3535 for (i
= 0; i
< len
; i
++ ) {
3536 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3537 if (d
->prod
- d
->cons
> d
->size
) {
3538 d
->cons
= d
->prod
- d
->size
;
3545 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3547 RingBufCharDriver
*d
= chr
->opaque
;
3550 qemu_mutex_lock(&chr
->chr_write_lock
);
3551 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3552 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3554 qemu_mutex_unlock(&chr
->chr_write_lock
);
3559 static void ringbuf_chr_free(struct CharDriverState
*chr
)
3561 RingBufCharDriver
*d
= chr
->opaque
;
3568 static CharDriverState
*qemu_chr_open_ringbuf(const char *id
,
3569 ChardevBackend
*backend
,
3574 ChardevRingbuf
*opts
= backend
->u
.ringbuf
.data
;
3575 ChardevCommon
*common
= qapi_ChardevRingbuf_base(opts
);
3576 CharDriverState
*chr
;
3577 RingBufCharDriver
*d
;
3579 chr
= qemu_chr_alloc(common
, errp
);
3583 d
= g_malloc(sizeof(*d
));
3585 d
->size
= opts
->has_size
? opts
->size
: 65536;
3587 /* The size must be power of 2 */
3588 if (d
->size
& (d
->size
- 1)) {
3589 error_setg(errp
, "size of ringbuf chardev must be power of two");
3595 d
->cbuf
= g_malloc0(d
->size
);
3598 chr
->chr_write
= ringbuf_chr_write
;
3599 chr
->chr_free
= ringbuf_chr_free
;
3605 qemu_chr_free_common(chr
);
3609 bool chr_is_ringbuf(const CharDriverState
*chr
)
3611 return chr
->chr_write
== ringbuf_chr_write
;
3614 void qmp_ringbuf_write(const char *device
, const char *data
,
3615 bool has_format
, enum DataFormat format
,
3618 CharDriverState
*chr
;
3619 const uint8_t *write_data
;
3623 chr
= qemu_chr_find(device
);
3625 error_setg(errp
, "Device '%s' not found", device
);
3629 if (!chr_is_ringbuf(chr
)) {
3630 error_setg(errp
,"%s is not a ringbuf device", device
);
3634 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3635 write_data
= qbase64_decode(data
, -1,
3642 write_data
= (uint8_t *)data
;
3643 write_count
= strlen(data
);
3646 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3648 if (write_data
!= (uint8_t *)data
) {
3649 g_free((void *)write_data
);
3653 error_setg(errp
, "Failed to write to device %s", device
);
3658 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3659 bool has_format
, enum DataFormat format
,
3662 CharDriverState
*chr
;
3667 chr
= qemu_chr_find(device
);
3669 error_setg(errp
, "Device '%s' not found", device
);
3673 if (!chr_is_ringbuf(chr
)) {
3674 error_setg(errp
,"%s is not a ringbuf device", device
);
3679 error_setg(errp
, "size must be greater than zero");
3683 count
= ringbuf_count(chr
);
3684 size
= size
> count
? count
: size
;
3685 read_data
= g_malloc(size
+ 1);
3687 ringbuf_chr_read(chr
, read_data
, size
);
3689 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3690 data
= g_base64_encode(read_data
, size
);
3694 * FIXME should read only complete, valid UTF-8 characters up
3695 * to @size bytes. Invalid sequences should be replaced by a
3696 * suitable replacement character. Except when (and only
3697 * when) ring buffer lost characters since last read, initial
3698 * continuation characters should be dropped.
3700 read_data
[size
] = 0;
3701 data
= (char *)read_data
;
3707 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3709 char host
[65], port
[33], width
[8], height
[8];
3713 Error
*local_err
= NULL
;
3715 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3717 error_report_err(local_err
);
3721 if (strstart(filename
, "mon:", &p
)) {
3723 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3724 if (strcmp(filename
, "stdio") == 0) {
3725 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3726 * but pass it to the guest. Handle this only for compat syntax,
3727 * for -chardev syntax we have special option for this.
3728 * This is what -nographic did, redirecting+muxing serial+monitor
3729 * to stdio causing Ctrl+C to be passed to guest. */
3730 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3734 if (strcmp(filename
, "null") == 0 ||
3735 strcmp(filename
, "pty") == 0 ||
3736 strcmp(filename
, "msmouse") == 0 ||
3737 strcmp(filename
, "braille") == 0 ||
3738 strcmp(filename
, "testdev") == 0 ||
3739 strcmp(filename
, "stdio") == 0) {
3740 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3743 if (strstart(filename
, "vc", &p
)) {
3744 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3746 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3748 qemu_opt_set(opts
, "width", width
, &error_abort
);
3749 qemu_opt_set(opts
, "height", height
, &error_abort
);
3750 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3752 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3753 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3760 if (strcmp(filename
, "con:") == 0) {
3761 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3764 if (strstart(filename
, "COM", NULL
)) {
3765 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3766 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3769 if (strstart(filename
, "file:", &p
)) {
3770 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3771 qemu_opt_set(opts
, "path", p
, &error_abort
);
3774 if (strstart(filename
, "pipe:", &p
)) {
3775 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3776 qemu_opt_set(opts
, "path", p
, &error_abort
);
3779 if (strstart(filename
, "tcp:", &p
) ||
3780 strstart(filename
, "telnet:", &p
)) {
3781 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3783 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3786 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3787 qemu_opt_set(opts
, "host", host
, &error_abort
);
3788 qemu_opt_set(opts
, "port", port
, &error_abort
);
3789 if (p
[pos
] == ',') {
3790 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3792 error_report_err(local_err
);
3796 if (strstart(filename
, "telnet:", &p
))
3797 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3800 if (strstart(filename
, "udp:", &p
)) {
3801 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3802 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3804 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3808 qemu_opt_set(opts
, "host", host
, &error_abort
);
3809 qemu_opt_set(opts
, "port", port
, &error_abort
);
3810 if (p
[pos
] == '@') {
3812 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3814 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3818 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3819 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3823 if (strstart(filename
, "unix:", &p
)) {
3824 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3825 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3827 error_report_err(local_err
);
3832 if (strstart(filename
, "/dev/parport", NULL
) ||
3833 strstart(filename
, "/dev/ppi", NULL
)) {
3834 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3835 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3838 if (strstart(filename
, "/dev/", NULL
)) {
3839 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3840 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3845 qemu_opts_del(opts
);
3849 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
3851 const char *logfile
= qemu_opt_get(opts
, "logfile");
3853 backend
->has_logfile
= logfile
!= NULL
;
3854 backend
->logfile
= logfile
? g_strdup(logfile
) : NULL
;
3856 backend
->has_logappend
= true;
3857 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
3861 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3864 const char *path
= qemu_opt_get(opts
, "path");
3868 error_setg(errp
, "chardev: file: no filename given");
3871 file
= backend
->u
.file
.data
= g_new0(ChardevFile
, 1);
3872 qemu_chr_parse_common(opts
, qapi_ChardevFile_base(file
));
3873 file
->out
= g_strdup(path
);
3875 file
->has_append
= true;
3876 file
->append
= qemu_opt_get_bool(opts
, "append", false);
3879 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3882 ChardevStdio
*stdio
;
3884 stdio
= backend
->u
.stdio
.data
= g_new0(ChardevStdio
, 1);
3885 qemu_chr_parse_common(opts
, qapi_ChardevStdio_base(stdio
));
3886 stdio
->has_signal
= true;
3887 stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3890 #ifdef HAVE_CHARDEV_SERIAL
3891 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3894 const char *device
= qemu_opt_get(opts
, "path");
3895 ChardevHostdev
*serial
;
3897 if (device
== NULL
) {
3898 error_setg(errp
, "chardev: serial/tty: no device path given");
3901 serial
= backend
->u
.serial
.data
= g_new0(ChardevHostdev
, 1);
3902 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(serial
));
3903 serial
->device
= g_strdup(device
);
3907 #ifdef HAVE_CHARDEV_PARPORT
3908 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3911 const char *device
= qemu_opt_get(opts
, "path");
3912 ChardevHostdev
*parallel
;
3914 if (device
== NULL
) {
3915 error_setg(errp
, "chardev: parallel: no device path given");
3918 parallel
= backend
->u
.parallel
.data
= g_new0(ChardevHostdev
, 1);
3919 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(parallel
));
3920 parallel
->device
= g_strdup(device
);
3924 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3927 const char *device
= qemu_opt_get(opts
, "path");
3928 ChardevHostdev
*dev
;
3930 if (device
== NULL
) {
3931 error_setg(errp
, "chardev: pipe: no device path given");
3934 dev
= backend
->u
.pipe
.data
= g_new0(ChardevHostdev
, 1);
3935 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(dev
));
3936 dev
->device
= g_strdup(device
);
3939 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3943 ChardevRingbuf
*ringbuf
;
3945 ringbuf
= backend
->u
.ringbuf
.data
= g_new0(ChardevRingbuf
, 1);
3946 qemu_chr_parse_common(opts
, qapi_ChardevRingbuf_base(ringbuf
));
3948 val
= qemu_opt_get_size(opts
, "size", 0);
3950 ringbuf
->has_size
= true;
3951 ringbuf
->size
= val
;
3955 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3958 const char *chardev
= qemu_opt_get(opts
, "chardev");
3961 if (chardev
== NULL
) {
3962 error_setg(errp
, "chardev: mux: no chardev given");
3965 mux
= backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
3966 qemu_chr_parse_common(opts
, qapi_ChardevMux_base(mux
));
3967 mux
->chardev
= g_strdup(chardev
);
3970 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3973 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3974 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3975 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3976 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3977 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3978 const char *path
= qemu_opt_get(opts
, "path");
3979 const char *host
= qemu_opt_get(opts
, "host");
3980 const char *port
= qemu_opt_get(opts
, "port");
3981 const char *tls_creds
= qemu_opt_get(opts
, "tls-creds");
3982 SocketAddress
*addr
;
3983 ChardevSocket
*sock
;
3987 error_setg(errp
, "chardev: socket: no host given");
3991 error_setg(errp
, "chardev: socket: no port given");
3996 error_setg(errp
, "TLS can only be used over TCP socket");
4001 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
4002 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
4004 sock
->has_nodelay
= true;
4005 sock
->nodelay
= do_nodelay
;
4006 sock
->has_server
= true;
4007 sock
->server
= is_listen
;
4008 sock
->has_telnet
= true;
4009 sock
->telnet
= is_telnet
;
4010 sock
->has_wait
= true;
4011 sock
->wait
= is_waitconnect
;
4012 sock
->has_reconnect
= true;
4013 sock
->reconnect
= reconnect
;
4014 sock
->tls_creds
= g_strdup(tls_creds
);
4016 addr
= g_new0(SocketAddress
, 1);
4018 UnixSocketAddress
*q_unix
;
4019 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
4020 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
4021 q_unix
->path
= g_strdup(path
);
4023 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
4024 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
4025 *addr
->u
.inet
.data
= (InetSocketAddress
) {
4026 .host
= g_strdup(host
),
4027 .port
= g_strdup(port
),
4028 .has_to
= qemu_opt_get(opts
, "to"),
4029 .to
= qemu_opt_get_number(opts
, "to", 0),
4030 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
4031 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
4032 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
4033 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
4039 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
4042 const char *host
= qemu_opt_get(opts
, "host");
4043 const char *port
= qemu_opt_get(opts
, "port");
4044 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
4045 const char *localport
= qemu_opt_get(opts
, "localport");
4046 bool has_local
= false;
4047 SocketAddress
*addr
;
4050 if (host
== NULL
|| strlen(host
) == 0) {
4053 if (port
== NULL
|| strlen(port
) == 0) {
4054 error_setg(errp
, "chardev: udp: remote port not specified");
4057 if (localport
== NULL
|| strlen(localport
) == 0) {
4062 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
4068 udp
= backend
->u
.udp
.data
= g_new0(ChardevUdp
, 1);
4069 qemu_chr_parse_common(opts
, qapi_ChardevUdp_base(udp
));
4071 addr
= g_new0(SocketAddress
, 1);
4072 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
4073 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
4074 *addr
->u
.inet
.data
= (InetSocketAddress
) {
4075 .host
= g_strdup(host
),
4076 .port
= g_strdup(port
),
4077 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
4078 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
4079 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
4080 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
4085 udp
->has_local
= true;
4086 addr
= g_new0(SocketAddress
, 1);
4087 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
4088 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
4089 *addr
->u
.inet
.data
= (InetSocketAddress
) {
4090 .host
= g_strdup(localaddr
),
4091 .port
= g_strdup(localport
),
4097 typedef struct CharDriver
{
4099 ChardevBackendKind kind
;
4100 CharDriverParse
*parse
;
4101 CharDriverCreate
*create
;
4104 static GSList
*backends
;
4106 void register_char_driver(const char *name
, ChardevBackendKind kind
,
4107 CharDriverParse
*parse
, CharDriverCreate
*create
)
4111 s
= g_malloc0(sizeof(*s
));
4112 s
->name
= g_strdup(name
);
4117 backends
= g_slist_append(backends
, s
);
4120 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
4123 Error
*local_err
= NULL
;
4125 CharDriverState
*chr
;
4127 ChardevReturn
*ret
= NULL
;
4128 ChardevBackend
*backend
;
4129 const char *id
= qemu_opts_id(opts
);
4132 if (qemu_opt_get(opts
, "backend") == NULL
) {
4133 error_setg(errp
, "chardev: \"%s\" missing backend",
4134 qemu_opts_id(opts
));
4138 if (is_help_option(qemu_opt_get(opts
, "backend"))) {
4139 fprintf(stderr
, "Available chardev backend types:\n");
4140 for (i
= backends
; i
; i
= i
->next
) {
4142 fprintf(stderr
, "%s\n", cd
->name
);
4144 exit(!is_help_option(qemu_opt_get(opts
, "backend")));
4148 error_setg(errp
, "chardev: no id specified");
4152 for (i
= backends
; i
; i
= i
->next
) {
4155 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
4160 error_setg(errp
, "chardev: backend \"%s\" not found",
4161 qemu_opt_get(opts
, "backend"));
4165 backend
= g_new0(ChardevBackend
, 1);
4167 if (qemu_opt_get_bool(opts
, "mux", 0)) {
4168 bid
= g_strdup_printf("%s-base", id
);
4172 backend
->type
= cd
->kind
;
4174 cd
->parse(opts
, backend
, &local_err
);
4176 error_propagate(errp
, local_err
);
4180 ChardevCommon
*cc
= g_new0(ChardevCommon
, 1);
4181 qemu_chr_parse_common(opts
, cc
);
4182 backend
->u
.null
.data
= cc
; /* Any ChardevCommon member would work */
4185 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
4191 qapi_free_ChardevBackend(backend
);
4192 qapi_free_ChardevReturn(ret
);
4193 backend
= g_new0(ChardevBackend
, 1);
4194 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
4195 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
4196 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
4197 ret
= qmp_chardev_add(id
, backend
, errp
);
4199 chr
= qemu_chr_find(bid
);
4200 qemu_chr_delete(chr
);
4206 chr
= qemu_chr_find(id
);
4209 qapi_free_ChardevBackend(backend
);
4210 qapi_free_ChardevReturn(ret
);
4218 CharDriverState
*qemu_chr_new_noreplay(const char *label
, const char *filename
)
4221 CharDriverState
*chr
;
4225 if (strstart(filename
, "chardev:", &p
)) {
4226 return qemu_chr_find(p
);
4229 opts
= qemu_chr_parse_compat(label
, filename
);
4233 chr
= qemu_chr_new_from_opts(opts
, &err
);
4235 error_report_err(err
);
4237 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
4238 monitor_init(chr
, MONITOR_USE_READLINE
);
4240 qemu_opts_del(opts
);
4244 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
)
4246 CharDriverState
*chr
;
4247 chr
= qemu_chr_new_noreplay(label
, filename
);
4249 chr
->replay
= replay_mode
!= REPLAY_MODE_NONE
;
4250 if (chr
->replay
&& chr
->chr_ioctl
) {
4252 "Replay: ioctl is not supported for serial devices yet\n");
4254 replay_register_char_driver(chr
);
4259 void qemu_chr_fe_set_echo(CharBackend
*be
, bool echo
)
4261 CharDriverState
*chr
= be
->chr
;
4263 if (chr
&& chr
->chr_set_echo
) {
4264 chr
->chr_set_echo(chr
, echo
);
4268 void qemu_chr_fe_set_open(CharBackend
*be
, int fe_open
)
4270 CharDriverState
*chr
= be
->chr
;
4276 if (be
->fe_open
== fe_open
) {
4279 be
->fe_open
= fe_open
;
4280 if (chr
->chr_set_fe_open
) {
4281 chr
->chr_set_fe_open(chr
, fe_open
);
4285 guint
qemu_chr_fe_add_watch(CharBackend
*be
, GIOCondition cond
,
4286 GIOFunc func
, void *user_data
)
4288 CharDriverState
*s
= be
->chr
;
4292 if (!s
|| s
->chr_add_watch
== NULL
) {
4296 src
= s
->chr_add_watch(s
, cond
);
4301 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
4302 tag
= g_source_attach(src
, NULL
);
4303 g_source_unref(src
);
4308 void qemu_chr_fe_disconnect(CharBackend
*be
)
4310 CharDriverState
*chr
= be
->chr
;
4312 if (chr
&& chr
->chr_disconnect
) {
4313 chr
->chr_disconnect(chr
);
4317 static void qemu_chr_free_common(CharDriverState
*chr
)
4320 chr
->be
->chr
= NULL
;
4322 g_free(chr
->filename
);
4324 if (chr
->logfd
!= -1) {
4327 qemu_mutex_destroy(&chr
->chr_write_lock
);
4331 void qemu_chr_free(CharDriverState
*chr
)
4333 if (chr
->chr_free
) {
4336 qemu_chr_free_common(chr
);
4339 void qemu_chr_delete(CharDriverState
*chr
)
4341 QTAILQ_REMOVE(&chardevs
, chr
, next
);
4345 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
4347 ChardevInfoList
*chr_list
= NULL
;
4348 CharDriverState
*chr
;
4350 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
4351 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
4352 info
->value
= g_malloc0(sizeof(*info
->value
));
4353 info
->value
->label
= g_strdup(chr
->label
);
4354 info
->value
->filename
= g_strdup(chr
->filename
);
4355 info
->value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
4357 info
->next
= chr_list
;
4364 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
4366 ChardevBackendInfoList
*backend_list
= NULL
;
4367 CharDriver
*c
= NULL
;
4370 for (i
= backends
; i
; i
= i
->next
) {
4371 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
4373 info
->value
= g_malloc0(sizeof(*info
->value
));
4374 info
->value
->name
= g_strdup(c
->name
);
4376 info
->next
= backend_list
;
4377 backend_list
= info
;
4380 return backend_list
;
4383 CharDriverState
*qemu_chr_find(const char *name
)
4385 CharDriverState
*chr
;
4387 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
4388 if (strcmp(chr
->label
, name
) != 0)
4395 QemuOptsList qemu_chardev_opts
= {
4397 .implied_opt_name
= "backend",
4398 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
4402 .type
= QEMU_OPT_STRING
,
4405 .type
= QEMU_OPT_STRING
,
4408 .type
= QEMU_OPT_STRING
,
4411 .type
= QEMU_OPT_STRING
,
4413 .name
= "localaddr",
4414 .type
= QEMU_OPT_STRING
,
4416 .name
= "localport",
4417 .type
= QEMU_OPT_STRING
,
4420 .type
= QEMU_OPT_NUMBER
,
4423 .type
= QEMU_OPT_BOOL
,
4426 .type
= QEMU_OPT_BOOL
,
4429 .type
= QEMU_OPT_BOOL
,
4432 .type
= QEMU_OPT_BOOL
,
4435 .type
= QEMU_OPT_BOOL
,
4437 .name
= "reconnect",
4438 .type
= QEMU_OPT_NUMBER
,
4441 .type
= QEMU_OPT_BOOL
,
4443 .name
= "tls-creds",
4444 .type
= QEMU_OPT_STRING
,
4447 .type
= QEMU_OPT_NUMBER
,
4450 .type
= QEMU_OPT_NUMBER
,
4453 .type
= QEMU_OPT_NUMBER
,
4456 .type
= QEMU_OPT_NUMBER
,
4459 .type
= QEMU_OPT_BOOL
,
4462 .type
= QEMU_OPT_BOOL
,
4465 .type
= QEMU_OPT_STRING
,
4468 .type
= QEMU_OPT_NUMBER
,
4471 .type
= QEMU_OPT_SIZE
,
4474 .type
= QEMU_OPT_STRING
,
4477 .type
= QEMU_OPT_BOOL
,
4480 .type
= QEMU_OPT_STRING
,
4482 .name
= "logappend",
4483 .type
= QEMU_OPT_BOOL
,
4485 { /* end of list */ }
4491 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4492 ChardevBackend
*backend
,
4497 ChardevFile
*file
= backend
->u
.file
.data
;
4498 ChardevCommon
*common
= qapi_ChardevFile_base(file
);
4504 error_setg(errp
, "input file not supported");
4508 if (file
->has_append
&& file
->append
) {
4509 /* Append to file if it already exists. */
4510 accessmode
= FILE_GENERIC_WRITE
& ~FILE_WRITE_DATA
;
4511 flags
= OPEN_ALWAYS
;
4513 /* Truncate file if it already exists. */
4514 accessmode
= GENERIC_WRITE
;
4515 flags
= CREATE_ALWAYS
;
4518 out
= CreateFile(file
->out
, accessmode
, FILE_SHARE_READ
, NULL
, flags
,
4519 FILE_ATTRIBUTE_NORMAL
, NULL
);
4520 if (out
== INVALID_HANDLE_VALUE
) {
4521 error_setg(errp
, "open %s failed", file
->out
);
4524 return qemu_chr_open_win_file(out
, common
, errp
);
4527 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4528 ChardevBackend
*backend
,
4533 ChardevHostdev
*serial
= backend
->u
.serial
.data
;
4534 ChardevCommon
*common
= qapi_ChardevHostdev_base(serial
);
4535 return qemu_chr_open_win_path(serial
->device
, common
, errp
);
4540 static int qmp_chardev_open_file_source(char *src
, int flags
,
4545 TFR(fd
= qemu_open(src
, flags
, 0666));
4547 error_setg_file_open(errp
, errno
, src
);
4552 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4553 ChardevBackend
*backend
,
4558 ChardevFile
*file
= backend
->u
.file
.data
;
4559 ChardevCommon
*common
= qapi_ChardevFile_base(file
);
4560 int flags
, in
= -1, out
;
4562 flags
= O_WRONLY
| O_CREAT
| O_BINARY
;
4563 if (file
->has_append
&& file
->append
) {
4569 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4576 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4583 return qemu_chr_open_fd(in
, out
, common
, errp
);
4586 #ifdef HAVE_CHARDEV_SERIAL
4587 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4588 ChardevBackend
*backend
,
4593 ChardevHostdev
*serial
= backend
->u
.serial
.data
;
4594 ChardevCommon
*common
= qapi_ChardevHostdev_base(serial
);
4597 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4601 qemu_set_nonblock(fd
);
4602 return qemu_chr_open_tty_fd(fd
, common
, be_opened
, errp
);
4606 #ifdef HAVE_CHARDEV_PARPORT
4607 static CharDriverState
*qmp_chardev_open_parallel(const char *id
,
4608 ChardevBackend
*backend
,
4613 ChardevHostdev
*parallel
= backend
->u
.parallel
.data
;
4614 ChardevCommon
*common
= qapi_ChardevHostdev_base(parallel
);
4617 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4621 return qemu_chr_open_pp_fd(fd
, common
, be_opened
, errp
);
4627 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4629 CharDriverState
*chr
= opaque
;
4630 TCPCharDriver
*s
= chr
->opaque
;
4631 QIOChannelSocket
*sioc
;
4633 s
->reconnect_timer
= 0;
4639 sioc
= qio_channel_socket_new();
4640 tcp_chr_set_client_ioc_name(chr
, sioc
);
4641 qio_channel_socket_connect_async(sioc
, s
->addr
,
4642 qemu_chr_socket_connected
,
4648 static CharDriverState
*qmp_chardev_open_socket(const char *id
,
4649 ChardevBackend
*backend
,
4654 CharDriverState
*chr
;
4656 ChardevSocket
*sock
= backend
->u
.socket
.data
;
4657 SocketAddress
*addr
= sock
->addr
;
4658 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4659 bool is_listen
= sock
->has_server
? sock
->server
: true;
4660 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4661 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4662 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4663 ChardevCommon
*common
= qapi_ChardevSocket_base(sock
);
4664 QIOChannelSocket
*sioc
= NULL
;
4666 chr
= qemu_chr_alloc(common
, errp
);
4670 s
= g_new0(TCPCharDriver
, 1);
4672 s
->is_unix
= addr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
4673 s
->is_listen
= is_listen
;
4674 s
->is_telnet
= is_telnet
;
4675 s
->do_nodelay
= do_nodelay
;
4676 if (sock
->tls_creds
) {
4678 creds
= object_resolve_path_component(
4679 object_get_objects_root(), sock
->tls_creds
);
4681 error_setg(errp
, "No TLS credentials with id '%s'",
4685 s
->tls_creds
= (QCryptoTLSCreds
*)
4686 object_dynamic_cast(creds
,
4687 TYPE_QCRYPTO_TLS_CREDS
);
4688 if (!s
->tls_creds
) {
4689 error_setg(errp
, "Object with id '%s' is not TLS credentials",
4693 object_ref(OBJECT(s
->tls_creds
));
4695 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
4696 error_setg(errp
, "%s",
4697 "Expected TLS credentials for server endpoint");
4701 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
4702 error_setg(errp
, "%s",
4703 "Expected TLS credentials for client endpoint");
4709 s
->addr
= QAPI_CLONE(SocketAddress
, sock
->addr
);
4711 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
4713 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
4717 chr
->chr_wait_connected
= tcp_chr_wait_connected
;
4718 chr
->chr_write
= tcp_chr_write
;
4719 chr
->chr_sync_read
= tcp_chr_sync_read
;
4720 chr
->chr_free
= tcp_chr_free
;
4721 chr
->chr_disconnect
= tcp_chr_disconnect
;
4722 chr
->get_msgfds
= tcp_get_msgfds
;
4723 chr
->set_msgfds
= tcp_set_msgfds
;
4724 chr
->chr_add_client
= tcp_chr_add_client
;
4725 chr
->chr_add_watch
= tcp_chr_add_watch
;
4726 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4727 /* be isn't opened until we get a connection */
4730 chr
->filename
= SocketAddress_to_str("disconnected:",
4731 addr
, is_listen
, is_telnet
);
4735 s
->do_telnetopt
= 1;
4737 } else if (reconnect
> 0) {
4738 s
->reconnect_time
= reconnect
;
4741 if (s
->reconnect_time
) {
4742 sioc
= qio_channel_socket_new();
4743 tcp_chr_set_client_ioc_name(chr
, sioc
);
4744 qio_channel_socket_connect_async(sioc
, s
->addr
,
4745 qemu_chr_socket_connected
,
4750 sioc
= qio_channel_socket_new();
4752 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
4753 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
4756 if (qio_channel_socket_listen_sync(sioc
, s
->addr
, errp
) < 0) {
4759 s
->listen_ioc
= sioc
;
4760 if (is_waitconnect
&&
4761 qemu_chr_wait_connected(chr
, errp
) < 0) {
4765 s
->listen_tag
= qio_channel_add_watch(
4766 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
,
4767 tcp_chr_accept
, chr
, NULL
);
4769 } else if (qemu_chr_wait_connected(chr
, errp
) < 0) {
4778 object_unref(OBJECT(sioc
));
4781 object_unref(OBJECT(s
->tls_creds
));
4784 qemu_chr_free_common(chr
);
4788 static CharDriverState
*qmp_chardev_open_udp(const char *id
,
4789 ChardevBackend
*backend
,
4794 ChardevUdp
*udp
= backend
->u
.udp
.data
;
4795 ChardevCommon
*common
= qapi_ChardevUdp_base(udp
);
4796 QIOChannelSocket
*sioc
= qio_channel_socket_new();
4798 CharDriverState
*chr
;
4800 if (qio_channel_socket_dgram_sync(sioc
,
4801 udp
->local
, udp
->remote
,
4803 object_unref(OBJECT(sioc
));
4806 chr
= qemu_chr_open_udp(sioc
, common
, be_opened
, errp
);
4808 name
= g_strdup_printf("chardev-udp-%s", chr
->label
);
4809 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
4816 bool qemu_chr_has_feature(CharDriverState
*chr
,
4817 CharDriverFeature feature
)
4819 return test_bit(feature
, chr
->features
);
4822 void qemu_chr_set_feature(CharDriverState
*chr
,
4823 CharDriverFeature feature
)
4825 return set_bit(feature
, chr
->features
);
4828 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4831 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4832 CharDriverState
*chr
= NULL
;
4833 Error
*local_err
= NULL
;
4836 bool be_opened
= true;
4838 chr
= qemu_chr_find(id
);
4840 error_setg(errp
, "Chardev '%s' already exists", id
);
4844 for (i
= backends
; i
; i
= i
->next
) {
4847 if (cd
->kind
== backend
->type
) {
4848 chr
= cd
->create(id
, backend
, ret
, &be_opened
, &local_err
);
4850 error_propagate(errp
, local_err
);
4859 error_setg(errp
, "chardev backend not available");
4863 chr
->label
= g_strdup(id
);
4864 if (!chr
->filename
) {
4865 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->type
]);
4868 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4870 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4878 void qmp_chardev_remove(const char *id
, Error
**errp
)
4880 CharDriverState
*chr
;
4882 chr
= qemu_chr_find(id
);
4884 error_setg(errp
, "Chardev '%s' not found", id
);
4887 if (qemu_chr_is_busy(chr
)) {
4888 error_setg(errp
, "Chardev '%s' is busy", id
);
4893 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
4896 qemu_chr_delete(chr
);
4899 void qemu_chr_cleanup(void)
4901 CharDriverState
*chr
, *tmp
;
4903 QTAILQ_FOREACH_SAFE(chr
, &chardevs
, next
, tmp
) {
4904 qemu_chr_delete(chr
);
4908 static void register_types(void)
4910 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
,
4911 qemu_chr_open_null
);
4912 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4913 qemu_chr_parse_socket
, qmp_chardev_open_socket
);
4914 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
,
4915 qmp_chardev_open_udp
);
4916 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4917 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4918 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4919 qemu_chr_parse_file_out
, qmp_chardev_open_file
);
4920 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4921 qemu_chr_parse_stdio
, qemu_chr_open_stdio
);
4922 #if defined HAVE_CHARDEV_SERIAL
4923 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4924 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4925 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4926 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4928 #ifdef HAVE_CHARDEV_PARPORT
4929 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4930 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4931 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4932 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4934 #ifdef HAVE_CHARDEV_PTY
4935 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
,
4939 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
,
4940 qemu_chr_open_win_con
);
4942 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4943 qemu_chr_parse_pipe
, qemu_chr_open_pipe
);
4944 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
,
4946 /* Bug-compatibility: */
4947 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4948 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4949 /* this must be done after machine init, since we register FEs with muxes
4950 * as part of realize functions like serial_isa_realizefn when -nographic
4953 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4956 type_init(register_types
);