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 /***********************************************************/
93 /* Socket address helpers */
95 static char *SocketAddress_to_str(const char *prefix
, SocketAddress
*addr
,
96 bool is_listen
, bool is_telnet
)
99 case SOCKET_ADDRESS_KIND_INET
:
100 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
101 is_telnet
? "telnet" : "tcp",
102 addr
->u
.inet
.data
->host
,
103 addr
->u
.inet
.data
->port
,
104 is_listen
? ",server" : "");
106 case SOCKET_ADDRESS_KIND_UNIX
:
107 return g_strdup_printf("%sunix:%s%s", prefix
,
108 addr
->u
.q_unix
.data
->path
,
109 is_listen
? ",server" : "");
111 case SOCKET_ADDRESS_KIND_FD
:
112 return g_strdup_printf("%sfd:%s%s", prefix
, addr
->u
.fd
.data
->str
,
113 is_listen
? ",server" : "");
120 static char *sockaddr_to_str(struct sockaddr_storage
*ss
, socklen_t ss_len
,
121 struct sockaddr_storage
*ps
, socklen_t ps_len
,
122 bool is_listen
, bool is_telnet
)
124 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
125 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
126 const char *left
= "", *right
= "";
128 switch (ss
->ss_family
) {
131 return g_strdup_printf("unix:%s%s",
132 ((struct sockaddr_un
*)(ss
))->sun_path
,
133 is_listen
? ",server" : "");
140 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
141 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
142 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
143 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
144 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
145 is_telnet
? "telnet" : "tcp",
146 left
, shost
, right
, sserv
,
147 is_listen
? ",server" : "",
148 left
, phost
, right
, pserv
);
151 return g_strdup_printf("unknown");
155 /***********************************************************/
156 /* character device */
158 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
159 QTAILQ_HEAD_INITIALIZER(chardevs
);
161 static void qemu_chr_free_common(CharDriverState
*chr
);
163 CharDriverState
*qemu_chr_alloc(ChardevCommon
*backend
, Error
**errp
)
165 CharDriverState
*chr
= g_malloc0(sizeof(CharDriverState
));
166 qemu_mutex_init(&chr
->chr_write_lock
);
169 if (backend
->has_logfile
) {
170 int flags
= O_WRONLY
| O_CREAT
;
171 if (backend
->has_logappend
&&
172 backend
->logappend
) {
177 chr
->logfd
= qemu_open(backend
->logfile
, flags
, 0666);
178 if (chr
->logfd
< 0) {
179 error_setg_errno(errp
, errno
,
180 "Unable to open logfile %s",
192 void qemu_chr_be_event(CharDriverState
*s
, int event
)
194 /* Keep track if the char device is open */
196 case CHR_EVENT_OPENED
:
199 case CHR_EVENT_CLOSED
:
206 s
->chr_event(s
->handler_opaque
, event
);
209 void qemu_chr_be_generic_open(CharDriverState
*s
)
211 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
215 /* Not reporting errors from writing to logfile, as logs are
216 * defined to be "best effort" only */
217 static void qemu_chr_fe_write_log(CharDriverState
*s
,
218 const uint8_t *buf
, size_t len
)
229 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
230 if (ret
== -1 && errno
== EAGAIN
) {
242 static int qemu_chr_fe_write_buffer(CharDriverState
*s
, const uint8_t *buf
, int len
, int *offset
)
247 qemu_mutex_lock(&s
->chr_write_lock
);
248 while (*offset
< len
) {
250 res
= s
->chr_write(s
, buf
+ *offset
, len
- *offset
);
251 if (res
< 0 && errno
== EAGAIN
) {
263 qemu_chr_fe_write_log(s
, buf
, *offset
);
265 qemu_mutex_unlock(&s
->chr_write_lock
);
270 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
274 if (s
->replay
&& replay_mode
== REPLAY_MODE_PLAY
) {
276 replay_char_write_event_load(&ret
, &offset
);
277 assert(offset
<= len
);
278 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
282 qemu_mutex_lock(&s
->chr_write_lock
);
283 ret
= s
->chr_write(s
, buf
, len
);
286 qemu_chr_fe_write_log(s
, buf
, ret
);
289 qemu_mutex_unlock(&s
->chr_write_lock
);
291 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
292 replay_char_write_event_save(ret
, ret
< 0 ? 0 : ret
);
298 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
303 if (s
->replay
&& replay_mode
== REPLAY_MODE_PLAY
) {
304 replay_char_write_event_load(&res
, &offset
);
305 assert(offset
<= len
);
306 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
310 res
= qemu_chr_fe_write_buffer(s
, buf
, len
, &offset
);
312 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
313 replay_char_write_event_save(res
, offset
);
322 int qemu_chr_fe_read_all(CharDriverState
*s
, uint8_t *buf
, int len
)
324 int offset
= 0, counter
= 10;
327 if (!s
->chr_sync_read
) {
331 if (s
->replay
&& replay_mode
== REPLAY_MODE_PLAY
) {
332 return replay_char_read_all_load(buf
);
335 while (offset
< len
) {
337 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
338 if (res
== -1 && errno
== EAGAIN
) {
348 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
349 replay_char_read_all_save_error(res
);
361 if (s
->replay
&& replay_mode
== REPLAY_MODE_RECORD
) {
362 replay_char_read_all_save_buf(buf
, offset
);
367 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
370 if (!s
->chr_ioctl
|| s
->replay
) {
373 res
= s
->chr_ioctl(s
, cmd
, arg
);
379 int qemu_chr_be_can_write(CharDriverState
*s
)
381 if (!s
->chr_can_read
)
383 return s
->chr_can_read(s
->handler_opaque
);
386 void qemu_chr_be_write_impl(CharDriverState
*s
, uint8_t *buf
, int len
)
389 s
->chr_read(s
->handler_opaque
, buf
, len
);
393 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
396 if (replay_mode
== REPLAY_MODE_PLAY
) {
399 replay_chr_be_write(s
, buf
, len
);
401 qemu_chr_be_write_impl(s
, buf
, len
);
405 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
408 int res
= (qemu_chr_fe_get_msgfds(s
, &fd
, 1) == 1) ? fd
: -1;
411 "Replay: get msgfd is not supported for serial devices yet\n");
417 int qemu_chr_fe_get_msgfds(CharDriverState
*s
, int *fds
, int len
)
419 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
422 int qemu_chr_fe_set_msgfds(CharDriverState
*s
, int *fds
, int num
)
424 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
427 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
429 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
432 void qemu_chr_accept_input(CharDriverState
*s
)
434 if (s
->chr_accept_input
)
435 s
->chr_accept_input(s
);
439 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
441 char buf
[READ_BUF_LEN
];
444 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
445 /* XXX this blocks entire thread. Rewrite to use
446 * qemu_chr_fe_write and background I/O callbacks */
447 qemu_chr_fe_write_all(s
, (uint8_t *)buf
, strlen(buf
));
451 static void remove_fd_in_watch(CharDriverState
*chr
);
453 void qemu_chr_add_handlers_full(CharDriverState
*s
,
454 IOCanReadHandler
*fd_can_read
,
455 IOReadHandler
*fd_read
,
456 IOEventHandler
*fd_event
,
458 GMainContext
*context
)
462 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
464 remove_fd_in_watch(s
);
468 s
->chr_can_read
= fd_can_read
;
469 s
->chr_read
= fd_read
;
470 s
->chr_event
= fd_event
;
471 s
->handler_opaque
= opaque
;
472 if (s
->chr_update_read_handler
) {
473 s
->chr_update_read_handler(s
, context
);
476 if (!s
->explicit_fe_open
) {
477 qemu_chr_fe_set_open(s
, fe_open
);
480 /* We're connecting to an already opened device, so let's make sure we
481 also get the open event */
482 if (fe_open
&& s
->be_open
) {
483 qemu_chr_be_generic_open(s
);
487 void qemu_chr_add_handlers(CharDriverState
*s
,
488 IOCanReadHandler
*fd_can_read
,
489 IOReadHandler
*fd_read
,
490 IOEventHandler
*fd_event
,
493 qemu_chr_add_handlers_full(s
, fd_can_read
, fd_read
,
494 fd_event
, opaque
, NULL
);
497 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
502 static CharDriverState
*qemu_chr_open_null(const char *id
,
503 ChardevBackend
*backend
,
507 CharDriverState
*chr
;
508 ChardevCommon
*common
= backend
->u
.null
.data
;
510 chr
= qemu_chr_alloc(common
, errp
);
514 chr
->chr_write
= null_chr_write
;
515 chr
->explicit_be_open
= true;
519 /* MUX driver for serial I/O splitting */
521 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
522 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
524 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
525 IOReadHandler
*chr_read
[MAX_MUX
];
526 IOEventHandler
*chr_event
[MAX_MUX
];
527 void *ext_opaque
[MAX_MUX
];
528 CharDriverState
*drv
;
533 /* Intermediate input buffer allows to catch escape sequences even if the
534 currently active device is not accepting any input - but only until it
536 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
541 /* Protected by the CharDriverState chr_write_lock. */
543 int64_t timestamps_start
;
547 /* Called with chr_write_lock held. */
548 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
550 MuxDriver
*d
= chr
->opaque
;
552 if (!d
->timestamps
) {
553 ret
= qemu_chr_fe_write(d
->drv
, buf
, len
);
558 for (i
= 0; i
< len
; i
++) {
564 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
565 if (d
->timestamps_start
== -1)
566 d
->timestamps_start
= ti
;
567 ti
-= d
->timestamps_start
;
569 snprintf(buf1
, sizeof(buf1
),
570 "[%02d:%02d:%02d.%03d] ",
575 /* XXX this blocks entire thread. Rewrite to use
576 * qemu_chr_fe_write and background I/O callbacks */
577 qemu_chr_fe_write_all(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
580 ret
+= qemu_chr_fe_write(d
->drv
, buf
+i
, 1);
581 if (buf
[i
] == '\n') {
589 static const char * const mux_help
[] = {
590 "% h print this help\n\r",
591 "% x exit emulator\n\r",
592 "% s save disk data back to file (if -snapshot)\n\r",
593 "% t toggle console timestamps\n\r",
594 "% b send break (magic sysrq)\n\r",
595 "% c switch between console and monitor\n\r",
600 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
601 static void mux_print_help(CharDriverState
*chr
)
604 char ebuf
[15] = "Escape-Char";
605 char cbuf
[50] = "\n\r";
607 if (term_escape_char
> 0 && term_escape_char
< 26) {
608 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
609 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
611 snprintf(cbuf
, sizeof(cbuf
),
612 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
615 /* XXX this blocks entire thread. Rewrite to use
616 * qemu_chr_fe_write and background I/O callbacks */
617 qemu_chr_fe_write_all(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
618 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
619 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
620 if (mux_help
[i
][j
] == '%')
621 qemu_chr_fe_write_all(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
623 qemu_chr_fe_write_all(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
628 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
630 if (d
->chr_event
[mux_nr
])
631 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
634 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
636 if (d
->term_got_escape
) {
637 d
->term_got_escape
= 0;
638 if (ch
== term_escape_char
)
647 const char *term
= "QEMU: Terminated\n\r";
648 qemu_chr_fe_write_all(chr
, (uint8_t *)term
, strlen(term
));
656 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
659 /* Switch to the next registered device */
660 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
662 if (d
->focus
>= d
->mux_cnt
)
664 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
667 d
->timestamps
= !d
->timestamps
;
668 d
->timestamps_start
= -1;
672 } else if (ch
== term_escape_char
) {
673 d
->term_got_escape
= 1;
681 static void mux_chr_accept_input(CharDriverState
*chr
)
683 MuxDriver
*d
= chr
->opaque
;
686 while (d
->prod
[m
] != d
->cons
[m
] &&
687 d
->chr_can_read
[m
] &&
688 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
689 d
->chr_read
[m
](d
->ext_opaque
[m
],
690 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
694 static int mux_chr_can_read(void *opaque
)
696 CharDriverState
*chr
= opaque
;
697 MuxDriver
*d
= chr
->opaque
;
700 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
702 if (d
->chr_can_read
[m
])
703 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
707 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
709 CharDriverState
*chr
= opaque
;
710 MuxDriver
*d
= chr
->opaque
;
714 mux_chr_accept_input (opaque
);
716 for(i
= 0; i
< size
; i
++)
717 if (mux_proc_byte(chr
, d
, buf
[i
])) {
718 if (d
->prod
[m
] == d
->cons
[m
] &&
719 d
->chr_can_read
[m
] &&
720 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
721 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
723 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
727 static void mux_chr_event(void *opaque
, int event
)
729 CharDriverState
*chr
= opaque
;
730 MuxDriver
*d
= chr
->opaque
;
733 /* Send the event to all registered listeners */
734 for (i
= 0; i
< d
->mux_cnt
; i
++)
735 mux_chr_send_event(d
, i
, event
);
738 static void mux_chr_update_read_handler(CharDriverState
*chr
,
739 GMainContext
*context
)
741 MuxDriver
*d
= chr
->opaque
;
744 if (d
->mux_cnt
>= MAX_MUX
) {
745 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
749 if (chr
->mux_idx
== -1) {
750 chr
->mux_idx
= d
->mux_cnt
++;
754 d
->ext_opaque
[idx
] = chr
->handler_opaque
;
755 d
->chr_can_read
[idx
] = chr
->chr_can_read
;
756 d
->chr_read
[idx
] = chr
->chr_read
;
757 d
->chr_event
[idx
] = chr
->chr_event
;
759 /* Fix up the real driver with mux routines */
760 if (d
->mux_cnt
== 1) {
761 qemu_chr_add_handlers_full(d
->drv
, mux_chr_can_read
,
766 if (d
->focus
!= -1) {
767 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
770 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
773 static bool muxes_realized
;
776 * Called after processing of default and command-line-specified
777 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
778 * to a mux chardev. This is done here to ensure that
779 * output/prompts/banners are only displayed for the FE that has
780 * focus when initial command-line processing/machine init is
783 * After this point, any new FE attached to any new or existing
784 * mux will receive CHR_EVENT_OPENED notifications for the BE
787 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
789 CharDriverState
*chr
;
791 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
793 MuxDriver
*d
= chr
->opaque
;
796 /* send OPENED to all already-attached FEs */
797 for (i
= 0; i
< d
->mux_cnt
; i
++) {
798 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
800 /* mark mux as OPENED so any new FEs will immediately receive
803 qemu_chr_be_generic_open(chr
);
806 muxes_realized
= true;
809 static Notifier muxes_realize_notify
= {
810 .notify
= muxes_realize_done
,
813 static GSource
*mux_chr_add_watch(CharDriverState
*s
, GIOCondition cond
)
815 MuxDriver
*d
= s
->opaque
;
816 return d
->drv
->chr_add_watch(d
->drv
, cond
);
819 static void mux_chr_close(struct CharDriverState
*chr
)
821 MuxDriver
*d
= chr
->opaque
;
826 static CharDriverState
*qemu_chr_open_mux(const char *id
,
827 ChardevBackend
*backend
,
828 ChardevReturn
*ret
, Error
**errp
)
830 ChardevMux
*mux
= backend
->u
.mux
.data
;
831 CharDriverState
*chr
, *drv
;
833 ChardevCommon
*common
= qapi_ChardevMux_base(mux
);
835 drv
= qemu_chr_find(mux
->chardev
);
837 error_setg(errp
, "mux: base chardev %s not found", mux
->chardev
);
841 chr
= qemu_chr_alloc(common
, errp
);
845 d
= g_new0(MuxDriver
, 1);
850 chr
->chr_close
= mux_chr_close
;
851 chr
->chr_write
= mux_chr_write
;
852 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
853 chr
->chr_accept_input
= mux_chr_accept_input
;
854 /* Frontend guest-open / -close notification is not support with muxes */
855 chr
->chr_set_fe_open
= NULL
;
856 if (drv
->chr_add_watch
) {
857 chr
->chr_add_watch
= mux_chr_add_watch
;
859 /* only default to opened state if we've realized the initial
862 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
869 typedef struct IOWatchPoll
876 IOCanReadHandler
*fd_can_read
;
879 GMainContext
*context
;
882 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
884 return container_of(source
, IOWatchPoll
, parent
);
887 static gboolean
io_watch_poll_prepare(GSource
*source
,
890 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
891 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
892 bool was_active
= iwp
->src
!= NULL
;
893 if (was_active
== now_active
) {
898 iwp
->src
= qio_channel_create_watch(
899 iwp
->ioc
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
);
900 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
901 g_source_attach(iwp
->src
, iwp
->context
);
903 g_source_destroy(iwp
->src
);
904 g_source_unref(iwp
->src
);
910 static gboolean
io_watch_poll_check(GSource
*source
)
915 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
921 static void io_watch_poll_finalize(GSource
*source
)
923 /* Due to a glib bug, removing the last reference to a source
924 * inside a finalize callback causes recursive locking (and a
925 * deadlock). This is not a problem inside other callbacks,
926 * including dispatch callbacks, so we call io_remove_watch_poll
927 * to remove this source. At this point, iwp->src must
928 * be NULL, or we would leak it.
930 * This would be solved much more elegantly by child sources,
931 * but we support older glib versions that do not have them.
933 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
934 assert(iwp
->src
== NULL
);
937 static GSourceFuncs io_watch_poll_funcs
= {
938 .prepare
= io_watch_poll_prepare
,
939 .check
= io_watch_poll_check
,
940 .dispatch
= io_watch_poll_dispatch
,
941 .finalize
= io_watch_poll_finalize
,
944 /* Can only be used for read */
945 static guint
io_add_watch_poll(QIOChannel
*ioc
,
946 IOCanReadHandler
*fd_can_read
,
947 QIOChannelFunc fd_read
,
949 GMainContext
*context
)
954 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
,
955 sizeof(IOWatchPoll
));
956 iwp
->fd_can_read
= fd_can_read
;
957 iwp
->opaque
= user_data
;
959 iwp
->fd_read
= (GSourceFunc
) fd_read
;
961 iwp
->context
= context
;
963 tag
= g_source_attach(&iwp
->parent
, context
);
964 g_source_unref(&iwp
->parent
);
968 static void io_remove_watch_poll(guint tag
)
973 g_return_if_fail (tag
> 0);
975 source
= g_main_context_find_source_by_id(NULL
, tag
);
976 g_return_if_fail (source
!= NULL
);
978 iwp
= io_watch_poll_from_source(source
);
980 g_source_destroy(iwp
->src
);
981 g_source_unref(iwp
->src
);
984 g_source_destroy(&iwp
->parent
);
987 static void remove_fd_in_watch(CharDriverState
*chr
)
989 if (chr
->fd_in_tag
) {
990 io_remove_watch_poll(chr
->fd_in_tag
);
996 static int io_channel_send_full(QIOChannel
*ioc
,
997 const void *buf
, size_t len
,
998 int *fds
, size_t nfds
)
1002 while (offset
< len
) {
1004 struct iovec iov
= { .iov_base
= (char *)buf
+ offset
,
1005 .iov_len
= len
- offset
};
1007 ret
= qio_channel_writev_full(
1010 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
1017 } else if (ret
< 0) {
1030 static int io_channel_send(QIOChannel
*ioc
, const void *buf
, size_t len
)
1032 return io_channel_send_full(ioc
, buf
, len
, NULL
, 0);
1036 typedef struct FDCharDriver
{
1037 CharDriverState
*chr
;
1038 QIOChannel
*ioc_in
, *ioc_out
;
1042 /* Called with chr_write_lock held. */
1043 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1045 FDCharDriver
*s
= chr
->opaque
;
1047 return io_channel_send(s
->ioc_out
, buf
, len
);
1050 static gboolean
fd_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1052 CharDriverState
*chr
= opaque
;
1053 FDCharDriver
*s
= chr
->opaque
;
1055 uint8_t buf
[READ_BUF_LEN
];
1059 if (len
> s
->max_size
) {
1066 ret
= qio_channel_read(
1067 chan
, (gchar
*)buf
, len
, NULL
);
1069 remove_fd_in_watch(chr
);
1070 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1074 qemu_chr_be_write(chr
, buf
, ret
);
1080 static int fd_chr_read_poll(void *opaque
)
1082 CharDriverState
*chr
= opaque
;
1083 FDCharDriver
*s
= chr
->opaque
;
1085 s
->max_size
= qemu_chr_be_can_write(chr
);
1089 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1091 FDCharDriver
*s
= chr
->opaque
;
1092 return qio_channel_create_watch(s
->ioc_out
, cond
);
1095 static void fd_chr_update_read_handler(CharDriverState
*chr
,
1096 GMainContext
*context
)
1098 FDCharDriver
*s
= chr
->opaque
;
1100 remove_fd_in_watch(chr
);
1102 chr
->fd_in_tag
= io_add_watch_poll(s
->ioc_in
,
1109 static void fd_chr_close(struct CharDriverState
*chr
)
1111 FDCharDriver
*s
= chr
->opaque
;
1113 remove_fd_in_watch(chr
);
1115 object_unref(OBJECT(s
->ioc_in
));
1118 object_unref(OBJECT(s
->ioc_out
));
1122 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1125 /* open a character device to a unix fd */
1126 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
,
1127 ChardevCommon
*backend
, Error
**errp
)
1129 CharDriverState
*chr
;
1132 chr
= qemu_chr_alloc(backend
, errp
);
1136 s
= g_new0(FDCharDriver
, 1);
1137 s
->ioc_in
= QIO_CHANNEL(qio_channel_file_new_fd(fd_in
));
1138 s
->ioc_out
= QIO_CHANNEL(qio_channel_file_new_fd(fd_out
));
1139 qemu_set_nonblock(fd_out
);
1142 chr
->chr_add_watch
= fd_chr_add_watch
;
1143 chr
->chr_write
= fd_chr_write
;
1144 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1145 chr
->chr_close
= fd_chr_close
;
1150 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
1151 ChardevBackend
*backend
,
1155 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
1159 const char *filename
= opts
->device
;
1160 ChardevCommon
*common
= qapi_ChardevHostdev_base(opts
);
1163 filename_in
= g_strdup_printf("%s.in", filename
);
1164 filename_out
= g_strdup_printf("%s.out", filename
);
1165 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1166 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1167 g_free(filename_in
);
1168 g_free(filename_out
);
1169 if (fd_in
< 0 || fd_out
< 0) {
1174 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1176 error_setg_file_open(errp
, errno
, filename
);
1180 return qemu_chr_open_fd(fd_in
, fd_out
, common
, errp
);
1183 /* init terminal so that we can grab keys */
1184 static struct termios oldtty
;
1185 static int old_fd0_flags
;
1186 static bool stdio_in_use
;
1187 static bool stdio_allow_signal
;
1188 static bool stdio_echo_state
;
1190 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
);
1192 static void term_exit(void)
1194 tcsetattr (0, TCSANOW
, &oldtty
);
1195 fcntl(0, F_SETFL
, old_fd0_flags
);
1198 static void term_stdio_handler(int sig
)
1200 /* restore echo after resume from suspend. */
1201 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
1204 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1208 stdio_echo_state
= echo
;
1211 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1212 |INLCR
|IGNCR
|ICRNL
|IXON
);
1213 tty
.c_oflag
|= OPOST
;
1214 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1215 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1218 tty
.c_cc
[VTIME
] = 0;
1220 if (!stdio_allow_signal
)
1221 tty
.c_lflag
&= ~ISIG
;
1223 tcsetattr (0, TCSANOW
, &tty
);
1226 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1232 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
1233 ChardevBackend
*backend
,
1237 ChardevStdio
*opts
= backend
->u
.stdio
.data
;
1238 CharDriverState
*chr
;
1239 struct sigaction act
;
1240 ChardevCommon
*common
= qapi_ChardevStdio_base(opts
);
1242 if (is_daemonized()) {
1243 error_setg(errp
, "cannot use stdio with -daemonize");
1248 error_setg(errp
, "cannot use stdio by multiple character devices");
1252 stdio_in_use
= true;
1253 old_fd0_flags
= fcntl(0, F_GETFL
);
1254 tcgetattr(0, &oldtty
);
1255 qemu_set_nonblock(0);
1258 memset(&act
, 0, sizeof(act
));
1259 act
.sa_handler
= term_stdio_handler
;
1260 sigaction(SIGCONT
, &act
, NULL
);
1262 chr
= qemu_chr_open_fd(0, 1, common
, errp
);
1266 chr
->chr_close
= qemu_chr_close_stdio
;
1267 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1268 if (opts
->has_signal
) {
1269 stdio_allow_signal
= opts
->signal
;
1271 qemu_chr_fe_set_echo(chr
, false);
1276 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1277 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1278 || defined(__GLIBC__)
1280 #define HAVE_CHARDEV_SERIAL 1
1281 #define HAVE_CHARDEV_PTY 1
1287 /* Protected by the CharDriverState chr_write_lock. */
1293 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1294 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1296 static gboolean
pty_chr_timer(gpointer opaque
)
1298 struct CharDriverState
*chr
= opaque
;
1299 PtyCharDriver
*s
= chr
->opaque
;
1301 qemu_mutex_lock(&chr
->chr_write_lock
);
1304 if (!s
->connected
) {
1306 pty_chr_update_read_handler_locked(chr
);
1308 qemu_mutex_unlock(&chr
->chr_write_lock
);
1312 /* Called with chr_write_lock held. */
1313 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1315 PtyCharDriver
*s
= chr
->opaque
;
1318 g_source_remove(s
->timer_tag
);
1323 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1325 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1329 /* Called with chr_write_lock held. */
1330 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1332 PtyCharDriver
*s
= chr
->opaque
;
1335 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(s
->ioc
);
1338 pfd
.events
= G_IO_OUT
;
1341 rc
= g_poll(&pfd
, 1, 0);
1342 } while (rc
== -1 && errno
== EINTR
);
1345 if (pfd
.revents
& G_IO_HUP
) {
1346 pty_chr_state(chr
, 0);
1348 pty_chr_state(chr
, 1);
1352 static void pty_chr_update_read_handler(CharDriverState
*chr
,
1353 GMainContext
*context
)
1355 qemu_mutex_lock(&chr
->chr_write_lock
);
1356 pty_chr_update_read_handler_locked(chr
);
1357 qemu_mutex_unlock(&chr
->chr_write_lock
);
1360 /* Called with chr_write_lock held. */
1361 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1363 PtyCharDriver
*s
= chr
->opaque
;
1365 if (!s
->connected
) {
1366 /* guest sends data, check for (re-)connect */
1367 pty_chr_update_read_handler_locked(chr
);
1368 if (!s
->connected
) {
1372 return io_channel_send(s
->ioc
, buf
, len
);
1375 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1377 PtyCharDriver
*s
= chr
->opaque
;
1378 if (!s
->connected
) {
1381 return qio_channel_create_watch(s
->ioc
, cond
);
1384 static int pty_chr_read_poll(void *opaque
)
1386 CharDriverState
*chr
= opaque
;
1387 PtyCharDriver
*s
= chr
->opaque
;
1389 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1390 return s
->read_bytes
;
1393 static gboolean
pty_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1395 CharDriverState
*chr
= opaque
;
1396 PtyCharDriver
*s
= chr
->opaque
;
1398 uint8_t buf
[READ_BUF_LEN
];
1402 if (len
> s
->read_bytes
)
1403 len
= s
->read_bytes
;
1407 ret
= qio_channel_read(s
->ioc
, (char *)buf
, len
, NULL
);
1409 pty_chr_state(chr
, 0);
1412 pty_chr_state(chr
, 1);
1413 qemu_chr_be_write(chr
, buf
, ret
);
1418 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1420 CharDriverState
*chr
= opaque
;
1421 PtyCharDriver
*s
= chr
->opaque
;
1424 qemu_chr_be_generic_open(chr
);
1428 /* Called with chr_write_lock held. */
1429 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1431 PtyCharDriver
*s
= chr
->opaque
;
1435 g_source_remove(s
->open_tag
);
1438 remove_fd_in_watch(chr
);
1440 /* (re-)connect poll interval for idle guests: once per second.
1441 * We check more frequently in case the guests sends data to
1442 * the virtual device linked to our pty. */
1443 pty_chr_rearm_timer(chr
, 1000);
1446 g_source_remove(s
->timer_tag
);
1449 if (!s
->connected
) {
1450 g_assert(s
->open_tag
== 0);
1452 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1454 if (!chr
->fd_in_tag
) {
1455 chr
->fd_in_tag
= io_add_watch_poll(s
->ioc
,
1463 static void pty_chr_close(struct CharDriverState
*chr
)
1465 PtyCharDriver
*s
= chr
->opaque
;
1467 qemu_mutex_lock(&chr
->chr_write_lock
);
1468 pty_chr_state(chr
, 0);
1469 object_unref(OBJECT(s
->ioc
));
1471 g_source_remove(s
->timer_tag
);
1474 qemu_mutex_unlock(&chr
->chr_write_lock
);
1476 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1479 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1480 ChardevBackend
*backend
,
1484 CharDriverState
*chr
;
1486 int master_fd
, slave_fd
;
1487 char pty_name
[PATH_MAX
];
1488 ChardevCommon
*common
= backend
->u
.pty
.data
;
1490 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1491 if (master_fd
< 0) {
1492 error_setg_errno(errp
, errno
, "Failed to create PTY");
1497 qemu_set_nonblock(master_fd
);
1499 chr
= qemu_chr_alloc(common
, errp
);
1505 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1506 ret
->pty
= g_strdup(pty_name
);
1507 ret
->has_pty
= true;
1509 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1512 s
= g_new0(PtyCharDriver
, 1);
1514 chr
->chr_write
= pty_chr_write
;
1515 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1516 chr
->chr_close
= pty_chr_close
;
1517 chr
->chr_add_watch
= pty_chr_add_watch
;
1518 chr
->explicit_be_open
= true;
1520 s
->ioc
= QIO_CHANNEL(qio_channel_file_new_fd(master_fd
));
1526 static void tty_serial_init(int fd
, int speed
,
1527 int parity
, int data_bits
, int stop_bits
)
1533 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1534 speed
, parity
, data_bits
, stop_bits
);
1536 tcgetattr (fd
, &tty
);
1538 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1539 speed
= speed
* 10 / 11;
1556 /* Non-Posix values follow. They may be unsupported on some systems. */
1558 check_speed(115200);
1560 check_speed(230400);
1563 check_speed(460800);
1566 check_speed(500000);
1569 check_speed(576000);
1572 check_speed(921600);
1575 check_speed(1000000);
1578 check_speed(1152000);
1581 check_speed(1500000);
1584 check_speed(2000000);
1587 check_speed(2500000);
1590 check_speed(3000000);
1593 check_speed(3500000);
1596 check_speed(4000000);
1601 cfsetispeed(&tty
, spd
);
1602 cfsetospeed(&tty
, spd
);
1604 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1605 |INLCR
|IGNCR
|ICRNL
|IXON
);
1606 tty
.c_oflag
|= OPOST
;
1607 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1608 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1629 tty
.c_cflag
|= PARENB
;
1632 tty
.c_cflag
|= PARENB
| PARODD
;
1636 tty
.c_cflag
|= CSTOPB
;
1638 tcsetattr (fd
, TCSANOW
, &tty
);
1641 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1643 FDCharDriver
*s
= chr
->opaque
;
1644 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(s
->ioc_in
);
1647 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1649 QEMUSerialSetParams
*ssp
= arg
;
1650 tty_serial_init(fioc
->fd
,
1651 ssp
->speed
, ssp
->parity
,
1652 ssp
->data_bits
, ssp
->stop_bits
);
1655 case CHR_IOCTL_SERIAL_SET_BREAK
:
1657 int enable
= *(int *)arg
;
1659 tcsendbreak(fioc
->fd
, 1);
1663 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1666 int *targ
= (int *)arg
;
1667 ioctl(fioc
->fd
, TIOCMGET
, &sarg
);
1669 if (sarg
& TIOCM_CTS
)
1670 *targ
|= CHR_TIOCM_CTS
;
1671 if (sarg
& TIOCM_CAR
)
1672 *targ
|= CHR_TIOCM_CAR
;
1673 if (sarg
& TIOCM_DSR
)
1674 *targ
|= CHR_TIOCM_DSR
;
1675 if (sarg
& TIOCM_RI
)
1676 *targ
|= CHR_TIOCM_RI
;
1677 if (sarg
& TIOCM_DTR
)
1678 *targ
|= CHR_TIOCM_DTR
;
1679 if (sarg
& TIOCM_RTS
)
1680 *targ
|= CHR_TIOCM_RTS
;
1683 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1685 int sarg
= *(int *)arg
;
1687 ioctl(fioc
->fd
, TIOCMGET
, &targ
);
1688 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1689 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1690 if (sarg
& CHR_TIOCM_CTS
)
1692 if (sarg
& CHR_TIOCM_CAR
)
1694 if (sarg
& CHR_TIOCM_DSR
)
1696 if (sarg
& CHR_TIOCM_RI
)
1698 if (sarg
& CHR_TIOCM_DTR
)
1700 if (sarg
& CHR_TIOCM_RTS
)
1702 ioctl(fioc
->fd
, TIOCMSET
, &targ
);
1711 static void qemu_chr_close_tty(CharDriverState
*chr
)
1716 static CharDriverState
*qemu_chr_open_tty_fd(int fd
,
1717 ChardevCommon
*backend
,
1720 CharDriverState
*chr
;
1722 tty_serial_init(fd
, 115200, 'N', 8, 1);
1723 chr
= qemu_chr_open_fd(fd
, fd
, backend
, errp
);
1727 chr
->chr_ioctl
= tty_serial_ioctl
;
1728 chr
->chr_close
= qemu_chr_close_tty
;
1731 #endif /* __linux__ || __sun__ */
1733 #if defined(__linux__)
1735 #define HAVE_CHARDEV_PARPORT 1
1740 } ParallelCharDriver
;
1742 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1744 if (s
->mode
!= mode
) {
1746 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1753 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1755 ParallelCharDriver
*drv
= chr
->opaque
;
1760 case CHR_IOCTL_PP_READ_DATA
:
1761 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1763 *(uint8_t *)arg
= b
;
1765 case CHR_IOCTL_PP_WRITE_DATA
:
1766 b
= *(uint8_t *)arg
;
1767 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1770 case CHR_IOCTL_PP_READ_CONTROL
:
1771 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1773 /* Linux gives only the lowest bits, and no way to know data
1774 direction! For better compatibility set the fixed upper
1776 *(uint8_t *)arg
= b
| 0xc0;
1778 case CHR_IOCTL_PP_WRITE_CONTROL
:
1779 b
= *(uint8_t *)arg
;
1780 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1783 case CHR_IOCTL_PP_READ_STATUS
:
1784 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1786 *(uint8_t *)arg
= b
;
1788 case CHR_IOCTL_PP_DATA_DIR
:
1789 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1792 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1793 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1794 struct ParallelIOArg
*parg
= arg
;
1795 int n
= read(fd
, parg
->buffer
, parg
->count
);
1796 if (n
!= parg
->count
) {
1801 case CHR_IOCTL_PP_EPP_READ
:
1802 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1803 struct ParallelIOArg
*parg
= arg
;
1804 int n
= read(fd
, parg
->buffer
, parg
->count
);
1805 if (n
!= parg
->count
) {
1810 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1811 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1812 struct ParallelIOArg
*parg
= arg
;
1813 int n
= write(fd
, parg
->buffer
, parg
->count
);
1814 if (n
!= parg
->count
) {
1819 case CHR_IOCTL_PP_EPP_WRITE
:
1820 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1821 struct ParallelIOArg
*parg
= arg
;
1822 int n
= write(fd
, parg
->buffer
, parg
->count
);
1823 if (n
!= parg
->count
) {
1834 static void pp_close(CharDriverState
*chr
)
1836 ParallelCharDriver
*drv
= chr
->opaque
;
1839 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1840 ioctl(fd
, PPRELEASE
);
1843 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1846 static CharDriverState
*qemu_chr_open_pp_fd(int fd
,
1847 ChardevCommon
*backend
,
1850 CharDriverState
*chr
;
1851 ParallelCharDriver
*drv
;
1853 if (ioctl(fd
, PPCLAIM
) < 0) {
1854 error_setg_errno(errp
, errno
, "not a parallel port");
1859 chr
= qemu_chr_alloc(backend
, errp
);
1864 drv
= g_new0(ParallelCharDriver
, 1);
1866 chr
->chr_write
= null_chr_write
;
1867 chr
->chr_ioctl
= pp_ioctl
;
1868 chr
->chr_close
= pp_close
;
1871 drv
->mode
= IEEE1284_MODE_COMPAT
;
1875 #endif /* __linux__ */
1877 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1879 #define HAVE_CHARDEV_PARPORT 1
1881 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1883 int fd
= (int)(intptr_t)chr
->opaque
;
1887 case CHR_IOCTL_PP_READ_DATA
:
1888 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1890 *(uint8_t *)arg
= b
;
1892 case CHR_IOCTL_PP_WRITE_DATA
:
1893 b
= *(uint8_t *)arg
;
1894 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1897 case CHR_IOCTL_PP_READ_CONTROL
:
1898 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1900 *(uint8_t *)arg
= b
;
1902 case CHR_IOCTL_PP_WRITE_CONTROL
:
1903 b
= *(uint8_t *)arg
;
1904 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1907 case CHR_IOCTL_PP_READ_STATUS
:
1908 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1910 *(uint8_t *)arg
= b
;
1918 static CharDriverState
*qemu_chr_open_pp_fd(int fd
,
1919 ChardevCommon
*backend
,
1922 CharDriverState
*chr
;
1924 chr
= qemu_chr_alloc(backend
, errp
);
1928 chr
->opaque
= (void *)(intptr_t)fd
;
1929 chr
->chr_write
= null_chr_write
;
1930 chr
->chr_ioctl
= pp_ioctl
;
1931 chr
->explicit_be_open
= true;
1938 #define HAVE_CHARDEV_SERIAL 1
1942 HANDLE hcom
, hrecv
, hsend
;
1947 /* Protected by the CharDriverState chr_write_lock. */
1953 HANDLE hInputReadyEvent
;
1954 HANDLE hInputDoneEvent
;
1955 HANDLE hInputThread
;
1956 uint8_t win_stdio_buf
;
1957 } WinStdioCharState
;
1959 #define NSENDBUF 2048
1960 #define NRECVBUF 2048
1961 #define MAXCONNECT 1
1962 #define NTIMEOUT 5000
1964 static int win_chr_poll(void *opaque
);
1965 static int win_chr_pipe_poll(void *opaque
);
1967 static void win_chr_close(CharDriverState
*chr
)
1969 WinCharState
*s
= chr
->opaque
;
1972 CloseHandle(s
->hsend
);
1976 CloseHandle(s
->hrecv
);
1980 CloseHandle(s
->hcom
);
1984 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1986 qemu_del_polling_cb(win_chr_poll
, chr
);
1988 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1991 static int win_chr_init(CharDriverState
*chr
, const char *filename
, Error
**errp
)
1993 WinCharState
*s
= chr
->opaque
;
1995 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
2000 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2002 error_setg(errp
, "Failed CreateEvent");
2005 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2007 error_setg(errp
, "Failed CreateEvent");
2011 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
2012 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
2013 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2014 error_setg(errp
, "Failed CreateFile (%lu)", GetLastError());
2019 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
2020 error_setg(errp
, "Failed SetupComm");
2024 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
2025 size
= sizeof(COMMCONFIG
);
2026 GetDefaultCommConfig(filename
, &comcfg
, &size
);
2027 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
2028 CommConfigDialog(filename
, NULL
, &comcfg
);
2030 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
2031 error_setg(errp
, "Failed SetCommState");
2035 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
2036 error_setg(errp
, "Failed SetCommMask");
2040 cto
.ReadIntervalTimeout
= MAXDWORD
;
2041 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
2042 error_setg(errp
, "Failed SetCommTimeouts");
2046 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
2047 error_setg(errp
, "Failed ClearCommError");
2050 qemu_add_polling_cb(win_chr_poll
, chr
);
2058 /* Called with chr_write_lock held. */
2059 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
2061 WinCharState
*s
= chr
->opaque
;
2062 DWORD len
, ret
, size
, err
;
2065 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
2066 s
->osend
.hEvent
= s
->hsend
;
2069 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
2071 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
2073 err
= GetLastError();
2074 if (err
== ERROR_IO_PENDING
) {
2075 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2093 static int win_chr_read_poll(CharDriverState
*chr
)
2095 WinCharState
*s
= chr
->opaque
;
2097 s
->max_size
= qemu_chr_be_can_write(chr
);
2101 static void win_chr_readfile(CharDriverState
*chr
)
2103 WinCharState
*s
= chr
->opaque
;
2105 uint8_t buf
[READ_BUF_LEN
];
2108 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2109 s
->orecv
.hEvent
= s
->hrecv
;
2110 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2112 err
= GetLastError();
2113 if (err
== ERROR_IO_PENDING
) {
2114 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2119 qemu_chr_be_write(chr
, buf
, size
);
2123 static void win_chr_read(CharDriverState
*chr
)
2125 WinCharState
*s
= chr
->opaque
;
2127 if (s
->len
> s
->max_size
)
2128 s
->len
= s
->max_size
;
2132 win_chr_readfile(chr
);
2135 static int win_chr_poll(void *opaque
)
2137 CharDriverState
*chr
= opaque
;
2138 WinCharState
*s
= chr
->opaque
;
2142 ClearCommError(s
->hcom
, &comerr
, &status
);
2143 if (status
.cbInQue
> 0) {
2144 s
->len
= status
.cbInQue
;
2145 win_chr_read_poll(chr
);
2152 static CharDriverState
*qemu_chr_open_win_path(const char *filename
,
2153 ChardevCommon
*backend
,
2156 CharDriverState
*chr
;
2159 chr
= qemu_chr_alloc(backend
, errp
);
2163 s
= g_new0(WinCharState
, 1);
2165 chr
->chr_write
= win_chr_write
;
2166 chr
->chr_close
= win_chr_close
;
2168 if (win_chr_init(chr
, filename
, errp
) < 0) {
2170 qemu_chr_free_common(chr
);
2176 static int win_chr_pipe_poll(void *opaque
)
2178 CharDriverState
*chr
= opaque
;
2179 WinCharState
*s
= chr
->opaque
;
2182 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2185 win_chr_read_poll(chr
);
2192 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
,
2195 WinCharState
*s
= chr
->opaque
;
2203 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2205 error_setg(errp
, "Failed CreateEvent");
2208 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2210 error_setg(errp
, "Failed CreateEvent");
2214 openname
= g_strdup_printf("\\\\.\\pipe\\%s", filename
);
2215 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2216 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2218 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2220 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2221 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
2226 ZeroMemory(&ov
, sizeof(ov
));
2227 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2228 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2230 error_setg(errp
, "Failed ConnectNamedPipe");
2234 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2236 error_setg(errp
, "Failed GetOverlappedResult");
2238 CloseHandle(ov
.hEvent
);
2245 CloseHandle(ov
.hEvent
);
2248 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2257 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
2258 ChardevBackend
*backend
,
2262 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
2263 const char *filename
= opts
->device
;
2264 CharDriverState
*chr
;
2266 ChardevCommon
*common
= qapi_ChardevHostdev_base(opts
);
2268 chr
= qemu_chr_alloc(common
, errp
);
2272 s
= g_new0(WinCharState
, 1);
2274 chr
->chr_write
= win_chr_write
;
2275 chr
->chr_close
= win_chr_close
;
2277 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
2279 qemu_chr_free_common(chr
);
2285 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
,
2286 ChardevCommon
*backend
,
2289 CharDriverState
*chr
;
2292 chr
= qemu_chr_alloc(backend
, errp
);
2296 s
= g_new0(WinCharState
, 1);
2299 chr
->chr_write
= win_chr_write
;
2303 static CharDriverState
*qemu_chr_open_win_con(const char *id
,
2304 ChardevBackend
*backend
,
2308 ChardevCommon
*common
= backend
->u
.console
.data
;
2309 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
),
2313 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2315 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2322 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2332 static void win_stdio_wait_func(void *opaque
)
2334 CharDriverState
*chr
= opaque
;
2335 WinStdioCharState
*stdio
= chr
->opaque
;
2336 INPUT_RECORD buf
[4];
2341 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2344 /* Avoid error storm */
2345 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2349 for (i
= 0; i
< dwSize
; i
++) {
2350 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2352 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2354 if (kev
->uChar
.AsciiChar
!= 0) {
2355 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2356 if (qemu_chr_be_can_write(chr
)) {
2357 uint8_t c
= kev
->uChar
.AsciiChar
;
2358 qemu_chr_be_write(chr
, &c
, 1);
2366 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2368 CharDriverState
*chr
= param
;
2369 WinStdioCharState
*stdio
= chr
->opaque
;
2375 /* Wait for one byte */
2376 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2378 /* Exit in case of error, continue if nothing read */
2386 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2387 if (stdio
->win_stdio_buf
== '\r') {
2391 /* Signal the main thread and wait until the byte was eaten */
2392 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2395 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2401 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2405 static void win_stdio_thread_wait_func(void *opaque
)
2407 CharDriverState
*chr
= opaque
;
2408 WinStdioCharState
*stdio
= chr
->opaque
;
2410 if (qemu_chr_be_can_write(chr
)) {
2411 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2414 SetEvent(stdio
->hInputDoneEvent
);
2417 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2419 WinStdioCharState
*stdio
= chr
->opaque
;
2422 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2425 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2427 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2431 static void win_stdio_close(CharDriverState
*chr
)
2433 WinStdioCharState
*stdio
= chr
->opaque
;
2435 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2436 CloseHandle(stdio
->hInputReadyEvent
);
2438 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2439 CloseHandle(stdio
->hInputDoneEvent
);
2441 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2442 TerminateThread(stdio
->hInputThread
, 0);
2445 g_free(chr
->opaque
);
2449 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
2450 ChardevBackend
*backend
,
2454 CharDriverState
*chr
;
2455 WinStdioCharState
*stdio
;
2458 ChardevCommon
*common
= qapi_ChardevStdio_base(backend
->u
.stdio
.data
);
2460 chr
= qemu_chr_alloc(common
, errp
);
2464 stdio
= g_new0(WinStdioCharState
, 1);
2466 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2467 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2468 error_setg(errp
, "cannot open stdio: invalid handle");
2472 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2474 chr
->opaque
= stdio
;
2475 chr
->chr_write
= win_stdio_write
;
2476 chr
->chr_close
= win_stdio_close
;
2479 if (qemu_add_wait_object(stdio
->hStdIn
,
2480 win_stdio_wait_func
, chr
)) {
2481 error_setg(errp
, "qemu_add_wait_object: failed");
2487 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2488 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2489 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2490 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2491 error_setg(errp
, "cannot create event");
2494 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2495 win_stdio_thread_wait_func
, chr
)) {
2496 error_setg(errp
, "qemu_add_wait_object: failed");
2499 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2502 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
2503 error_setg(errp
, "cannot create stdio thread");
2508 dwMode
|= ENABLE_LINE_INPUT
;
2511 /* set the terminal in raw mode */
2512 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2513 dwMode
|= ENABLE_PROCESSED_INPUT
;
2516 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2518 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2519 qemu_chr_fe_set_echo(chr
, false);
2524 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2526 CloseHandle(stdio
->hInputReadyEvent
);
2527 CloseHandle(stdio
->hInputDoneEvent
);
2529 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2532 #endif /* !_WIN32 */
2535 /***********************************************************/
2536 /* UDP Net console */
2540 uint8_t buf
[READ_BUF_LEN
];
2546 /* Called with chr_write_lock held. */
2547 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2549 NetCharDriver
*s
= chr
->opaque
;
2551 return qio_channel_write(
2552 s
->ioc
, (const char *)buf
, len
, NULL
);
2555 static int udp_chr_read_poll(void *opaque
)
2557 CharDriverState
*chr
= opaque
;
2558 NetCharDriver
*s
= chr
->opaque
;
2560 s
->max_size
= qemu_chr_be_can_write(chr
);
2562 /* If there were any stray characters in the queue process them
2565 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2566 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2568 s
->max_size
= qemu_chr_be_can_write(chr
);
2573 static gboolean
udp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2575 CharDriverState
*chr
= opaque
;
2576 NetCharDriver
*s
= chr
->opaque
;
2579 if (s
->max_size
== 0) {
2582 ret
= qio_channel_read(
2583 s
->ioc
, (char *)s
->buf
, sizeof(s
->buf
), NULL
);
2585 remove_fd_in_watch(chr
);
2591 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2592 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2594 s
->max_size
= qemu_chr_be_can_write(chr
);
2600 static void udp_chr_update_read_handler(CharDriverState
*chr
,
2601 GMainContext
*context
)
2603 NetCharDriver
*s
= chr
->opaque
;
2605 remove_fd_in_watch(chr
);
2607 chr
->fd_in_tag
= io_add_watch_poll(s
->ioc
,
2614 static void udp_chr_close(CharDriverState
*chr
)
2616 NetCharDriver
*s
= chr
->opaque
;
2618 remove_fd_in_watch(chr
);
2620 object_unref(OBJECT(s
->ioc
));
2623 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2626 static CharDriverState
*qemu_chr_open_udp(QIOChannelSocket
*sioc
,
2627 ChardevCommon
*backend
,
2630 CharDriverState
*chr
= NULL
;
2631 NetCharDriver
*s
= NULL
;
2633 chr
= qemu_chr_alloc(backend
, errp
);
2637 s
= g_new0(NetCharDriver
, 1);
2639 s
->ioc
= QIO_CHANNEL(sioc
);
2643 chr
->chr_write
= udp_chr_write
;
2644 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2645 chr
->chr_close
= udp_chr_close
;
2646 /* be isn't opened until we get a connection */
2647 chr
->explicit_be_open
= true;
2651 /***********************************************************/
2652 /* TCP Net console */
2655 QIOChannel
*ioc
; /* Client I/O channel */
2656 QIOChannelSocket
*sioc
; /* Client master channel */
2657 QIOChannelSocket
*listen_ioc
;
2659 QCryptoTLSCreds
*tls_creds
;
2666 size_t read_msgfds_num
;
2668 size_t write_msgfds_num
;
2670 SocketAddress
*addr
;
2674 guint reconnect_timer
;
2675 int64_t reconnect_time
;
2676 bool connect_err_reported
;
2679 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2681 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2683 TCPCharDriver
*s
= chr
->opaque
;
2684 assert(s
->connected
== 0);
2685 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2686 socket_reconnect_timeout
, chr
);
2689 static void check_report_connect_error(CharDriverState
*chr
,
2692 TCPCharDriver
*s
= chr
->opaque
;
2694 if (!s
->connect_err_reported
) {
2695 error_report("Unable to connect character device %s: %s",
2696 chr
->label
, error_get_pretty(err
));
2697 s
->connect_err_reported
= true;
2699 qemu_chr_socket_restart_timer(chr
);
2702 static gboolean
tcp_chr_accept(QIOChannel
*chan
,
2706 /* Called with chr_write_lock held. */
2707 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2709 TCPCharDriver
*s
= chr
->opaque
;
2711 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
2713 s
->write_msgfds_num
);
2715 /* free the written msgfds, no matter what */
2716 if (s
->write_msgfds_num
) {
2717 g_free(s
->write_msgfds
);
2718 s
->write_msgfds
= 0;
2719 s
->write_msgfds_num
= 0;
2724 /* XXX: indicate an error ? */
2729 static int tcp_chr_read_poll(void *opaque
)
2731 CharDriverState
*chr
= opaque
;
2732 TCPCharDriver
*s
= chr
->opaque
;
2735 s
->max_size
= qemu_chr_be_can_write(chr
);
2740 #define IAC_BREAK 243
2741 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2743 uint8_t *buf
, int *size
)
2745 /* Handle any telnet client's basic IAC options to satisfy char by
2746 * char mode with no echo. All IAC options will be removed from
2747 * the buf and the do_telnetopt variable will be used to track the
2748 * state of the width of the IAC information.
2750 * IAC commands come in sets of 3 bytes with the exception of the
2751 * "IAC BREAK" command and the double IAC.
2757 for (i
= 0; i
< *size
; i
++) {
2758 if (s
->do_telnetopt
> 1) {
2759 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2760 /* Double IAC means send an IAC */
2764 s
->do_telnetopt
= 1;
2766 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2767 /* Handle IAC break commands by sending a serial break */
2768 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2773 if (s
->do_telnetopt
>= 4) {
2774 s
->do_telnetopt
= 1;
2777 if ((unsigned char)buf
[i
] == IAC
) {
2778 s
->do_telnetopt
= 2;
2789 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2791 TCPCharDriver
*s
= chr
->opaque
;
2792 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2794 assert(num
<= TCP_MAX_FDS
);
2799 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2801 /* Close unused fds */
2802 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2803 close(s
->read_msgfds
[i
]);
2806 g_free(s
->read_msgfds
);
2808 s
->read_msgfds_num
= 0;
2814 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2816 TCPCharDriver
*s
= chr
->opaque
;
2818 /* clear old pending fd array */
2819 g_free(s
->write_msgfds
);
2820 s
->write_msgfds
= NULL
;
2821 s
->write_msgfds_num
= 0;
2823 if (!s
->connected
||
2824 !qio_channel_has_feature(s
->ioc
,
2825 QIO_CHANNEL_FEATURE_FD_PASS
)) {
2830 s
->write_msgfds
= g_new(int, num
);
2831 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2834 s
->write_msgfds_num
= num
;
2839 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2841 TCPCharDriver
*s
= chr
->opaque
;
2842 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
2846 size_t msgfds_num
= 0;
2848 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
2849 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
2850 &msgfds
, &msgfds_num
,
2853 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
2858 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
2861 } else if (ret
== -1) {
2866 /* close and clean read_msgfds */
2867 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2868 close(s
->read_msgfds
[i
]);
2871 if (s
->read_msgfds_num
) {
2872 g_free(s
->read_msgfds
);
2875 s
->read_msgfds
= msgfds
;
2876 s
->read_msgfds_num
= msgfds_num
;
2879 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2880 int fd
= s
->read_msgfds
[i
];
2885 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2888 #ifndef MSG_CMSG_CLOEXEC
2889 qemu_set_cloexec(fd
);
2896 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2898 TCPCharDriver
*s
= chr
->opaque
;
2899 return qio_channel_create_watch(s
->ioc
, cond
);
2902 static void tcp_chr_free_connection(CharDriverState
*chr
)
2904 TCPCharDriver
*s
= chr
->opaque
;
2907 if (!s
->connected
) {
2911 if (s
->read_msgfds_num
) {
2912 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2913 close(s
->read_msgfds
[i
]);
2915 g_free(s
->read_msgfds
);
2916 s
->read_msgfds
= NULL
;
2917 s
->read_msgfds_num
= 0;
2920 tcp_set_msgfds(chr
, NULL
, 0);
2921 remove_fd_in_watch(chr
);
2922 object_unref(OBJECT(s
->sioc
));
2924 object_unref(OBJECT(s
->ioc
));
2926 g_free(chr
->filename
);
2927 chr
->filename
= NULL
;
2931 static void tcp_chr_disconnect(CharDriverState
*chr
)
2933 TCPCharDriver
*s
= chr
->opaque
;
2935 if (!s
->connected
) {
2939 tcp_chr_free_connection(chr
);
2941 if (s
->listen_ioc
) {
2942 s
->listen_tag
= qio_channel_add_watch(
2943 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, tcp_chr_accept
, chr
, NULL
);
2945 chr
->filename
= SocketAddress_to_str("disconnected:", s
->addr
,
2946 s
->is_listen
, s
->is_telnet
);
2947 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2948 if (s
->reconnect_time
) {
2949 qemu_chr_socket_restart_timer(chr
);
2953 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2955 CharDriverState
*chr
= opaque
;
2956 TCPCharDriver
*s
= chr
->opaque
;
2957 uint8_t buf
[READ_BUF_LEN
];
2960 if (!s
->connected
|| s
->max_size
<= 0) {
2964 if (len
> s
->max_size
)
2966 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2967 if (size
== 0 || size
== -1) {
2968 /* connection closed */
2969 tcp_chr_disconnect(chr
);
2970 } else if (size
> 0) {
2971 if (s
->do_telnetopt
)
2972 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2974 qemu_chr_be_write(chr
, buf
, size
);
2980 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2982 TCPCharDriver
*s
= chr
->opaque
;
2985 if (!s
->connected
) {
2989 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2991 /* connection closed */
2992 tcp_chr_disconnect(chr
);
2998 static void tcp_chr_connect(void *opaque
)
3000 CharDriverState
*chr
= opaque
;
3001 TCPCharDriver
*s
= chr
->opaque
;
3003 g_free(chr
->filename
);
3004 chr
->filename
= sockaddr_to_str(
3005 &s
->sioc
->localAddr
, s
->sioc
->localAddrLen
,
3006 &s
->sioc
->remoteAddr
, s
->sioc
->remoteAddrLen
,
3007 s
->is_listen
, s
->is_telnet
);
3011 chr
->fd_in_tag
= io_add_watch_poll(s
->ioc
,
3016 qemu_chr_be_generic_open(chr
);
3019 static void tcp_chr_update_read_handler(CharDriverState
*chr
,
3020 GMainContext
*context
)
3022 TCPCharDriver
*s
= chr
->opaque
;
3024 if (!s
->connected
) {
3028 remove_fd_in_watch(chr
);
3030 chr
->fd_in_tag
= io_add_watch_poll(s
->ioc
,
3038 CharDriverState
*chr
;
3041 } TCPCharDriverTelnetInit
;
3043 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
3044 GIOCondition cond G_GNUC_UNUSED
,
3047 TCPCharDriverTelnetInit
*init
= user_data
;
3050 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
3052 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
3055 tcp_chr_disconnect(init
->chr
);
3059 init
->buflen
-= ret
;
3061 if (init
->buflen
== 0) {
3062 tcp_chr_connect(init
->chr
);
3066 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
3071 static void tcp_chr_telnet_init(CharDriverState
*chr
)
3073 TCPCharDriver
*s
= chr
->opaque
;
3074 TCPCharDriverTelnetInit
*init
=
3075 g_new0(TCPCharDriverTelnetInit
, 1);
3081 #define IACSET(x, a, b, c) \
3088 /* Prep the telnet negotion to put telnet in binary,
3089 * no echo, single char mode */
3090 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3091 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3092 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3093 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3097 qio_channel_add_watch(
3099 tcp_chr_telnet_init_io
,
3104 static void tcp_chr_tls_handshake(Object
*source
,
3108 CharDriverState
*chr
= user_data
;
3109 TCPCharDriver
*s
= chr
->opaque
;
3112 tcp_chr_disconnect(chr
);
3114 if (s
->do_telnetopt
) {
3115 tcp_chr_telnet_init(chr
);
3117 tcp_chr_connect(chr
);
3123 static void tcp_chr_tls_init(CharDriverState
*chr
)
3125 TCPCharDriver
*s
= chr
->opaque
;
3126 QIOChannelTLS
*tioc
;
3130 tioc
= qio_channel_tls_new_server(
3131 s
->ioc
, s
->tls_creds
,
3132 NULL
, /* XXX Use an ACL */
3135 tioc
= qio_channel_tls_new_client(
3136 s
->ioc
, s
->tls_creds
,
3137 s
->addr
->u
.inet
.data
->host
,
3142 tcp_chr_disconnect(chr
);
3145 object_unref(OBJECT(s
->ioc
));
3146 s
->ioc
= QIO_CHANNEL(tioc
);
3148 qio_channel_tls_handshake(tioc
,
3149 tcp_chr_tls_handshake
,
3155 static int tcp_chr_new_client(CharDriverState
*chr
, QIOChannelSocket
*sioc
)
3157 TCPCharDriver
*s
= chr
->opaque
;
3158 if (s
->ioc
!= NULL
) {
3162 s
->ioc
= QIO_CHANNEL(sioc
);
3163 object_ref(OBJECT(sioc
));
3165 object_ref(OBJECT(sioc
));
3167 qio_channel_set_blocking(s
->ioc
, false, NULL
);
3169 if (s
->do_nodelay
) {
3170 qio_channel_set_delay(s
->ioc
, false);
3172 if (s
->listen_tag
) {
3173 g_source_remove(s
->listen_tag
);
3178 tcp_chr_tls_init(chr
);
3180 if (s
->do_telnetopt
) {
3181 tcp_chr_telnet_init(chr
);
3183 tcp_chr_connect(chr
);
3191 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
3194 QIOChannelSocket
*sioc
;
3196 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
3200 ret
= tcp_chr_new_client(chr
, sioc
);
3201 object_unref(OBJECT(sioc
));
3205 static gboolean
tcp_chr_accept(QIOChannel
*channel
,
3209 CharDriverState
*chr
= opaque
;
3210 QIOChannelSocket
*sioc
;
3212 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel
),
3218 tcp_chr_new_client(chr
, sioc
);
3220 object_unref(OBJECT(sioc
));
3225 static int tcp_chr_wait_connected(CharDriverState
*chr
, Error
**errp
)
3227 TCPCharDriver
*s
= chr
->opaque
;
3228 QIOChannelSocket
*sioc
;
3230 /* It can't wait on s->connected, since it is set asynchronously
3231 * in TLS and telnet cases, only wait for an accepted socket */
3234 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
3236 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), true, NULL
);
3237 tcp_chr_accept(QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, chr
);
3238 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), false, NULL
);
3240 sioc
= qio_channel_socket_new();
3241 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
3242 object_unref(OBJECT(sioc
));
3245 tcp_chr_new_client(chr
, sioc
);
3246 object_unref(OBJECT(sioc
));
3253 int qemu_chr_wait_connected(CharDriverState
*chr
, Error
**errp
)
3255 if (chr
->chr_wait_connected
) {
3256 return chr
->chr_wait_connected(chr
, errp
);
3262 static void tcp_chr_close(CharDriverState
*chr
)
3264 TCPCharDriver
*s
= chr
->opaque
;
3266 tcp_chr_free_connection(chr
);
3268 if (s
->reconnect_timer
) {
3269 g_source_remove(s
->reconnect_timer
);
3270 s
->reconnect_timer
= 0;
3272 qapi_free_SocketAddress(s
->addr
);
3273 if (s
->listen_tag
) {
3274 g_source_remove(s
->listen_tag
);
3277 if (s
->listen_ioc
) {
3278 object_unref(OBJECT(s
->listen_ioc
));
3281 object_unref(OBJECT(s
->tls_creds
));
3284 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3288 static void qemu_chr_socket_connected(Object
*src
, Error
*err
, void *opaque
)
3290 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(src
);
3291 CharDriverState
*chr
= opaque
;
3292 TCPCharDriver
*s
= chr
->opaque
;
3295 check_report_connect_error(chr
, err
);
3300 s
->connect_err_reported
= false;
3301 tcp_chr_new_client(chr
, sioc
);
3302 object_unref(OBJECT(sioc
));
3306 /*********************************************************/
3307 /* Ring buffer chardev */
3314 } RingBufCharDriver
;
3316 static size_t ringbuf_count(const CharDriverState
*chr
)
3318 const RingBufCharDriver
*d
= chr
->opaque
;
3320 return d
->prod
- d
->cons
;
3323 /* Called with chr_write_lock held. */
3324 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3326 RingBufCharDriver
*d
= chr
->opaque
;
3329 if (!buf
|| (len
< 0)) {
3333 for (i
= 0; i
< len
; i
++ ) {
3334 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3335 if (d
->prod
- d
->cons
> d
->size
) {
3336 d
->cons
= d
->prod
- d
->size
;
3343 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3345 RingBufCharDriver
*d
= chr
->opaque
;
3348 qemu_mutex_lock(&chr
->chr_write_lock
);
3349 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3350 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3352 qemu_mutex_unlock(&chr
->chr_write_lock
);
3357 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3359 RingBufCharDriver
*d
= chr
->opaque
;
3366 static CharDriverState
*qemu_chr_open_ringbuf(const char *id
,
3367 ChardevBackend
*backend
,
3371 ChardevRingbuf
*opts
= backend
->u
.ringbuf
.data
;
3372 ChardevCommon
*common
= qapi_ChardevRingbuf_base(opts
);
3373 CharDriverState
*chr
;
3374 RingBufCharDriver
*d
;
3376 chr
= qemu_chr_alloc(common
, errp
);
3380 d
= g_malloc(sizeof(*d
));
3382 d
->size
= opts
->has_size
? opts
->size
: 65536;
3384 /* The size must be power of 2 */
3385 if (d
->size
& (d
->size
- 1)) {
3386 error_setg(errp
, "size of ringbuf chardev must be power of two");
3392 d
->cbuf
= g_malloc0(d
->size
);
3395 chr
->chr_write
= ringbuf_chr_write
;
3396 chr
->chr_close
= ringbuf_chr_close
;
3402 qemu_chr_free_common(chr
);
3406 bool chr_is_ringbuf(const CharDriverState
*chr
)
3408 return chr
->chr_write
== ringbuf_chr_write
;
3411 void qmp_ringbuf_write(const char *device
, const char *data
,
3412 bool has_format
, enum DataFormat format
,
3415 CharDriverState
*chr
;
3416 const uint8_t *write_data
;
3420 chr
= qemu_chr_find(device
);
3422 error_setg(errp
, "Device '%s' not found", device
);
3426 if (!chr_is_ringbuf(chr
)) {
3427 error_setg(errp
,"%s is not a ringbuf device", device
);
3431 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3432 write_data
= qbase64_decode(data
, -1,
3439 write_data
= (uint8_t *)data
;
3440 write_count
= strlen(data
);
3443 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3445 if (write_data
!= (uint8_t *)data
) {
3446 g_free((void *)write_data
);
3450 error_setg(errp
, "Failed to write to device %s", device
);
3455 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3456 bool has_format
, enum DataFormat format
,
3459 CharDriverState
*chr
;
3464 chr
= qemu_chr_find(device
);
3466 error_setg(errp
, "Device '%s' not found", device
);
3470 if (!chr_is_ringbuf(chr
)) {
3471 error_setg(errp
,"%s is not a ringbuf device", device
);
3476 error_setg(errp
, "size must be greater than zero");
3480 count
= ringbuf_count(chr
);
3481 size
= size
> count
? count
: size
;
3482 read_data
= g_malloc(size
+ 1);
3484 ringbuf_chr_read(chr
, read_data
, size
);
3486 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3487 data
= g_base64_encode(read_data
, size
);
3491 * FIXME should read only complete, valid UTF-8 characters up
3492 * to @size bytes. Invalid sequences should be replaced by a
3493 * suitable replacement character. Except when (and only
3494 * when) ring buffer lost characters since last read, initial
3495 * continuation characters should be dropped.
3497 read_data
[size
] = 0;
3498 data
= (char *)read_data
;
3504 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3506 char host
[65], port
[33], width
[8], height
[8];
3510 Error
*local_err
= NULL
;
3512 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3514 error_report_err(local_err
);
3518 if (strstart(filename
, "mon:", &p
)) {
3520 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3521 if (strcmp(filename
, "stdio") == 0) {
3522 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3523 * but pass it to the guest. Handle this only for compat syntax,
3524 * for -chardev syntax we have special option for this.
3525 * This is what -nographic did, redirecting+muxing serial+monitor
3526 * to stdio causing Ctrl+C to be passed to guest. */
3527 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3531 if (strcmp(filename
, "null") == 0 ||
3532 strcmp(filename
, "pty") == 0 ||
3533 strcmp(filename
, "msmouse") == 0 ||
3534 strcmp(filename
, "braille") == 0 ||
3535 strcmp(filename
, "testdev") == 0 ||
3536 strcmp(filename
, "stdio") == 0) {
3537 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3540 if (strstart(filename
, "vc", &p
)) {
3541 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3543 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3545 qemu_opt_set(opts
, "width", width
, &error_abort
);
3546 qemu_opt_set(opts
, "height", height
, &error_abort
);
3547 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3549 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3550 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3557 if (strcmp(filename
, "con:") == 0) {
3558 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3561 if (strstart(filename
, "COM", NULL
)) {
3562 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3563 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3566 if (strstart(filename
, "file:", &p
)) {
3567 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3568 qemu_opt_set(opts
, "path", p
, &error_abort
);
3571 if (strstart(filename
, "pipe:", &p
)) {
3572 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3573 qemu_opt_set(opts
, "path", p
, &error_abort
);
3576 if (strstart(filename
, "tcp:", &p
) ||
3577 strstart(filename
, "telnet:", &p
)) {
3578 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3580 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3583 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3584 qemu_opt_set(opts
, "host", host
, &error_abort
);
3585 qemu_opt_set(opts
, "port", port
, &error_abort
);
3586 if (p
[pos
] == ',') {
3587 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3589 error_report_err(local_err
);
3593 if (strstart(filename
, "telnet:", &p
))
3594 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3597 if (strstart(filename
, "udp:", &p
)) {
3598 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3599 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3601 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3605 qemu_opt_set(opts
, "host", host
, &error_abort
);
3606 qemu_opt_set(opts
, "port", port
, &error_abort
);
3607 if (p
[pos
] == '@') {
3609 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3611 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3615 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3616 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3620 if (strstart(filename
, "unix:", &p
)) {
3621 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3622 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3624 error_report_err(local_err
);
3629 if (strstart(filename
, "/dev/parport", NULL
) ||
3630 strstart(filename
, "/dev/ppi", NULL
)) {
3631 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3632 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3635 if (strstart(filename
, "/dev/", NULL
)) {
3636 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3637 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3642 qemu_opts_del(opts
);
3646 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
3648 const char *logfile
= qemu_opt_get(opts
, "logfile");
3650 backend
->has_logfile
= logfile
!= NULL
;
3651 backend
->logfile
= logfile
? g_strdup(logfile
) : NULL
;
3653 backend
->has_logappend
= true;
3654 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
3658 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3661 const char *path
= qemu_opt_get(opts
, "path");
3665 error_setg(errp
, "chardev: file: no filename given");
3668 file
= backend
->u
.file
.data
= g_new0(ChardevFile
, 1);
3669 qemu_chr_parse_common(opts
, qapi_ChardevFile_base(file
));
3670 file
->out
= g_strdup(path
);
3672 file
->has_append
= true;
3673 file
->append
= qemu_opt_get_bool(opts
, "append", false);
3676 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3679 ChardevStdio
*stdio
;
3681 stdio
= backend
->u
.stdio
.data
= g_new0(ChardevStdio
, 1);
3682 qemu_chr_parse_common(opts
, qapi_ChardevStdio_base(stdio
));
3683 stdio
->has_signal
= true;
3684 stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3687 #ifdef HAVE_CHARDEV_SERIAL
3688 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3691 const char *device
= qemu_opt_get(opts
, "path");
3692 ChardevHostdev
*serial
;
3694 if (device
== NULL
) {
3695 error_setg(errp
, "chardev: serial/tty: no device path given");
3698 serial
= backend
->u
.serial
.data
= g_new0(ChardevHostdev
, 1);
3699 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(serial
));
3700 serial
->device
= g_strdup(device
);
3704 #ifdef HAVE_CHARDEV_PARPORT
3705 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3708 const char *device
= qemu_opt_get(opts
, "path");
3709 ChardevHostdev
*parallel
;
3711 if (device
== NULL
) {
3712 error_setg(errp
, "chardev: parallel: no device path given");
3715 parallel
= backend
->u
.parallel
.data
= g_new0(ChardevHostdev
, 1);
3716 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(parallel
));
3717 parallel
->device
= g_strdup(device
);
3721 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3724 const char *device
= qemu_opt_get(opts
, "path");
3725 ChardevHostdev
*dev
;
3727 if (device
== NULL
) {
3728 error_setg(errp
, "chardev: pipe: no device path given");
3731 dev
= backend
->u
.pipe
.data
= g_new0(ChardevHostdev
, 1);
3732 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(dev
));
3733 dev
->device
= g_strdup(device
);
3736 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3740 ChardevRingbuf
*ringbuf
;
3742 ringbuf
= backend
->u
.ringbuf
.data
= g_new0(ChardevRingbuf
, 1);
3743 qemu_chr_parse_common(opts
, qapi_ChardevRingbuf_base(ringbuf
));
3745 val
= qemu_opt_get_size(opts
, "size", 0);
3747 ringbuf
->has_size
= true;
3748 ringbuf
->size
= val
;
3752 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3755 const char *chardev
= qemu_opt_get(opts
, "chardev");
3758 if (chardev
== NULL
) {
3759 error_setg(errp
, "chardev: mux: no chardev given");
3762 mux
= backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
3763 qemu_chr_parse_common(opts
, qapi_ChardevMux_base(mux
));
3764 mux
->chardev
= g_strdup(chardev
);
3767 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3770 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3771 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3772 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3773 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3774 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3775 const char *path
= qemu_opt_get(opts
, "path");
3776 const char *host
= qemu_opt_get(opts
, "host");
3777 const char *port
= qemu_opt_get(opts
, "port");
3778 const char *tls_creds
= qemu_opt_get(opts
, "tls-creds");
3779 SocketAddress
*addr
;
3780 ChardevSocket
*sock
;
3784 error_setg(errp
, "chardev: socket: no host given");
3788 error_setg(errp
, "chardev: socket: no port given");
3793 error_setg(errp
, "TLS can only be used over TCP socket");
3798 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
3799 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
3801 sock
->has_nodelay
= true;
3802 sock
->nodelay
= do_nodelay
;
3803 sock
->has_server
= true;
3804 sock
->server
= is_listen
;
3805 sock
->has_telnet
= true;
3806 sock
->telnet
= is_telnet
;
3807 sock
->has_wait
= true;
3808 sock
->wait
= is_waitconnect
;
3809 sock
->has_reconnect
= true;
3810 sock
->reconnect
= reconnect
;
3811 sock
->tls_creds
= g_strdup(tls_creds
);
3813 addr
= g_new0(SocketAddress
, 1);
3815 UnixSocketAddress
*q_unix
;
3816 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
3817 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
3818 q_unix
->path
= g_strdup(path
);
3820 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3821 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
3822 *addr
->u
.inet
.data
= (InetSocketAddress
) {
3823 .host
= g_strdup(host
),
3824 .port
= g_strdup(port
),
3825 .has_to
= qemu_opt_get(opts
, "to"),
3826 .to
= qemu_opt_get_number(opts
, "to", 0),
3827 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
3828 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
3829 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
3830 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
3836 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
3839 const char *host
= qemu_opt_get(opts
, "host");
3840 const char *port
= qemu_opt_get(opts
, "port");
3841 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
3842 const char *localport
= qemu_opt_get(opts
, "localport");
3843 bool has_local
= false;
3844 SocketAddress
*addr
;
3847 if (host
== NULL
|| strlen(host
) == 0) {
3850 if (port
== NULL
|| strlen(port
) == 0) {
3851 error_setg(errp
, "chardev: udp: remote port not specified");
3854 if (localport
== NULL
|| strlen(localport
) == 0) {
3859 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
3865 udp
= backend
->u
.udp
.data
= g_new0(ChardevUdp
, 1);
3866 qemu_chr_parse_common(opts
, qapi_ChardevUdp_base(udp
));
3868 addr
= g_new0(SocketAddress
, 1);
3869 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3870 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
3871 *addr
->u
.inet
.data
= (InetSocketAddress
) {
3872 .host
= g_strdup(host
),
3873 .port
= g_strdup(port
),
3874 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
3875 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
3876 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
3877 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
3882 udp
->has_local
= true;
3883 addr
= g_new0(SocketAddress
, 1);
3884 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3885 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
3886 *addr
->u
.inet
.data
= (InetSocketAddress
) {
3887 .host
= g_strdup(localaddr
),
3888 .port
= g_strdup(localport
),
3894 typedef struct CharDriver
{
3896 ChardevBackendKind kind
;
3897 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3898 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3899 ChardevReturn
*ret
, Error
**errp
);
3902 static GSList
*backends
;
3904 void register_char_driver(const char *name
, ChardevBackendKind kind
,
3905 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
),
3906 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3907 ChardevReturn
*ret
, Error
**errp
))
3911 s
= g_malloc0(sizeof(*s
));
3912 s
->name
= g_strdup(name
);
3917 backends
= g_slist_append(backends
, s
);
3920 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3921 void (*init
)(struct CharDriverState
*s
),
3924 Error
*local_err
= NULL
;
3926 CharDriverState
*chr
;
3928 ChardevReturn
*ret
= NULL
;
3929 ChardevBackend
*backend
;
3930 const char *id
= qemu_opts_id(opts
);
3933 if (qemu_opt_get(opts
, "backend") == NULL
) {
3934 error_setg(errp
, "chardev: \"%s\" missing backend",
3935 qemu_opts_id(opts
));
3939 if (is_help_option(qemu_opt_get(opts
, "backend"))) {
3940 fprintf(stderr
, "Available chardev backend types:\n");
3941 for (i
= backends
; i
; i
= i
->next
) {
3943 fprintf(stderr
, "%s\n", cd
->name
);
3945 exit(!is_help_option(qemu_opt_get(opts
, "backend")));
3949 error_setg(errp
, "chardev: no id specified");
3953 for (i
= backends
; i
; i
= i
->next
) {
3956 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3961 error_setg(errp
, "chardev: backend \"%s\" not found",
3962 qemu_opt_get(opts
, "backend"));
3966 backend
= g_new0(ChardevBackend
, 1);
3968 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3969 bid
= g_strdup_printf("%s-base", id
);
3973 backend
->type
= cd
->kind
;
3975 cd
->parse(opts
, backend
, &local_err
);
3977 error_propagate(errp
, local_err
);
3981 ChardevCommon
*cc
= g_new0(ChardevCommon
, 1);
3982 qemu_chr_parse_common(opts
, cc
);
3983 backend
->u
.null
.data
= cc
; /* Any ChardevCommon member would work */
3986 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3992 qapi_free_ChardevBackend(backend
);
3993 qapi_free_ChardevReturn(ret
);
3994 backend
= g_new0(ChardevBackend
, 1);
3995 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
3996 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
3997 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
3998 ret
= qmp_chardev_add(id
, backend
, errp
);
4000 chr
= qemu_chr_find(bid
);
4001 qemu_chr_delete(chr
);
4007 chr
= qemu_chr_find(id
);
4010 qapi_free_ChardevBackend(backend
);
4011 qapi_free_ChardevReturn(ret
);
4019 CharDriverState
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
4020 void (*init
)(struct CharDriverState
*s
))
4023 CharDriverState
*chr
;
4027 if (strstart(filename
, "chardev:", &p
)) {
4028 return qemu_chr_find(p
);
4031 opts
= qemu_chr_parse_compat(label
, filename
);
4035 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
4037 error_report_err(err
);
4039 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
4040 qemu_chr_fe_claim_no_fail(chr
);
4041 monitor_init(chr
, MONITOR_USE_READLINE
);
4043 qemu_opts_del(opts
);
4047 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
4049 CharDriverState
*chr
;
4050 chr
= qemu_chr_new_noreplay(label
, filename
, init
);
4052 chr
->replay
= replay_mode
!= REPLAY_MODE_NONE
;
4053 if (chr
->replay
&& chr
->chr_ioctl
) {
4055 "Replay: ioctl is not supported for serial devices yet\n");
4057 replay_register_char_driver(chr
);
4062 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
4064 if (chr
->chr_set_echo
) {
4065 chr
->chr_set_echo(chr
, echo
);
4069 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
4071 if (chr
->fe_open
== fe_open
) {
4074 chr
->fe_open
= fe_open
;
4075 if (chr
->chr_set_fe_open
) {
4076 chr
->chr_set_fe_open(chr
, fe_open
);
4080 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
4082 if (chr
->chr_fe_event
) {
4083 chr
->chr_fe_event(chr
, event
);
4087 guint
qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
4088 GIOFunc func
, void *user_data
)
4093 if (s
->chr_add_watch
== NULL
) {
4097 src
= s
->chr_add_watch(s
, cond
);
4102 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
4103 tag
= g_source_attach(src
, NULL
);
4104 g_source_unref(src
);
4109 int qemu_chr_fe_claim(CharDriverState
*s
)
4111 if (s
->avail_connections
< 1) {
4114 s
->avail_connections
--;
4118 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
4120 if (qemu_chr_fe_claim(s
) != 0) {
4121 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
4122 __func__
, s
->label
);
4127 void qemu_chr_fe_release(CharDriverState
*s
)
4129 s
->avail_connections
++;
4132 void qemu_chr_disconnect(CharDriverState
*chr
)
4134 if (chr
->chr_disconnect
) {
4135 chr
->chr_disconnect(chr
);
4139 static void qemu_chr_free_common(CharDriverState
*chr
)
4141 g_free(chr
->filename
);
4143 if (chr
->logfd
!= -1) {
4146 qemu_mutex_destroy(&chr
->chr_write_lock
);
4150 void qemu_chr_free(CharDriverState
*chr
)
4152 if (chr
->chr_close
) {
4153 chr
->chr_close(chr
);
4155 qemu_chr_free_common(chr
);
4158 void qemu_chr_delete(CharDriverState
*chr
)
4160 QTAILQ_REMOVE(&chardevs
, chr
, next
);
4164 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
4166 ChardevInfoList
*chr_list
= NULL
;
4167 CharDriverState
*chr
;
4169 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
4170 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
4171 info
->value
= g_malloc0(sizeof(*info
->value
));
4172 info
->value
->label
= g_strdup(chr
->label
);
4173 info
->value
->filename
= g_strdup(chr
->filename
);
4174 info
->value
->frontend_open
= chr
->fe_open
;
4176 info
->next
= chr_list
;
4183 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
4185 ChardevBackendInfoList
*backend_list
= NULL
;
4186 CharDriver
*c
= NULL
;
4189 for (i
= backends
; i
; i
= i
->next
) {
4190 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
4192 info
->value
= g_malloc0(sizeof(*info
->value
));
4193 info
->value
->name
= g_strdup(c
->name
);
4195 info
->next
= backend_list
;
4196 backend_list
= info
;
4199 return backend_list
;
4202 CharDriverState
*qemu_chr_find(const char *name
)
4204 CharDriverState
*chr
;
4206 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
4207 if (strcmp(chr
->label
, name
) != 0)
4214 QemuOptsList qemu_chardev_opts
= {
4216 .implied_opt_name
= "backend",
4217 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
4221 .type
= QEMU_OPT_STRING
,
4224 .type
= QEMU_OPT_STRING
,
4227 .type
= QEMU_OPT_STRING
,
4230 .type
= QEMU_OPT_STRING
,
4232 .name
= "localaddr",
4233 .type
= QEMU_OPT_STRING
,
4235 .name
= "localport",
4236 .type
= QEMU_OPT_STRING
,
4239 .type
= QEMU_OPT_NUMBER
,
4242 .type
= QEMU_OPT_BOOL
,
4245 .type
= QEMU_OPT_BOOL
,
4248 .type
= QEMU_OPT_BOOL
,
4251 .type
= QEMU_OPT_BOOL
,
4254 .type
= QEMU_OPT_BOOL
,
4256 .name
= "reconnect",
4257 .type
= QEMU_OPT_NUMBER
,
4260 .type
= QEMU_OPT_BOOL
,
4262 .name
= "tls-creds",
4263 .type
= QEMU_OPT_STRING
,
4266 .type
= QEMU_OPT_NUMBER
,
4269 .type
= QEMU_OPT_NUMBER
,
4272 .type
= QEMU_OPT_NUMBER
,
4275 .type
= QEMU_OPT_NUMBER
,
4278 .type
= QEMU_OPT_BOOL
,
4281 .type
= QEMU_OPT_BOOL
,
4284 .type
= QEMU_OPT_STRING
,
4287 .type
= QEMU_OPT_NUMBER
,
4290 .type
= QEMU_OPT_SIZE
,
4293 .type
= QEMU_OPT_STRING
,
4296 .type
= QEMU_OPT_BOOL
,
4299 .type
= QEMU_OPT_STRING
,
4301 .name
= "logappend",
4302 .type
= QEMU_OPT_BOOL
,
4304 { /* end of list */ }
4310 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4311 ChardevBackend
*backend
,
4315 ChardevFile
*file
= backend
->u
.file
.data
;
4316 ChardevCommon
*common
= qapi_ChardevFile_base(file
);
4322 error_setg(errp
, "input file not supported");
4326 if (file
->has_append
&& file
->append
) {
4327 /* Append to file if it already exists. */
4328 accessmode
= FILE_GENERIC_WRITE
& ~FILE_WRITE_DATA
;
4329 flags
= OPEN_ALWAYS
;
4331 /* Truncate file if it already exists. */
4332 accessmode
= GENERIC_WRITE
;
4333 flags
= CREATE_ALWAYS
;
4336 out
= CreateFile(file
->out
, accessmode
, FILE_SHARE_READ
, NULL
, flags
,
4337 FILE_ATTRIBUTE_NORMAL
, NULL
);
4338 if (out
== INVALID_HANDLE_VALUE
) {
4339 error_setg(errp
, "open %s failed", file
->out
);
4342 return qemu_chr_open_win_file(out
, common
, errp
);
4345 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4346 ChardevBackend
*backend
,
4350 ChardevHostdev
*serial
= backend
->u
.serial
.data
;
4351 ChardevCommon
*common
= qapi_ChardevHostdev_base(serial
);
4352 return qemu_chr_open_win_path(serial
->device
, common
, errp
);
4357 static int qmp_chardev_open_file_source(char *src
, int flags
,
4362 TFR(fd
= qemu_open(src
, flags
, 0666));
4364 error_setg_file_open(errp
, errno
, src
);
4369 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4370 ChardevBackend
*backend
,
4374 ChardevFile
*file
= backend
->u
.file
.data
;
4375 ChardevCommon
*common
= qapi_ChardevFile_base(file
);
4376 int flags
, in
= -1, out
;
4378 flags
= O_WRONLY
| O_CREAT
| O_BINARY
;
4379 if (file
->has_append
&& file
->append
) {
4385 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4392 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4399 return qemu_chr_open_fd(in
, out
, common
, errp
);
4402 #ifdef HAVE_CHARDEV_SERIAL
4403 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4404 ChardevBackend
*backend
,
4408 ChardevHostdev
*serial
= backend
->u
.serial
.data
;
4409 ChardevCommon
*common
= qapi_ChardevHostdev_base(serial
);
4412 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4416 qemu_set_nonblock(fd
);
4417 return qemu_chr_open_tty_fd(fd
, common
, errp
);
4421 #ifdef HAVE_CHARDEV_PARPORT
4422 static CharDriverState
*qmp_chardev_open_parallel(const char *id
,
4423 ChardevBackend
*backend
,
4427 ChardevHostdev
*parallel
= backend
->u
.parallel
.data
;
4428 ChardevCommon
*common
= qapi_ChardevHostdev_base(parallel
);
4431 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4435 return qemu_chr_open_pp_fd(fd
, common
, errp
);
4441 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4443 CharDriverState
*chr
= opaque
;
4444 TCPCharDriver
*s
= chr
->opaque
;
4445 QIOChannelSocket
*sioc
;
4447 s
->reconnect_timer
= 0;
4453 sioc
= qio_channel_socket_new();
4454 qio_channel_socket_connect_async(sioc
, s
->addr
,
4455 qemu_chr_socket_connected
,
4461 static CharDriverState
*qmp_chardev_open_socket(const char *id
,
4462 ChardevBackend
*backend
,
4466 CharDriverState
*chr
;
4468 ChardevSocket
*sock
= backend
->u
.socket
.data
;
4469 SocketAddress
*addr
= sock
->addr
;
4470 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4471 bool is_listen
= sock
->has_server
? sock
->server
: true;
4472 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4473 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4474 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4475 ChardevCommon
*common
= qapi_ChardevSocket_base(sock
);
4476 QIOChannelSocket
*sioc
= NULL
;
4478 chr
= qemu_chr_alloc(common
, errp
);
4482 s
= g_new0(TCPCharDriver
, 1);
4484 s
->is_unix
= addr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
4485 s
->is_listen
= is_listen
;
4486 s
->is_telnet
= is_telnet
;
4487 s
->do_nodelay
= do_nodelay
;
4488 if (sock
->tls_creds
) {
4490 creds
= object_resolve_path_component(
4491 object_get_objects_root(), sock
->tls_creds
);
4493 error_setg(errp
, "No TLS credentials with id '%s'",
4497 s
->tls_creds
= (QCryptoTLSCreds
*)
4498 object_dynamic_cast(creds
,
4499 TYPE_QCRYPTO_TLS_CREDS
);
4500 if (!s
->tls_creds
) {
4501 error_setg(errp
, "Object with id '%s' is not TLS credentials",
4505 object_ref(OBJECT(s
->tls_creds
));
4507 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
4508 error_setg(errp
, "%s",
4509 "Expected TLS credentials for server endpoint");
4513 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
4514 error_setg(errp
, "%s",
4515 "Expected TLS credentials for client endpoint");
4521 s
->addr
= QAPI_CLONE(SocketAddress
, sock
->addr
);
4523 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
4525 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
4529 chr
->chr_wait_connected
= tcp_chr_wait_connected
;
4530 chr
->chr_write
= tcp_chr_write
;
4531 chr
->chr_sync_read
= tcp_chr_sync_read
;
4532 chr
->chr_close
= tcp_chr_close
;
4533 chr
->chr_disconnect
= tcp_chr_disconnect
;
4534 chr
->get_msgfds
= tcp_get_msgfds
;
4535 chr
->set_msgfds
= tcp_set_msgfds
;
4536 chr
->chr_add_client
= tcp_chr_add_client
;
4537 chr
->chr_add_watch
= tcp_chr_add_watch
;
4538 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4539 /* be isn't opened until we get a connection */
4540 chr
->explicit_be_open
= true;
4542 chr
->filename
= SocketAddress_to_str("disconnected:",
4543 addr
, is_listen
, is_telnet
);
4547 s
->do_telnetopt
= 1;
4549 } else if (reconnect
> 0) {
4550 s
->reconnect_time
= reconnect
;
4553 if (s
->reconnect_time
) {
4554 sioc
= qio_channel_socket_new();
4555 qio_channel_socket_connect_async(sioc
, s
->addr
,
4556 qemu_chr_socket_connected
,
4560 sioc
= qio_channel_socket_new();
4561 if (qio_channel_socket_listen_sync(sioc
, s
->addr
, errp
) < 0) {
4564 s
->listen_ioc
= sioc
;
4565 if (is_waitconnect
&&
4566 qemu_chr_wait_connected(chr
, errp
) < 0) {
4570 s
->listen_tag
= qio_channel_add_watch(
4571 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
,
4572 tcp_chr_accept
, chr
, NULL
);
4574 } else if (qemu_chr_wait_connected(chr
, errp
) < 0) {
4583 object_unref(OBJECT(sioc
));
4586 object_unref(OBJECT(s
->tls_creds
));
4589 qemu_chr_free_common(chr
);
4593 static CharDriverState
*qmp_chardev_open_udp(const char *id
,
4594 ChardevBackend
*backend
,
4598 ChardevUdp
*udp
= backend
->u
.udp
.data
;
4599 ChardevCommon
*common
= qapi_ChardevUdp_base(udp
);
4600 QIOChannelSocket
*sioc
= qio_channel_socket_new();
4602 if (qio_channel_socket_dgram_sync(sioc
,
4603 udp
->local
, udp
->remote
,
4605 object_unref(OBJECT(sioc
));
4608 return qemu_chr_open_udp(sioc
, common
, errp
);
4612 bool qemu_chr_has_feature(CharDriverState
*chr
,
4613 CharDriverFeature feature
)
4615 return test_bit(feature
, chr
->features
);
4618 void qemu_chr_set_feature(CharDriverState
*chr
,
4619 CharDriverFeature feature
)
4621 return set_bit(feature
, chr
->features
);
4624 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4627 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4628 CharDriverState
*chr
= NULL
;
4629 Error
*local_err
= NULL
;
4633 chr
= qemu_chr_find(id
);
4635 error_setg(errp
, "Chardev '%s' already exists", id
);
4640 for (i
= backends
; i
; i
= i
->next
) {
4643 if (cd
->kind
== backend
->type
) {
4644 chr
= cd
->create(id
, backend
, ret
, &local_err
);
4646 error_propagate(errp
, local_err
);
4655 error_setg(errp
, "chardev backend not available");
4659 chr
->label
= g_strdup(id
);
4660 chr
->avail_connections
=
4661 (backend
->type
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4662 if (!chr
->filename
) {
4663 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->type
]);
4665 if (!chr
->explicit_be_open
) {
4666 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4668 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4676 void qmp_chardev_remove(const char *id
, Error
**errp
)
4678 CharDriverState
*chr
;
4680 chr
= qemu_chr_find(id
);
4682 error_setg(errp
, "Chardev '%s' not found", id
);
4685 if (chr
->chr_can_read
|| chr
->chr_read
||
4686 chr
->chr_event
|| chr
->handler_opaque
) {
4687 error_setg(errp
, "Chardev '%s' is busy", id
);
4692 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
4695 qemu_chr_delete(chr
);
4698 void qemu_chr_cleanup(void)
4700 CharDriverState
*chr
, *tmp
;
4702 QTAILQ_FOREACH_SAFE(chr
, &chardevs
, next
, tmp
) {
4703 qemu_chr_delete(chr
);
4707 static void register_types(void)
4709 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
,
4710 qemu_chr_open_null
);
4711 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4712 qemu_chr_parse_socket
, qmp_chardev_open_socket
);
4713 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
,
4714 qmp_chardev_open_udp
);
4715 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4716 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4717 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4718 qemu_chr_parse_file_out
, qmp_chardev_open_file
);
4719 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4720 qemu_chr_parse_stdio
, qemu_chr_open_stdio
);
4721 #if defined HAVE_CHARDEV_SERIAL
4722 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4723 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4724 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4725 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4727 #ifdef HAVE_CHARDEV_PARPORT
4728 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4729 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4730 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4731 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4733 #ifdef HAVE_CHARDEV_PTY
4734 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
,
4738 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
,
4739 qemu_chr_open_win_con
);
4741 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4742 qemu_chr_parse_pipe
, qemu_chr_open_pipe
);
4743 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
,
4745 /* Bug-compatibility: */
4746 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4747 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4748 /* this must be done after machine init, since we register FEs with muxes
4749 * as part of realize functions like serial_isa_realizefn when -nographic
4752 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4755 type_init(register_types
);