4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "sysemu/sysemu.h"
27 #include "qemu/error-report.h"
28 #include "qemu/timer.h"
29 #include "sysemu/char.h"
31 #include "qmp-commands.h"
32 #include "qapi/qmp-input-visitor.h"
33 #include "qapi/qmp-output-visitor.h"
34 #include "qapi-visit.h"
35 #include "qemu/base64.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
54 #include <arpa/inet.h>
56 #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>
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip_icmp.h> // must come after ip.h
80 #include <netinet/udp.h>
81 #include <netinet/tcp.h>
86 #include "qemu/sockets.h"
87 #include "ui/qemu-spice.h"
89 #define READ_BUF_LEN 4096
90 #define READ_RETRIES 10
91 #define CHR_MAX_FILENAME_SIZE 256
92 #define TCP_MAX_FDS 16
94 /***********************************************************/
95 /* Socket address helpers */
97 static int SocketAddress_to_str(char *dest
, int max_len
,
98 const char *prefix
, SocketAddress
*addr
,
99 bool is_listen
, bool is_telnet
)
101 switch (addr
->type
) {
102 case SOCKET_ADDRESS_KIND_INET
:
103 return snprintf(dest
, max_len
, "%s%s:%s:%s%s", prefix
,
104 is_telnet
? "telnet" : "tcp", addr
->u
.inet
->host
,
105 addr
->u
.inet
->port
, is_listen
? ",server" : "");
107 case SOCKET_ADDRESS_KIND_UNIX
:
108 return snprintf(dest
, max_len
, "%sunix:%s%s", prefix
,
109 addr
->u
.q_unix
->path
, is_listen
? ",server" : "");
111 case SOCKET_ADDRESS_KIND_FD
:
112 return snprintf(dest
, max_len
, "%sfd:%s%s", prefix
, addr
->u
.fd
->str
,
113 is_listen
? ",server" : "");
120 static int sockaddr_to_str(char *dest
, int max_len
,
121 struct sockaddr_storage
*ss
, socklen_t ss_len
,
122 struct sockaddr_storage
*ps
, socklen_t ps_len
,
123 bool is_listen
, bool is_telnet
)
125 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
126 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
127 const char *left
= "", *right
= "";
129 switch (ss
->ss_family
) {
132 return snprintf(dest
, max_len
, "unix:%s%s",
133 ((struct sockaddr_un
*)(ss
))->sun_path
,
134 is_listen
? ",server" : "");
141 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
142 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
143 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
144 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
145 return snprintf(dest
, max_len
, "%s:%s%s%s:%s%s <-> %s%s%s:%s",
146 is_telnet
? "telnet" : "tcp",
147 left
, shost
, right
, sserv
,
148 is_listen
? ",server" : "",
149 left
, phost
, right
, pserv
);
152 return snprintf(dest
, max_len
, "unknown");
156 /***********************************************************/
157 /* character device */
159 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
160 QTAILQ_HEAD_INITIALIZER(chardevs
);
162 static void qemu_chr_free_common(CharDriverState
*chr
);
164 CharDriverState
*qemu_chr_alloc(ChardevCommon
*backend
, Error
**errp
)
166 CharDriverState
*chr
= g_malloc0(sizeof(CharDriverState
));
167 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
) {
233 } while (ret
== -1 && errno
== EAGAIN
);
242 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
246 qemu_mutex_lock(&s
->chr_write_lock
);
247 ret
= s
->chr_write(s
, buf
, len
);
250 qemu_chr_fe_write_log(s
, buf
, ret
);
253 qemu_mutex_unlock(&s
->chr_write_lock
);
257 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
262 qemu_mutex_lock(&s
->chr_write_lock
);
263 while (offset
< len
) {
265 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
266 if (res
== -1 && errno
== EAGAIN
) {
269 } while (res
== -1 && errno
== EAGAIN
);
278 qemu_chr_fe_write_log(s
, buf
, offset
);
281 qemu_mutex_unlock(&s
->chr_write_lock
);
289 int qemu_chr_fe_read_all(CharDriverState
*s
, uint8_t *buf
, int len
)
291 int offset
= 0, counter
= 10;
294 if (!s
->chr_sync_read
) {
298 while (offset
< len
) {
300 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
301 if (res
== -1 && errno
== EAGAIN
) {
304 } while (res
== -1 && errno
== EAGAIN
);
324 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
328 return s
->chr_ioctl(s
, cmd
, arg
);
331 int qemu_chr_be_can_write(CharDriverState
*s
)
333 if (!s
->chr_can_read
)
335 return s
->chr_can_read(s
->handler_opaque
);
338 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
341 s
->chr_read(s
->handler_opaque
, buf
, len
);
345 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
348 return (qemu_chr_fe_get_msgfds(s
, &fd
, 1) == 1) ? fd
: -1;
351 int qemu_chr_fe_get_msgfds(CharDriverState
*s
, int *fds
, int len
)
353 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
356 int qemu_chr_fe_set_msgfds(CharDriverState
*s
, int *fds
, int num
)
358 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
361 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
363 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
366 void qemu_chr_accept_input(CharDriverState
*s
)
368 if (s
->chr_accept_input
)
369 s
->chr_accept_input(s
);
373 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
375 char buf
[READ_BUF_LEN
];
378 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
379 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
383 static void remove_fd_in_watch(CharDriverState
*chr
);
385 void qemu_chr_add_handlers(CharDriverState
*s
,
386 IOCanReadHandler
*fd_can_read
,
387 IOReadHandler
*fd_read
,
388 IOEventHandler
*fd_event
,
393 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
395 remove_fd_in_watch(s
);
399 s
->chr_can_read
= fd_can_read
;
400 s
->chr_read
= fd_read
;
401 s
->chr_event
= fd_event
;
402 s
->handler_opaque
= opaque
;
403 if (fe_open
&& s
->chr_update_read_handler
)
404 s
->chr_update_read_handler(s
);
406 if (!s
->explicit_fe_open
) {
407 qemu_chr_fe_set_open(s
, fe_open
);
410 /* We're connecting to an already opened device, so let's make sure we
411 also get the open event */
412 if (fe_open
&& s
->be_open
) {
413 qemu_chr_be_generic_open(s
);
417 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
422 static CharDriverState
*qemu_chr_open_null(const char *id
,
423 ChardevBackend
*backend
,
427 CharDriverState
*chr
;
428 ChardevCommon
*common
= qapi_ChardevDummy_base(backend
->u
.null
);
430 chr
= qemu_chr_alloc(common
, errp
);
434 chr
->chr_write
= null_chr_write
;
435 chr
->explicit_be_open
= true;
439 /* MUX driver for serial I/O splitting */
441 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
442 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
444 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
445 IOReadHandler
*chr_read
[MAX_MUX
];
446 IOEventHandler
*chr_event
[MAX_MUX
];
447 void *ext_opaque
[MAX_MUX
];
448 CharDriverState
*drv
;
453 /* Intermediate input buffer allows to catch escape sequences even if the
454 currently active device is not accepting any input - but only until it
456 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
461 /* Protected by the CharDriverState chr_write_lock. */
463 int64_t timestamps_start
;
467 /* Called with chr_write_lock held. */
468 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
470 MuxDriver
*d
= chr
->opaque
;
472 if (!d
->timestamps
) {
473 ret
= qemu_chr_fe_write(d
->drv
, buf
, len
);
478 for (i
= 0; i
< len
; i
++) {
484 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
485 if (d
->timestamps_start
== -1)
486 d
->timestamps_start
= ti
;
487 ti
-= d
->timestamps_start
;
489 snprintf(buf1
, sizeof(buf1
),
490 "[%02d:%02d:%02d.%03d] ",
495 qemu_chr_fe_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
498 ret
+= qemu_chr_fe_write(d
->drv
, buf
+i
, 1);
499 if (buf
[i
] == '\n') {
507 static const char * const mux_help
[] = {
508 "% h print this help\n\r",
509 "% x exit emulator\n\r",
510 "% s save disk data back to file (if -snapshot)\n\r",
511 "% t toggle console timestamps\n\r",
512 "% b send break (magic sysrq)\n\r",
513 "% c switch between console and monitor\n\r",
518 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
519 static void mux_print_help(CharDriverState
*chr
)
522 char ebuf
[15] = "Escape-Char";
523 char cbuf
[50] = "\n\r";
525 if (term_escape_char
> 0 && term_escape_char
< 26) {
526 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
527 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
529 snprintf(cbuf
, sizeof(cbuf
),
530 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
533 qemu_chr_fe_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
534 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
535 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
536 if (mux_help
[i
][j
] == '%')
537 qemu_chr_fe_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
539 qemu_chr_fe_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
544 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
546 if (d
->chr_event
[mux_nr
])
547 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
550 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
552 if (d
->term_got_escape
) {
553 d
->term_got_escape
= 0;
554 if (ch
== term_escape_char
)
563 const char *term
= "QEMU: Terminated\n\r";
564 qemu_chr_fe_write(chr
, (uint8_t *)term
, strlen(term
));
572 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
575 /* Switch to the next registered device */
576 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
578 if (d
->focus
>= d
->mux_cnt
)
580 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
583 d
->timestamps
= !d
->timestamps
;
584 d
->timestamps_start
= -1;
588 } else if (ch
== term_escape_char
) {
589 d
->term_got_escape
= 1;
597 static void mux_chr_accept_input(CharDriverState
*chr
)
599 MuxDriver
*d
= chr
->opaque
;
602 while (d
->prod
[m
] != d
->cons
[m
] &&
603 d
->chr_can_read
[m
] &&
604 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
605 d
->chr_read
[m
](d
->ext_opaque
[m
],
606 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
610 static int mux_chr_can_read(void *opaque
)
612 CharDriverState
*chr
= opaque
;
613 MuxDriver
*d
= chr
->opaque
;
616 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
618 if (d
->chr_can_read
[m
])
619 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
623 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
625 CharDriverState
*chr
= opaque
;
626 MuxDriver
*d
= chr
->opaque
;
630 mux_chr_accept_input (opaque
);
632 for(i
= 0; i
< size
; i
++)
633 if (mux_proc_byte(chr
, d
, buf
[i
])) {
634 if (d
->prod
[m
] == d
->cons
[m
] &&
635 d
->chr_can_read
[m
] &&
636 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
637 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
639 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
643 static void mux_chr_event(void *opaque
, int event
)
645 CharDriverState
*chr
= opaque
;
646 MuxDriver
*d
= chr
->opaque
;
649 /* Send the event to all registered listeners */
650 for (i
= 0; i
< d
->mux_cnt
; i
++)
651 mux_chr_send_event(d
, i
, event
);
654 static void mux_chr_update_read_handler(CharDriverState
*chr
)
656 MuxDriver
*d
= chr
->opaque
;
658 if (d
->mux_cnt
>= MAX_MUX
) {
659 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
662 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
663 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
664 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
665 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
666 /* Fix up the real driver with mux routines */
667 if (d
->mux_cnt
== 0) {
668 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
671 if (d
->focus
!= -1) {
672 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
674 d
->focus
= d
->mux_cnt
;
676 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
679 static bool muxes_realized
;
682 * Called after processing of default and command-line-specified
683 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
684 * to a mux chardev. This is done here to ensure that
685 * output/prompts/banners are only displayed for the FE that has
686 * focus when initial command-line processing/machine init is
689 * After this point, any new FE attached to any new or existing
690 * mux will receive CHR_EVENT_OPENED notifications for the BE
693 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
695 CharDriverState
*chr
;
697 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
699 MuxDriver
*d
= chr
->opaque
;
702 /* send OPENED to all already-attached FEs */
703 for (i
= 0; i
< d
->mux_cnt
; i
++) {
704 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
706 /* mark mux as OPENED so any new FEs will immediately receive
709 qemu_chr_be_generic_open(chr
);
712 muxes_realized
= true;
715 static Notifier muxes_realize_notify
= {
716 .notify
= muxes_realize_done
,
719 static GSource
*mux_chr_add_watch(CharDriverState
*s
, GIOCondition cond
)
721 MuxDriver
*d
= s
->opaque
;
722 return d
->drv
->chr_add_watch(d
->drv
, cond
);
725 static CharDriverState
*qemu_chr_open_mux(const char *id
,
726 ChardevBackend
*backend
,
727 ChardevReturn
*ret
, Error
**errp
)
729 ChardevMux
*mux
= backend
->u
.mux
;
730 CharDriverState
*chr
, *drv
;
732 ChardevCommon
*common
= qapi_ChardevMux_base(backend
->u
.mux
);
734 drv
= qemu_chr_find(mux
->chardev
);
736 error_setg(errp
, "mux: base chardev %s not found", mux
->chardev
);
740 chr
= qemu_chr_alloc(common
, errp
);
744 d
= g_new0(MuxDriver
, 1);
749 chr
->chr_write
= mux_chr_write
;
750 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
751 chr
->chr_accept_input
= mux_chr_accept_input
;
752 /* Frontend guest-open / -close notification is not support with muxes */
753 chr
->chr_set_fe_open
= NULL
;
754 if (drv
->chr_add_watch
) {
755 chr
->chr_add_watch
= mux_chr_add_watch
;
757 /* only default to opened state if we've realized the initial
760 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
767 typedef struct IOWatchPoll
774 IOCanReadHandler
*fd_can_read
;
779 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
781 return container_of(source
, IOWatchPoll
, parent
);
784 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
786 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
787 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
788 bool was_active
= iwp
->src
!= NULL
;
789 if (was_active
== now_active
) {
794 iwp
->src
= g_io_create_watch(iwp
->channel
,
795 G_IO_IN
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
);
796 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
797 g_source_attach(iwp
->src
, NULL
);
799 g_source_destroy(iwp
->src
);
800 g_source_unref(iwp
->src
);
806 static gboolean
io_watch_poll_check(GSource
*source
)
811 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
817 static void io_watch_poll_finalize(GSource
*source
)
819 /* Due to a glib bug, removing the last reference to a source
820 * inside a finalize callback causes recursive locking (and a
821 * deadlock). This is not a problem inside other callbacks,
822 * including dispatch callbacks, so we call io_remove_watch_poll
823 * to remove this source. At this point, iwp->src must
824 * be NULL, or we would leak it.
826 * This would be solved much more elegantly by child sources,
827 * but we support older glib versions that do not have them.
829 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
830 assert(iwp
->src
== NULL
);
833 static GSourceFuncs io_watch_poll_funcs
= {
834 .prepare
= io_watch_poll_prepare
,
835 .check
= io_watch_poll_check
,
836 .dispatch
= io_watch_poll_dispatch
,
837 .finalize
= io_watch_poll_finalize
,
840 /* Can only be used for read */
841 static guint
io_add_watch_poll(GIOChannel
*channel
,
842 IOCanReadHandler
*fd_can_read
,
849 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
850 iwp
->fd_can_read
= fd_can_read
;
851 iwp
->opaque
= user_data
;
852 iwp
->channel
= channel
;
853 iwp
->fd_read
= (GSourceFunc
) fd_read
;
856 tag
= g_source_attach(&iwp
->parent
, NULL
);
857 g_source_unref(&iwp
->parent
);
861 static void io_remove_watch_poll(guint tag
)
866 g_return_if_fail (tag
> 0);
868 source
= g_main_context_find_source_by_id(NULL
, tag
);
869 g_return_if_fail (source
!= NULL
);
871 iwp
= io_watch_poll_from_source(source
);
873 g_source_destroy(iwp
->src
);
874 g_source_unref(iwp
->src
);
877 g_source_destroy(&iwp
->parent
);
880 static void remove_fd_in_watch(CharDriverState
*chr
)
882 if (chr
->fd_in_tag
) {
883 io_remove_watch_poll(chr
->fd_in_tag
);
889 static GIOChannel
*io_channel_from_fd(int fd
)
897 chan
= g_io_channel_unix_new(fd
);
899 g_io_channel_set_encoding(chan
, NULL
, NULL
);
900 g_io_channel_set_buffered(chan
, FALSE
);
906 static GIOChannel
*io_channel_from_socket(int fd
)
915 chan
= g_io_channel_win32_new_socket(fd
);
917 chan
= g_io_channel_unix_new(fd
);
920 g_io_channel_set_encoding(chan
, NULL
, NULL
);
921 g_io_channel_set_buffered(chan
, FALSE
);
926 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
929 GIOStatus status
= G_IO_STATUS_NORMAL
;
931 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
932 gsize bytes_written
= 0;
934 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
935 &bytes_written
, NULL
);
936 offset
+= bytes_written
;
943 case G_IO_STATUS_NORMAL
:
946 case G_IO_STATUS_AGAIN
:
958 typedef struct FDCharDriver
{
959 CharDriverState
*chr
;
960 GIOChannel
*fd_in
, *fd_out
;
964 /* Called with chr_write_lock held. */
965 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
967 FDCharDriver
*s
= chr
->opaque
;
969 return io_channel_send(s
->fd_out
, buf
, len
);
972 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
974 CharDriverState
*chr
= opaque
;
975 FDCharDriver
*s
= chr
->opaque
;
977 uint8_t buf
[READ_BUF_LEN
];
982 if (len
> s
->max_size
) {
989 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
990 len
, &bytes_read
, NULL
);
991 if (status
== G_IO_STATUS_EOF
) {
992 remove_fd_in_watch(chr
);
993 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
996 if (status
== G_IO_STATUS_NORMAL
) {
997 qemu_chr_be_write(chr
, buf
, bytes_read
);
1003 static int fd_chr_read_poll(void *opaque
)
1005 CharDriverState
*chr
= opaque
;
1006 FDCharDriver
*s
= chr
->opaque
;
1008 s
->max_size
= qemu_chr_be_can_write(chr
);
1012 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1014 FDCharDriver
*s
= chr
->opaque
;
1015 return g_io_create_watch(s
->fd_out
, cond
);
1018 static void fd_chr_update_read_handler(CharDriverState
*chr
)
1020 FDCharDriver
*s
= chr
->opaque
;
1022 remove_fd_in_watch(chr
);
1024 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
1029 static void fd_chr_close(struct CharDriverState
*chr
)
1031 FDCharDriver
*s
= chr
->opaque
;
1033 remove_fd_in_watch(chr
);
1035 g_io_channel_unref(s
->fd_in
);
1038 g_io_channel_unref(s
->fd_out
);
1042 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1045 /* open a character device to a unix fd */
1046 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
,
1047 ChardevCommon
*backend
, Error
**errp
)
1049 CharDriverState
*chr
;
1052 chr
= qemu_chr_alloc(backend
, errp
);
1056 s
= g_new0(FDCharDriver
, 1);
1057 s
->fd_in
= io_channel_from_fd(fd_in
);
1058 s
->fd_out
= io_channel_from_fd(fd_out
);
1059 qemu_set_nonblock(fd_out
);
1062 chr
->chr_add_watch
= fd_chr_add_watch
;
1063 chr
->chr_write
= fd_chr_write
;
1064 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
1065 chr
->chr_close
= fd_chr_close
;
1070 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
1071 ChardevBackend
*backend
,
1075 ChardevHostdev
*opts
= backend
->u
.pipe
;
1077 char filename_in
[CHR_MAX_FILENAME_SIZE
];
1078 char filename_out
[CHR_MAX_FILENAME_SIZE
];
1079 const char *filename
= opts
->device
;
1080 ChardevCommon
*common
= qapi_ChardevHostdev_base(backend
->u
.pipe
);
1082 snprintf(filename_in
, CHR_MAX_FILENAME_SIZE
, "%s.in", filename
);
1083 snprintf(filename_out
, CHR_MAX_FILENAME_SIZE
, "%s.out", filename
);
1084 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1085 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1086 if (fd_in
< 0 || fd_out
< 0) {
1091 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1093 error_setg_file_open(errp
, errno
, filename
);
1097 return qemu_chr_open_fd(fd_in
, fd_out
, common
, errp
);
1100 /* init terminal so that we can grab keys */
1101 static struct termios oldtty
;
1102 static int old_fd0_flags
;
1103 static bool stdio_in_use
;
1104 static bool stdio_allow_signal
;
1105 static bool stdio_echo_state
;
1107 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
);
1109 static void term_exit(void)
1111 tcsetattr (0, TCSANOW
, &oldtty
);
1112 fcntl(0, F_SETFL
, old_fd0_flags
);
1115 static void term_stdio_handler(int sig
)
1117 /* restore echo after resume from suspend. */
1118 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
1121 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1125 stdio_echo_state
= echo
;
1128 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1129 |INLCR
|IGNCR
|ICRNL
|IXON
);
1130 tty
.c_oflag
|= OPOST
;
1131 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1132 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1135 tty
.c_cc
[VTIME
] = 0;
1137 if (!stdio_allow_signal
)
1138 tty
.c_lflag
&= ~ISIG
;
1140 tcsetattr (0, TCSANOW
, &tty
);
1143 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1149 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
1150 ChardevBackend
*backend
,
1154 ChardevStdio
*opts
= backend
->u
.stdio
;
1155 CharDriverState
*chr
;
1156 struct sigaction act
;
1157 ChardevCommon
*common
= qapi_ChardevStdio_base(backend
->u
.stdio
);
1159 if (is_daemonized()) {
1160 error_setg(errp
, "cannot use stdio with -daemonize");
1165 error_setg(errp
, "cannot use stdio by multiple character devices");
1169 stdio_in_use
= true;
1170 old_fd0_flags
= fcntl(0, F_GETFL
);
1171 tcgetattr(0, &oldtty
);
1172 qemu_set_nonblock(0);
1175 memset(&act
, 0, sizeof(act
));
1176 act
.sa_handler
= term_stdio_handler
;
1177 sigaction(SIGCONT
, &act
, NULL
);
1179 chr
= qemu_chr_open_fd(0, 1, common
, errp
);
1180 chr
->chr_close
= qemu_chr_close_stdio
;
1181 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1182 if (opts
->has_signal
) {
1183 stdio_allow_signal
= opts
->signal
;
1185 qemu_chr_fe_set_echo(chr
, false);
1190 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1191 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1192 || defined(__GLIBC__)
1194 #define HAVE_CHARDEV_SERIAL 1
1195 #define HAVE_CHARDEV_PTY 1
1201 /* Protected by the CharDriverState chr_write_lock. */
1207 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1208 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1210 static gboolean
pty_chr_timer(gpointer opaque
)
1212 struct CharDriverState
*chr
= opaque
;
1213 PtyCharDriver
*s
= chr
->opaque
;
1215 qemu_mutex_lock(&chr
->chr_write_lock
);
1218 if (!s
->connected
) {
1220 pty_chr_update_read_handler_locked(chr
);
1222 qemu_mutex_unlock(&chr
->chr_write_lock
);
1226 /* Called with chr_write_lock held. */
1227 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1229 PtyCharDriver
*s
= chr
->opaque
;
1232 g_source_remove(s
->timer_tag
);
1237 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1239 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1243 /* Called with chr_write_lock held. */
1244 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1246 PtyCharDriver
*s
= chr
->opaque
;
1250 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1251 pfd
.events
= G_IO_OUT
;
1254 rc
= g_poll(&pfd
, 1, 0);
1255 } while (rc
== -1 && errno
== EINTR
);
1258 if (pfd
.revents
& G_IO_HUP
) {
1259 pty_chr_state(chr
, 0);
1261 pty_chr_state(chr
, 1);
1265 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1267 qemu_mutex_lock(&chr
->chr_write_lock
);
1268 pty_chr_update_read_handler_locked(chr
);
1269 qemu_mutex_unlock(&chr
->chr_write_lock
);
1272 /* Called with chr_write_lock held. */
1273 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1275 PtyCharDriver
*s
= chr
->opaque
;
1277 if (!s
->connected
) {
1278 /* guest sends data, check for (re-)connect */
1279 pty_chr_update_read_handler_locked(chr
);
1280 if (!s
->connected
) {
1284 return io_channel_send(s
->fd
, buf
, len
);
1287 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1289 PtyCharDriver
*s
= chr
->opaque
;
1290 if (!s
->connected
) {
1293 return g_io_create_watch(s
->fd
, cond
);
1296 static int pty_chr_read_poll(void *opaque
)
1298 CharDriverState
*chr
= opaque
;
1299 PtyCharDriver
*s
= chr
->opaque
;
1301 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1302 return s
->read_bytes
;
1305 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1307 CharDriverState
*chr
= opaque
;
1308 PtyCharDriver
*s
= chr
->opaque
;
1310 uint8_t buf
[READ_BUF_LEN
];
1314 if (len
> s
->read_bytes
)
1315 len
= s
->read_bytes
;
1319 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1320 if (status
!= G_IO_STATUS_NORMAL
) {
1321 pty_chr_state(chr
, 0);
1324 pty_chr_state(chr
, 1);
1325 qemu_chr_be_write(chr
, buf
, size
);
1330 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1332 CharDriverState
*chr
= opaque
;
1333 PtyCharDriver
*s
= chr
->opaque
;
1336 qemu_chr_be_generic_open(chr
);
1340 /* Called with chr_write_lock held. */
1341 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1343 PtyCharDriver
*s
= chr
->opaque
;
1347 g_source_remove(s
->open_tag
);
1350 remove_fd_in_watch(chr
);
1352 /* (re-)connect poll interval for idle guests: once per second.
1353 * We check more frequently in case the guests sends data to
1354 * the virtual device linked to our pty. */
1355 pty_chr_rearm_timer(chr
, 1000);
1358 g_source_remove(s
->timer_tag
);
1361 if (!s
->connected
) {
1362 g_assert(s
->open_tag
== 0);
1364 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1366 if (!chr
->fd_in_tag
) {
1367 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1373 static void pty_chr_close(struct CharDriverState
*chr
)
1375 PtyCharDriver
*s
= chr
->opaque
;
1378 qemu_mutex_lock(&chr
->chr_write_lock
);
1379 pty_chr_state(chr
, 0);
1380 fd
= g_io_channel_unix_get_fd(s
->fd
);
1381 g_io_channel_unref(s
->fd
);
1384 g_source_remove(s
->timer_tag
);
1387 qemu_mutex_unlock(&chr
->chr_write_lock
);
1389 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1392 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1393 ChardevBackend
*backend
,
1397 CharDriverState
*chr
;
1399 int master_fd
, slave_fd
;
1400 char pty_name
[PATH_MAX
];
1401 ChardevCommon
*common
= qapi_ChardevDummy_base(backend
->u
.pty
);
1403 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1404 if (master_fd
< 0) {
1405 error_setg_errno(errp
, errno
, "Failed to create PTY");
1410 qemu_set_nonblock(master_fd
);
1412 chr
= qemu_chr_alloc(common
, errp
);
1418 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1419 ret
->pty
= g_strdup(pty_name
);
1420 ret
->has_pty
= true;
1422 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1425 s
= g_new0(PtyCharDriver
, 1);
1427 chr
->chr_write
= pty_chr_write
;
1428 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1429 chr
->chr_close
= pty_chr_close
;
1430 chr
->chr_add_watch
= pty_chr_add_watch
;
1431 chr
->explicit_be_open
= true;
1433 s
->fd
= io_channel_from_fd(master_fd
);
1439 static void tty_serial_init(int fd
, int speed
,
1440 int parity
, int data_bits
, int stop_bits
)
1446 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1447 speed
, parity
, data_bits
, stop_bits
);
1449 tcgetattr (fd
, &tty
);
1451 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1452 speed
= speed
* 10 / 11;
1469 /* Non-Posix values follow. They may be unsupported on some systems. */
1471 check_speed(115200);
1473 check_speed(230400);
1476 check_speed(460800);
1479 check_speed(500000);
1482 check_speed(576000);
1485 check_speed(921600);
1488 check_speed(1000000);
1491 check_speed(1152000);
1494 check_speed(1500000);
1497 check_speed(2000000);
1500 check_speed(2500000);
1503 check_speed(3000000);
1506 check_speed(3500000);
1509 check_speed(4000000);
1514 cfsetispeed(&tty
, spd
);
1515 cfsetospeed(&tty
, spd
);
1517 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1518 |INLCR
|IGNCR
|ICRNL
|IXON
);
1519 tty
.c_oflag
|= OPOST
;
1520 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1521 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1542 tty
.c_cflag
|= PARENB
;
1545 tty
.c_cflag
|= PARENB
| PARODD
;
1549 tty
.c_cflag
|= CSTOPB
;
1551 tcsetattr (fd
, TCSANOW
, &tty
);
1554 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1556 FDCharDriver
*s
= chr
->opaque
;
1559 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1561 QEMUSerialSetParams
*ssp
= arg
;
1562 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1563 ssp
->speed
, ssp
->parity
,
1564 ssp
->data_bits
, ssp
->stop_bits
);
1567 case CHR_IOCTL_SERIAL_SET_BREAK
:
1569 int enable
= *(int *)arg
;
1571 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1575 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1578 int *targ
= (int *)arg
;
1579 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1581 if (sarg
& TIOCM_CTS
)
1582 *targ
|= CHR_TIOCM_CTS
;
1583 if (sarg
& TIOCM_CAR
)
1584 *targ
|= CHR_TIOCM_CAR
;
1585 if (sarg
& TIOCM_DSR
)
1586 *targ
|= CHR_TIOCM_DSR
;
1587 if (sarg
& TIOCM_RI
)
1588 *targ
|= CHR_TIOCM_RI
;
1589 if (sarg
& TIOCM_DTR
)
1590 *targ
|= CHR_TIOCM_DTR
;
1591 if (sarg
& TIOCM_RTS
)
1592 *targ
|= CHR_TIOCM_RTS
;
1595 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1597 int sarg
= *(int *)arg
;
1599 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1600 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1601 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1602 if (sarg
& CHR_TIOCM_CTS
)
1604 if (sarg
& CHR_TIOCM_CAR
)
1606 if (sarg
& CHR_TIOCM_DSR
)
1608 if (sarg
& CHR_TIOCM_RI
)
1610 if (sarg
& CHR_TIOCM_DTR
)
1612 if (sarg
& CHR_TIOCM_RTS
)
1614 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1623 static void qemu_chr_close_tty(CharDriverState
*chr
)
1625 FDCharDriver
*s
= chr
->opaque
;
1629 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1639 static CharDriverState
*qemu_chr_open_tty_fd(int fd
,
1640 ChardevCommon
*backend
,
1643 CharDriverState
*chr
;
1645 tty_serial_init(fd
, 115200, 'N', 8, 1);
1646 chr
= qemu_chr_open_fd(fd
, fd
, backend
, errp
);
1647 chr
->chr_ioctl
= tty_serial_ioctl
;
1648 chr
->chr_close
= qemu_chr_close_tty
;
1651 #endif /* __linux__ || __sun__ */
1653 #if defined(__linux__)
1655 #define HAVE_CHARDEV_PARPORT 1
1660 } ParallelCharDriver
;
1662 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1664 if (s
->mode
!= mode
) {
1666 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1673 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1675 ParallelCharDriver
*drv
= chr
->opaque
;
1680 case CHR_IOCTL_PP_READ_DATA
:
1681 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1683 *(uint8_t *)arg
= b
;
1685 case CHR_IOCTL_PP_WRITE_DATA
:
1686 b
= *(uint8_t *)arg
;
1687 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1690 case CHR_IOCTL_PP_READ_CONTROL
:
1691 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1693 /* Linux gives only the lowest bits, and no way to know data
1694 direction! For better compatibility set the fixed upper
1696 *(uint8_t *)arg
= b
| 0xc0;
1698 case CHR_IOCTL_PP_WRITE_CONTROL
:
1699 b
= *(uint8_t *)arg
;
1700 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1703 case CHR_IOCTL_PP_READ_STATUS
:
1704 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1706 *(uint8_t *)arg
= b
;
1708 case CHR_IOCTL_PP_DATA_DIR
:
1709 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1712 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1713 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1714 struct ParallelIOArg
*parg
= arg
;
1715 int n
= read(fd
, parg
->buffer
, parg
->count
);
1716 if (n
!= parg
->count
) {
1721 case CHR_IOCTL_PP_EPP_READ
:
1722 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1723 struct ParallelIOArg
*parg
= arg
;
1724 int n
= read(fd
, parg
->buffer
, parg
->count
);
1725 if (n
!= parg
->count
) {
1730 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1731 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1732 struct ParallelIOArg
*parg
= arg
;
1733 int n
= write(fd
, parg
->buffer
, parg
->count
);
1734 if (n
!= parg
->count
) {
1739 case CHR_IOCTL_PP_EPP_WRITE
:
1740 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1741 struct ParallelIOArg
*parg
= arg
;
1742 int n
= write(fd
, parg
->buffer
, parg
->count
);
1743 if (n
!= parg
->count
) {
1754 static void pp_close(CharDriverState
*chr
)
1756 ParallelCharDriver
*drv
= chr
->opaque
;
1759 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1760 ioctl(fd
, PPRELEASE
);
1763 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1766 static CharDriverState
*qemu_chr_open_pp_fd(int fd
,
1767 ChardevCommon
*backend
,
1770 CharDriverState
*chr
;
1771 ParallelCharDriver
*drv
;
1773 if (ioctl(fd
, PPCLAIM
) < 0) {
1774 error_setg_errno(errp
, errno
, "not a parallel port");
1779 drv
= g_new0(ParallelCharDriver
, 1);
1781 drv
->mode
= IEEE1284_MODE_COMPAT
;
1783 chr
= qemu_chr_alloc(backend
, errp
);
1787 chr
->chr_write
= null_chr_write
;
1788 chr
->chr_ioctl
= pp_ioctl
;
1789 chr
->chr_close
= pp_close
;
1794 #endif /* __linux__ */
1796 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1798 #define HAVE_CHARDEV_PARPORT 1
1800 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1802 int fd
= (int)(intptr_t)chr
->opaque
;
1806 case CHR_IOCTL_PP_READ_DATA
:
1807 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1809 *(uint8_t *)arg
= b
;
1811 case CHR_IOCTL_PP_WRITE_DATA
:
1812 b
= *(uint8_t *)arg
;
1813 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1816 case CHR_IOCTL_PP_READ_CONTROL
:
1817 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1819 *(uint8_t *)arg
= b
;
1821 case CHR_IOCTL_PP_WRITE_CONTROL
:
1822 b
= *(uint8_t *)arg
;
1823 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1826 case CHR_IOCTL_PP_READ_STATUS
:
1827 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1829 *(uint8_t *)arg
= b
;
1837 static CharDriverState
*qemu_chr_open_pp_fd(int fd
,
1838 ChardevBackend
*backend
,
1841 CharDriverState
*chr
;
1843 chr
= qemu_chr_alloc(common
, errp
);
1847 chr
->opaque
= (void *)(intptr_t)fd
;
1848 chr
->chr_write
= null_chr_write
;
1849 chr
->chr_ioctl
= pp_ioctl
;
1850 chr
->explicit_be_open
= true;
1857 #define HAVE_CHARDEV_SERIAL 1
1861 HANDLE hcom
, hrecv
, hsend
;
1866 /* Protected by the CharDriverState chr_write_lock. */
1872 HANDLE hInputReadyEvent
;
1873 HANDLE hInputDoneEvent
;
1874 HANDLE hInputThread
;
1875 uint8_t win_stdio_buf
;
1876 } WinStdioCharState
;
1878 #define NSENDBUF 2048
1879 #define NRECVBUF 2048
1880 #define MAXCONNECT 1
1881 #define NTIMEOUT 5000
1883 static int win_chr_poll(void *opaque
);
1884 static int win_chr_pipe_poll(void *opaque
);
1886 static void win_chr_close(CharDriverState
*chr
)
1888 WinCharState
*s
= chr
->opaque
;
1891 CloseHandle(s
->hsend
);
1895 CloseHandle(s
->hrecv
);
1899 CloseHandle(s
->hcom
);
1903 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1905 qemu_del_polling_cb(win_chr_poll
, chr
);
1907 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1910 static int win_chr_init(CharDriverState
*chr
, const char *filename
, Error
**errp
)
1912 WinCharState
*s
= chr
->opaque
;
1914 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1919 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1921 error_setg(errp
, "Failed CreateEvent");
1924 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1926 error_setg(errp
, "Failed CreateEvent");
1930 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1931 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1932 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1933 error_setg(errp
, "Failed CreateFile (%lu)", GetLastError());
1938 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1939 error_setg(errp
, "Failed SetupComm");
1943 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1944 size
= sizeof(COMMCONFIG
);
1945 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1946 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1947 CommConfigDialog(filename
, NULL
, &comcfg
);
1949 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1950 error_setg(errp
, "Failed SetCommState");
1954 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1955 error_setg(errp
, "Failed SetCommMask");
1959 cto
.ReadIntervalTimeout
= MAXDWORD
;
1960 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1961 error_setg(errp
, "Failed SetCommTimeouts");
1965 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1966 error_setg(errp
, "Failed ClearCommError");
1969 qemu_add_polling_cb(win_chr_poll
, chr
);
1977 /* Called with chr_write_lock held. */
1978 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1980 WinCharState
*s
= chr
->opaque
;
1981 DWORD len
, ret
, size
, err
;
1984 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1985 s
->osend
.hEvent
= s
->hsend
;
1988 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1990 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1992 err
= GetLastError();
1993 if (err
== ERROR_IO_PENDING
) {
1994 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
2012 static int win_chr_read_poll(CharDriverState
*chr
)
2014 WinCharState
*s
= chr
->opaque
;
2016 s
->max_size
= qemu_chr_be_can_write(chr
);
2020 static void win_chr_readfile(CharDriverState
*chr
)
2022 WinCharState
*s
= chr
->opaque
;
2024 uint8_t buf
[READ_BUF_LEN
];
2027 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
2028 s
->orecv
.hEvent
= s
->hrecv
;
2029 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
2031 err
= GetLastError();
2032 if (err
== ERROR_IO_PENDING
) {
2033 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
2038 qemu_chr_be_write(chr
, buf
, size
);
2042 static void win_chr_read(CharDriverState
*chr
)
2044 WinCharState
*s
= chr
->opaque
;
2046 if (s
->len
> s
->max_size
)
2047 s
->len
= s
->max_size
;
2051 win_chr_readfile(chr
);
2054 static int win_chr_poll(void *opaque
)
2056 CharDriverState
*chr
= opaque
;
2057 WinCharState
*s
= chr
->opaque
;
2061 ClearCommError(s
->hcom
, &comerr
, &status
);
2062 if (status
.cbInQue
> 0) {
2063 s
->len
= status
.cbInQue
;
2064 win_chr_read_poll(chr
);
2071 static CharDriverState
*qemu_chr_open_win_path(const char *filename
,
2072 ChardevCommon
*backend
,
2075 CharDriverState
*chr
;
2078 chr
= qemu_chr_alloc(backend
, errp
);
2082 s
= g_new0(WinCharState
, 1);
2084 chr
->chr_write
= win_chr_write
;
2085 chr
->chr_close
= win_chr_close
;
2087 if (win_chr_init(chr
, filename
, errp
) < 0) {
2089 qemu_chr_free_common(chr
);
2095 static int win_chr_pipe_poll(void *opaque
)
2097 CharDriverState
*chr
= opaque
;
2098 WinCharState
*s
= chr
->opaque
;
2101 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
2104 win_chr_read_poll(chr
);
2111 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
,
2114 WinCharState
*s
= chr
->opaque
;
2118 char openname
[CHR_MAX_FILENAME_SIZE
];
2122 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2124 error_setg(errp
, "Failed CreateEvent");
2127 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2129 error_setg(errp
, "Failed CreateEvent");
2133 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
2134 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
2135 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
2137 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
2138 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
2139 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
2144 ZeroMemory(&ov
, sizeof(ov
));
2145 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
2146 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
2148 error_setg(errp
, "Failed ConnectNamedPipe");
2152 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2154 error_setg(errp
, "Failed GetOverlappedResult");
2156 CloseHandle(ov
.hEvent
);
2163 CloseHandle(ov
.hEvent
);
2166 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2175 static CharDriverState
*qemu_chr_open_pipe(const char *id
,
2176 ChardevBackend
*backend
,
2180 ChardevHostdev
*opts
= backend
->u
.pipe
;
2181 const char *filename
= opts
->device
;
2182 CharDriverState
*chr
;
2184 ChardevCommon
*common
= qapi_ChardevHostdev_base(backend
->u
.pipe
);
2186 chr
= qemu_chr_alloc(common
, errp
);
2190 s
= g_new0(WinCharState
, 1);
2192 chr
->chr_write
= win_chr_write
;
2193 chr
->chr_close
= win_chr_close
;
2195 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
2197 qemu_chr_free_common(chr
);
2203 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
,
2204 ChardevCommon
*backend
,
2207 CharDriverState
*chr
;
2210 chr
= qemu_chr_alloc(backend
, errp
);
2214 s
= g_new0(WinCharState
, 1);
2217 chr
->chr_write
= win_chr_write
;
2221 static CharDriverState
*qemu_chr_open_win_con(const char *id
,
2222 ChardevBackend
*backend
,
2226 ChardevCommon
*common
= qapi_ChardevDummy_base(backend
->u
.console
);
2227 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
),
2231 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2233 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2240 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2250 static void win_stdio_wait_func(void *opaque
)
2252 CharDriverState
*chr
= opaque
;
2253 WinStdioCharState
*stdio
= chr
->opaque
;
2254 INPUT_RECORD buf
[4];
2259 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2262 /* Avoid error storm */
2263 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2267 for (i
= 0; i
< dwSize
; i
++) {
2268 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2270 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2272 if (kev
->uChar
.AsciiChar
!= 0) {
2273 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2274 if (qemu_chr_be_can_write(chr
)) {
2275 uint8_t c
= kev
->uChar
.AsciiChar
;
2276 qemu_chr_be_write(chr
, &c
, 1);
2284 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2286 CharDriverState
*chr
= param
;
2287 WinStdioCharState
*stdio
= chr
->opaque
;
2293 /* Wait for one byte */
2294 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2296 /* Exit in case of error, continue if nothing read */
2304 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2305 if (stdio
->win_stdio_buf
== '\r') {
2309 /* Signal the main thread and wait until the byte was eaten */
2310 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2313 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2319 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2323 static void win_stdio_thread_wait_func(void *opaque
)
2325 CharDriverState
*chr
= opaque
;
2326 WinStdioCharState
*stdio
= chr
->opaque
;
2328 if (qemu_chr_be_can_write(chr
)) {
2329 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2332 SetEvent(stdio
->hInputDoneEvent
);
2335 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2337 WinStdioCharState
*stdio
= chr
->opaque
;
2340 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2343 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2345 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2349 static void win_stdio_close(CharDriverState
*chr
)
2351 WinStdioCharState
*stdio
= chr
->opaque
;
2353 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2354 CloseHandle(stdio
->hInputReadyEvent
);
2356 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2357 CloseHandle(stdio
->hInputDoneEvent
);
2359 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2360 TerminateThread(stdio
->hInputThread
, 0);
2363 g_free(chr
->opaque
);
2367 static CharDriverState
*qemu_chr_open_stdio(const char *id
,
2368 ChardevBackend
*backend
,
2372 CharDriverState
*chr
;
2373 WinStdioCharState
*stdio
;
2376 ChardevCommon
*common
= qapi_ChardevStdio_base(backend
->u
.stdio
);
2378 chr
= qemu_chr_alloc(common
, errp
);
2382 stdio
= g_new0(WinStdioCharState
, 1);
2384 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2385 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2386 error_setg(errp
, "cannot open stdio: invalid handle");
2390 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2392 chr
->opaque
= stdio
;
2393 chr
->chr_write
= win_stdio_write
;
2394 chr
->chr_close
= win_stdio_close
;
2397 if (qemu_add_wait_object(stdio
->hStdIn
,
2398 win_stdio_wait_func
, chr
)) {
2399 error_setg(errp
, "qemu_add_wait_object: failed");
2405 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2406 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2407 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2408 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2409 error_setg(errp
, "cannot create event");
2412 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2413 win_stdio_thread_wait_func
, chr
)) {
2414 error_setg(errp
, "qemu_add_wait_object: failed");
2417 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2420 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
2421 error_setg(errp
, "cannot create stdio thread");
2426 dwMode
|= ENABLE_LINE_INPUT
;
2429 /* set the terminal in raw mode */
2430 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2431 dwMode
|= ENABLE_PROCESSED_INPUT
;
2434 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2436 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2437 qemu_chr_fe_set_echo(chr
, false);
2442 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2444 CloseHandle(stdio
->hInputReadyEvent
);
2445 CloseHandle(stdio
->hInputDoneEvent
);
2447 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2450 #endif /* !_WIN32 */
2453 /***********************************************************/
2454 /* UDP Net console */
2459 uint8_t buf
[READ_BUF_LEN
];
2465 /* Called with chr_write_lock held. */
2466 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2468 NetCharDriver
*s
= chr
->opaque
;
2469 gsize bytes_written
;
2472 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2473 if (status
== G_IO_STATUS_EOF
) {
2475 } else if (status
!= G_IO_STATUS_NORMAL
) {
2479 return bytes_written
;
2482 static int udp_chr_read_poll(void *opaque
)
2484 CharDriverState
*chr
= opaque
;
2485 NetCharDriver
*s
= chr
->opaque
;
2487 s
->max_size
= qemu_chr_be_can_write(chr
);
2489 /* If there were any stray characters in the queue process them
2492 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2493 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2495 s
->max_size
= qemu_chr_be_can_write(chr
);
2500 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2502 CharDriverState
*chr
= opaque
;
2503 NetCharDriver
*s
= chr
->opaque
;
2504 gsize bytes_read
= 0;
2507 if (s
->max_size
== 0) {
2510 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2512 s
->bufcnt
= bytes_read
;
2513 s
->bufptr
= s
->bufcnt
;
2514 if (status
!= G_IO_STATUS_NORMAL
) {
2515 remove_fd_in_watch(chr
);
2520 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2521 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2523 s
->max_size
= qemu_chr_be_can_write(chr
);
2529 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2531 NetCharDriver
*s
= chr
->opaque
;
2533 remove_fd_in_watch(chr
);
2535 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2540 static void udp_chr_close(CharDriverState
*chr
)
2542 NetCharDriver
*s
= chr
->opaque
;
2544 remove_fd_in_watch(chr
);
2546 g_io_channel_unref(s
->chan
);
2550 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2553 static CharDriverState
*qemu_chr_open_udp_fd(int fd
,
2554 ChardevCommon
*backend
,
2557 CharDriverState
*chr
= NULL
;
2558 NetCharDriver
*s
= NULL
;
2560 chr
= qemu_chr_alloc(backend
, errp
);
2564 s
= g_new0(NetCharDriver
, 1);
2567 s
->chan
= io_channel_from_socket(s
->fd
);
2571 chr
->chr_write
= udp_chr_write
;
2572 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2573 chr
->chr_close
= udp_chr_close
;
2574 /* be isn't opened until we get a connection */
2575 chr
->explicit_be_open
= true;
2579 /***********************************************************/
2580 /* TCP Net console */
2584 GIOChannel
*chan
, *listen_chan
;
2593 int read_msgfds_num
;
2595 int write_msgfds_num
;
2597 SocketAddress
*addr
;
2601 guint reconnect_timer
;
2602 int64_t reconnect_time
;
2603 bool connect_err_reported
;
2606 static gboolean
socket_reconnect_timeout(gpointer opaque
);
2608 static void qemu_chr_socket_restart_timer(CharDriverState
*chr
)
2610 TCPCharDriver
*s
= chr
->opaque
;
2611 assert(s
->connected
== 0);
2612 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
2613 socket_reconnect_timeout
, chr
);
2616 static void check_report_connect_error(CharDriverState
*chr
,
2619 TCPCharDriver
*s
= chr
->opaque
;
2621 if (!s
->connect_err_reported
) {
2622 error_report("Unable to connect character device %s: %s",
2623 chr
->label
, error_get_pretty(err
));
2624 s
->connect_err_reported
= true;
2626 qemu_chr_socket_restart_timer(chr
);
2629 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2632 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2634 TCPCharDriver
*s
= chr
->opaque
;
2639 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2640 char control
[CMSG_SPACE(fd_size
)];
2641 struct cmsghdr
*cmsg
;
2643 memset(&msgh
, 0, sizeof(msgh
));
2644 memset(control
, 0, sizeof(control
));
2646 /* set the payload */
2647 iov
.iov_base
= (uint8_t *) buf
;
2650 msgh
.msg_iov
= &iov
;
2651 msgh
.msg_iovlen
= 1;
2653 msgh
.msg_control
= control
;
2654 msgh
.msg_controllen
= sizeof(control
);
2656 cmsg
= CMSG_FIRSTHDR(&msgh
);
2658 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2659 cmsg
->cmsg_level
= SOL_SOCKET
;
2660 cmsg
->cmsg_type
= SCM_RIGHTS
;
2661 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2664 r
= sendmsg(s
->fd
, &msgh
, 0);
2665 } while (r
< 0 && errno
== EINTR
);
2667 /* free the written msgfds, no matter what */
2668 if (s
->write_msgfds_num
) {
2669 g_free(s
->write_msgfds
);
2670 s
->write_msgfds
= 0;
2671 s
->write_msgfds_num
= 0;
2678 /* Called with chr_write_lock held. */
2679 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2681 TCPCharDriver
*s
= chr
->opaque
;
2684 if (s
->is_unix
&& s
->write_msgfds_num
) {
2685 return unix_send_msgfds(chr
, buf
, len
);
2689 return io_channel_send(s
->chan
, buf
, len
);
2692 /* XXX: indicate an error ? */
2697 static int tcp_chr_read_poll(void *opaque
)
2699 CharDriverState
*chr
= opaque
;
2700 TCPCharDriver
*s
= chr
->opaque
;
2703 s
->max_size
= qemu_chr_be_can_write(chr
);
2708 #define IAC_BREAK 243
2709 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2711 uint8_t *buf
, int *size
)
2713 /* Handle any telnet client's basic IAC options to satisfy char by
2714 * char mode with no echo. All IAC options will be removed from
2715 * the buf and the do_telnetopt variable will be used to track the
2716 * state of the width of the IAC information.
2718 * IAC commands come in sets of 3 bytes with the exception of the
2719 * "IAC BREAK" command and the double IAC.
2725 for (i
= 0; i
< *size
; i
++) {
2726 if (s
->do_telnetopt
> 1) {
2727 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2728 /* Double IAC means send an IAC */
2732 s
->do_telnetopt
= 1;
2734 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2735 /* Handle IAC break commands by sending a serial break */
2736 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2741 if (s
->do_telnetopt
>= 4) {
2742 s
->do_telnetopt
= 1;
2745 if ((unsigned char)buf
[i
] == IAC
) {
2746 s
->do_telnetopt
= 2;
2757 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2759 TCPCharDriver
*s
= chr
->opaque
;
2760 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2762 assert(num
<= TCP_MAX_FDS
);
2767 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2769 /* Close unused fds */
2770 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2771 close(s
->read_msgfds
[i
]);
2774 g_free(s
->read_msgfds
);
2776 s
->read_msgfds_num
= 0;
2782 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2784 TCPCharDriver
*s
= chr
->opaque
;
2786 /* clear old pending fd array */
2787 g_free(s
->write_msgfds
);
2790 s
->write_msgfds
= g_new(int, num
);
2791 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2794 s
->write_msgfds_num
= num
;
2800 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2802 TCPCharDriver
*s
= chr
->opaque
;
2803 struct cmsghdr
*cmsg
;
2805 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2808 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2809 cmsg
->cmsg_level
!= SOL_SOCKET
||
2810 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2814 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2820 /* close and clean read_msgfds */
2821 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2822 close(s
->read_msgfds
[i
]);
2825 if (s
->read_msgfds_num
) {
2826 g_free(s
->read_msgfds
);
2829 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2830 s
->read_msgfds
= g_malloc(fd_size
);
2831 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2833 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2834 int fd
= s
->read_msgfds
[i
];
2839 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2842 #ifndef MSG_CMSG_CLOEXEC
2843 qemu_set_cloexec(fd
);
2849 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2851 TCPCharDriver
*s
= chr
->opaque
;
2852 struct msghdr msg
= { NULL
, };
2853 struct iovec iov
[1];
2855 struct cmsghdr cmsg
;
2856 char control
[CMSG_SPACE(sizeof(int) * TCP_MAX_FDS
)];
2861 iov
[0].iov_base
= buf
;
2862 iov
[0].iov_len
= len
;
2866 msg
.msg_control
= &msg_control
;
2867 msg
.msg_controllen
= sizeof(msg_control
);
2869 #ifdef MSG_CMSG_CLOEXEC
2870 flags
|= MSG_CMSG_CLOEXEC
;
2873 ret
= recvmsg(s
->fd
, &msg
, flags
);
2874 } while (ret
== -1 && errno
== EINTR
);
2876 if (ret
> 0 && s
->is_unix
) {
2877 unix_process_msgfd(chr
, &msg
);
2883 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2885 TCPCharDriver
*s
= chr
->opaque
;
2889 ret
= qemu_recv(s
->fd
, buf
, len
, 0);
2890 } while (ret
== -1 && socket_error() == EINTR
);
2896 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2898 TCPCharDriver
*s
= chr
->opaque
;
2899 return g_io_create_watch(s
->chan
, cond
);
2902 static void tcp_chr_disconnect(CharDriverState
*chr
)
2904 TCPCharDriver
*s
= chr
->opaque
;
2907 if (s
->listen_chan
) {
2908 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2909 tcp_chr_accept
, chr
);
2911 remove_fd_in_watch(chr
);
2912 g_io_channel_unref(s
->chan
);
2916 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2917 "disconnected:", s
->addr
, s
->is_listen
, s
->is_telnet
);
2918 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2919 if (s
->reconnect_time
) {
2920 qemu_chr_socket_restart_timer(chr
);
2924 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2926 CharDriverState
*chr
= opaque
;
2927 TCPCharDriver
*s
= chr
->opaque
;
2928 uint8_t buf
[READ_BUF_LEN
];
2931 if (!s
->connected
|| s
->max_size
<= 0) {
2935 if (len
> s
->max_size
)
2937 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2940 socket_error() != EAGAIN
&& socket_error() != EWOULDBLOCK
)) {
2941 /* connection closed */
2942 tcp_chr_disconnect(chr
);
2943 } else if (size
> 0) {
2944 if (s
->do_telnetopt
)
2945 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2947 qemu_chr_be_write(chr
, buf
, size
);
2953 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2955 TCPCharDriver
*s
= chr
->opaque
;
2958 if (!s
->connected
) {
2962 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2964 /* connection closed */
2965 tcp_chr_disconnect(chr
);
2972 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2974 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
, NULL
, NULL
);
2977 chr
->avail_connections
= 1;
2984 static void tcp_chr_connect(void *opaque
)
2986 CharDriverState
*chr
= opaque
;
2987 TCPCharDriver
*s
= chr
->opaque
;
2988 struct sockaddr_storage ss
, ps
;
2989 socklen_t ss_len
= sizeof(ss
), ps_len
= sizeof(ps
);
2991 memset(&ss
, 0, ss_len
);
2992 if (getsockname(s
->fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2993 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2994 "Error in getsockname: %s\n", strerror(errno
));
2995 } else if (getpeername(s
->fd
, (struct sockaddr
*) &ps
, &ps_len
) != 0) {
2996 snprintf(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
2997 "Error in getpeername: %s\n", strerror(errno
));
2999 sockaddr_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
,
3000 &ss
, ss_len
, &ps
, ps_len
,
3001 s
->is_listen
, s
->is_telnet
);
3006 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
3009 qemu_chr_be_generic_open(chr
);
3012 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
3014 TCPCharDriver
*s
= chr
->opaque
;
3016 remove_fd_in_watch(chr
);
3018 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
3023 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3024 static void tcp_chr_telnet_init(int fd
)
3027 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3028 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3029 send(fd
, (char *)buf
, 3, 0);
3030 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3031 send(fd
, (char *)buf
, 3, 0);
3032 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3033 send(fd
, (char *)buf
, 3, 0);
3034 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3035 send(fd
, (char *)buf
, 3, 0);
3038 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
3040 TCPCharDriver
*s
= chr
->opaque
;
3044 qemu_set_nonblock(fd
);
3046 socket_set_nodelay(fd
);
3048 s
->chan
= io_channel_from_socket(fd
);
3049 if (s
->listen_tag
) {
3050 g_source_remove(s
->listen_tag
);
3053 tcp_chr_connect(chr
);
3058 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
3060 CharDriverState
*chr
= opaque
;
3061 TCPCharDriver
*s
= chr
->opaque
;
3062 struct sockaddr_in saddr
;
3064 struct sockaddr_un uaddr
;
3066 struct sockaddr
*addr
;
3073 len
= sizeof(uaddr
);
3074 addr
= (struct sockaddr
*)&uaddr
;
3078 len
= sizeof(saddr
);
3079 addr
= (struct sockaddr
*)&saddr
;
3081 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
3082 if (fd
< 0 && errno
!= EINTR
) {
3085 } else if (fd
>= 0) {
3086 if (s
->do_telnetopt
)
3087 tcp_chr_telnet_init(fd
);
3091 if (tcp_chr_add_client(chr
, fd
) < 0)
3097 static void tcp_chr_close(CharDriverState
*chr
)
3099 TCPCharDriver
*s
= chr
->opaque
;
3102 if (s
->reconnect_timer
) {
3103 g_source_remove(s
->reconnect_timer
);
3104 s
->reconnect_timer
= 0;
3106 qapi_free_SocketAddress(s
->addr
);
3108 remove_fd_in_watch(chr
);
3110 g_io_channel_unref(s
->chan
);
3114 if (s
->listen_fd
>= 0) {
3115 if (s
->listen_tag
) {
3116 g_source_remove(s
->listen_tag
);
3119 if (s
->listen_chan
) {
3120 g_io_channel_unref(s
->listen_chan
);
3122 closesocket(s
->listen_fd
);
3124 if (s
->read_msgfds_num
) {
3125 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
3126 close(s
->read_msgfds
[i
]);
3128 g_free(s
->read_msgfds
);
3130 if (s
->write_msgfds_num
) {
3131 g_free(s
->write_msgfds
);
3134 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
3137 static void qemu_chr_finish_socket_connection(CharDriverState
*chr
, int fd
)
3139 TCPCharDriver
*s
= chr
->opaque
;
3143 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
3144 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
3145 tcp_chr_accept
, chr
);
3149 socket_set_nodelay(fd
);
3150 s
->chan
= io_channel_from_socket(s
->fd
);
3151 tcp_chr_connect(chr
);
3155 static void qemu_chr_socket_connected(int fd
, Error
*err
, void *opaque
)
3157 CharDriverState
*chr
= opaque
;
3158 TCPCharDriver
*s
= chr
->opaque
;
3161 check_report_connect_error(chr
, err
);
3165 s
->connect_err_reported
= false;
3166 qemu_chr_finish_socket_connection(chr
, fd
);
3169 static bool qemu_chr_open_socket_fd(CharDriverState
*chr
, Error
**errp
)
3171 TCPCharDriver
*s
= chr
->opaque
;
3175 fd
= socket_listen(s
->addr
, errp
);
3176 } else if (s
->reconnect_time
) {
3177 fd
= socket_connect(s
->addr
, errp
, qemu_chr_socket_connected
, chr
);
3180 fd
= socket_connect(s
->addr
, errp
, NULL
, NULL
);
3186 qemu_chr_finish_socket_connection(chr
, fd
);
3190 /*********************************************************/
3191 /* Ring buffer chardev */
3198 } RingBufCharDriver
;
3200 static size_t ringbuf_count(const CharDriverState
*chr
)
3202 const RingBufCharDriver
*d
= chr
->opaque
;
3204 return d
->prod
- d
->cons
;
3207 /* Called with chr_write_lock held. */
3208 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3210 RingBufCharDriver
*d
= chr
->opaque
;
3213 if (!buf
|| (len
< 0)) {
3217 for (i
= 0; i
< len
; i
++ ) {
3218 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3219 if (d
->prod
- d
->cons
> d
->size
) {
3220 d
->cons
= d
->prod
- d
->size
;
3227 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3229 RingBufCharDriver
*d
= chr
->opaque
;
3232 qemu_mutex_lock(&chr
->chr_write_lock
);
3233 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3234 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3236 qemu_mutex_unlock(&chr
->chr_write_lock
);
3241 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3243 RingBufCharDriver
*d
= chr
->opaque
;
3250 static CharDriverState
*qemu_chr_open_ringbuf(const char *id
,
3251 ChardevBackend
*backend
,
3255 ChardevRingbuf
*opts
= backend
->u
.ringbuf
;
3256 ChardevCommon
*common
= qapi_ChardevRingbuf_base(backend
->u
.ringbuf
);
3257 CharDriverState
*chr
;
3258 RingBufCharDriver
*d
;
3260 chr
= qemu_chr_alloc(common
, errp
);
3264 d
= g_malloc(sizeof(*d
));
3266 d
->size
= opts
->has_size
? opts
->size
: 65536;
3268 /* The size must be power of 2 */
3269 if (d
->size
& (d
->size
- 1)) {
3270 error_setg(errp
, "size of ringbuf chardev must be power of two");
3276 d
->cbuf
= g_malloc0(d
->size
);
3279 chr
->chr_write
= ringbuf_chr_write
;
3280 chr
->chr_close
= ringbuf_chr_close
;
3286 qemu_chr_free_common(chr
);
3290 bool chr_is_ringbuf(const CharDriverState
*chr
)
3292 return chr
->chr_write
== ringbuf_chr_write
;
3295 void qmp_ringbuf_write(const char *device
, const char *data
,
3296 bool has_format
, enum DataFormat format
,
3299 CharDriverState
*chr
;
3300 const uint8_t *write_data
;
3304 chr
= qemu_chr_find(device
);
3306 error_setg(errp
, "Device '%s' not found", device
);
3310 if (!chr_is_ringbuf(chr
)) {
3311 error_setg(errp
,"%s is not a ringbuf device", device
);
3315 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3316 write_data
= qbase64_decode(data
, -1,
3323 write_data
= (uint8_t *)data
;
3324 write_count
= strlen(data
);
3327 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3329 if (write_data
!= (uint8_t *)data
) {
3330 g_free((void *)write_data
);
3334 error_setg(errp
, "Failed to write to device %s", device
);
3339 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3340 bool has_format
, enum DataFormat format
,
3343 CharDriverState
*chr
;
3348 chr
= qemu_chr_find(device
);
3350 error_setg(errp
, "Device '%s' not found", device
);
3354 if (!chr_is_ringbuf(chr
)) {
3355 error_setg(errp
,"%s is not a ringbuf device", device
);
3360 error_setg(errp
, "size must be greater than zero");
3364 count
= ringbuf_count(chr
);
3365 size
= size
> count
? count
: size
;
3366 read_data
= g_malloc(size
+ 1);
3368 ringbuf_chr_read(chr
, read_data
, size
);
3370 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3371 data
= g_base64_encode(read_data
, size
);
3375 * FIXME should read only complete, valid UTF-8 characters up
3376 * to @size bytes. Invalid sequences should be replaced by a
3377 * suitable replacement character. Except when (and only
3378 * when) ring buffer lost characters since last read, initial
3379 * continuation characters should be dropped.
3381 read_data
[size
] = 0;
3382 data
= (char *)read_data
;
3388 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3390 char host
[65], port
[33], width
[8], height
[8];
3394 Error
*local_err
= NULL
;
3396 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3398 error_report_err(local_err
);
3402 if (strstart(filename
, "mon:", &p
)) {
3404 qemu_opt_set(opts
, "mux", "on", &error_abort
);
3405 if (strcmp(filename
, "stdio") == 0) {
3406 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3407 * but pass it to the guest. Handle this only for compat syntax,
3408 * for -chardev syntax we have special option for this.
3409 * This is what -nographic did, redirecting+muxing serial+monitor
3410 * to stdio causing Ctrl+C to be passed to guest. */
3411 qemu_opt_set(opts
, "signal", "off", &error_abort
);
3415 if (strcmp(filename
, "null") == 0 ||
3416 strcmp(filename
, "pty") == 0 ||
3417 strcmp(filename
, "msmouse") == 0 ||
3418 strcmp(filename
, "braille") == 0 ||
3419 strcmp(filename
, "testdev") == 0 ||
3420 strcmp(filename
, "stdio") == 0) {
3421 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
3424 if (strstart(filename
, "vc", &p
)) {
3425 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
3427 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3429 qemu_opt_set(opts
, "width", width
, &error_abort
);
3430 qemu_opt_set(opts
, "height", height
, &error_abort
);
3431 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3433 qemu_opt_set(opts
, "cols", width
, &error_abort
);
3434 qemu_opt_set(opts
, "rows", height
, &error_abort
);
3441 if (strcmp(filename
, "con:") == 0) {
3442 qemu_opt_set(opts
, "backend", "console", &error_abort
);
3445 if (strstart(filename
, "COM", NULL
)) {
3446 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
3447 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3450 if (strstart(filename
, "file:", &p
)) {
3451 qemu_opt_set(opts
, "backend", "file", &error_abort
);
3452 qemu_opt_set(opts
, "path", p
, &error_abort
);
3455 if (strstart(filename
, "pipe:", &p
)) {
3456 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
3457 qemu_opt_set(opts
, "path", p
, &error_abort
);
3460 if (strstart(filename
, "tcp:", &p
) ||
3461 strstart(filename
, "telnet:", &p
)) {
3462 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3464 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3467 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3468 qemu_opt_set(opts
, "host", host
, &error_abort
);
3469 qemu_opt_set(opts
, "port", port
, &error_abort
);
3470 if (p
[pos
] == ',') {
3471 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
3473 error_report_err(local_err
);
3477 if (strstart(filename
, "telnet:", &p
))
3478 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
3481 if (strstart(filename
, "udp:", &p
)) {
3482 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
3483 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3485 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3489 qemu_opt_set(opts
, "host", host
, &error_abort
);
3490 qemu_opt_set(opts
, "port", port
, &error_abort
);
3491 if (p
[pos
] == '@') {
3493 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3495 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3499 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
3500 qemu_opt_set(opts
, "localport", port
, &error_abort
);
3504 if (strstart(filename
, "unix:", &p
)) {
3505 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
3506 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
3508 error_report_err(local_err
);
3513 if (strstart(filename
, "/dev/parport", NULL
) ||
3514 strstart(filename
, "/dev/ppi", NULL
)) {
3515 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
3516 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3519 if (strstart(filename
, "/dev/", NULL
)) {
3520 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
3521 qemu_opt_set(opts
, "path", filename
, &error_abort
);
3526 qemu_opts_del(opts
);
3530 static void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
3532 const char *logfile
= qemu_opt_get(opts
, "logfile");
3534 backend
->has_logfile
= logfile
!= NULL
;
3535 backend
->logfile
= logfile
? g_strdup(logfile
) : NULL
;
3537 backend
->has_logappend
= true;
3538 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
3542 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3545 const char *path
= qemu_opt_get(opts
, "path");
3548 error_setg(errp
, "chardev: file: no filename given");
3551 backend
->u
.file
= g_new0(ChardevFile
, 1);
3552 qemu_chr_parse_common(opts
, qapi_ChardevFile_base(backend
->u
.file
));
3553 backend
->u
.file
->out
= g_strdup(path
);
3555 backend
->u
.file
->has_append
= true;
3556 backend
->u
.file
->append
= qemu_opt_get_bool(opts
, "append", false);
3559 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3562 backend
->u
.stdio
= g_new0(ChardevStdio
, 1);
3563 qemu_chr_parse_common(opts
, qapi_ChardevStdio_base(backend
->u
.stdio
));
3564 backend
->u
.stdio
->has_signal
= true;
3565 backend
->u
.stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3568 #ifdef HAVE_CHARDEV_SERIAL
3569 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3572 const char *device
= qemu_opt_get(opts
, "path");
3574 if (device
== NULL
) {
3575 error_setg(errp
, "chardev: serial/tty: no device path given");
3578 backend
->u
.serial
= g_new0(ChardevHostdev
, 1);
3579 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(backend
->u
.serial
));
3580 backend
->u
.serial
->device
= g_strdup(device
);
3584 #ifdef HAVE_CHARDEV_PARPORT
3585 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3588 const char *device
= qemu_opt_get(opts
, "path");
3590 if (device
== NULL
) {
3591 error_setg(errp
, "chardev: parallel: no device path given");
3594 backend
->u
.parallel
= g_new0(ChardevHostdev
, 1);
3595 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(backend
->u
.parallel
));
3596 backend
->u
.parallel
->device
= g_strdup(device
);
3600 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3603 const char *device
= qemu_opt_get(opts
, "path");
3605 if (device
== NULL
) {
3606 error_setg(errp
, "chardev: pipe: no device path given");
3609 backend
->u
.pipe
= g_new0(ChardevHostdev
, 1);
3610 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(backend
->u
.pipe
));
3611 backend
->u
.pipe
->device
= g_strdup(device
);
3614 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3619 backend
->u
.ringbuf
= g_new0(ChardevRingbuf
, 1);
3620 qemu_chr_parse_common(opts
, qapi_ChardevRingbuf_base(backend
->u
.ringbuf
));
3622 val
= qemu_opt_get_size(opts
, "size", 0);
3624 backend
->u
.ringbuf
->has_size
= true;
3625 backend
->u
.ringbuf
->size
= val
;
3629 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3632 const char *chardev
= qemu_opt_get(opts
, "chardev");
3634 if (chardev
== NULL
) {
3635 error_setg(errp
, "chardev: mux: no chardev given");
3638 backend
->u
.mux
= g_new0(ChardevMux
, 1);
3639 qemu_chr_parse_common(opts
, qapi_ChardevMux_base(backend
->u
.mux
));
3640 backend
->u
.mux
->chardev
= g_strdup(chardev
);
3643 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
3646 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
3647 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
3648 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
3649 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
3650 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
3651 const char *path
= qemu_opt_get(opts
, "path");
3652 const char *host
= qemu_opt_get(opts
, "host");
3653 const char *port
= qemu_opt_get(opts
, "port");
3654 SocketAddress
*addr
;
3658 error_setg(errp
, "chardev: socket: no host given");
3662 error_setg(errp
, "chardev: socket: no port given");
3667 backend
->u
.socket
= g_new0(ChardevSocket
, 1);
3668 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(backend
->u
.socket
));
3670 backend
->u
.socket
->has_nodelay
= true;
3671 backend
->u
.socket
->nodelay
= do_nodelay
;
3672 backend
->u
.socket
->has_server
= true;
3673 backend
->u
.socket
->server
= is_listen
;
3674 backend
->u
.socket
->has_telnet
= true;
3675 backend
->u
.socket
->telnet
= is_telnet
;
3676 backend
->u
.socket
->has_wait
= true;
3677 backend
->u
.socket
->wait
= is_waitconnect
;
3678 backend
->u
.socket
->has_reconnect
= true;
3679 backend
->u
.socket
->reconnect
= reconnect
;
3681 addr
= g_new0(SocketAddress
, 1);
3683 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
3684 addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
3685 addr
->u
.q_unix
->path
= g_strdup(path
);
3687 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3688 addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
3689 addr
->u
.inet
->host
= g_strdup(host
);
3690 addr
->u
.inet
->port
= g_strdup(port
);
3691 addr
->u
.inet
->has_to
= qemu_opt_get(opts
, "to");
3692 addr
->u
.inet
->to
= qemu_opt_get_number(opts
, "to", 0);
3693 addr
->u
.inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3694 addr
->u
.inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3695 addr
->u
.inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3696 addr
->u
.inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3698 backend
->u
.socket
->addr
= addr
;
3701 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
3704 const char *host
= qemu_opt_get(opts
, "host");
3705 const char *port
= qemu_opt_get(opts
, "port");
3706 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
3707 const char *localport
= qemu_opt_get(opts
, "localport");
3708 bool has_local
= false;
3709 SocketAddress
*addr
;
3711 if (host
== NULL
|| strlen(host
) == 0) {
3714 if (port
== NULL
|| strlen(port
) == 0) {
3715 error_setg(errp
, "chardev: udp: remote port not specified");
3718 if (localport
== NULL
|| strlen(localport
) == 0) {
3723 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
3729 backend
->u
.udp
= g_new0(ChardevUdp
, 1);
3730 qemu_chr_parse_common(opts
, qapi_ChardevUdp_base(backend
->u
.udp
));
3732 addr
= g_new0(SocketAddress
, 1);
3733 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3734 addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
3735 addr
->u
.inet
->host
= g_strdup(host
);
3736 addr
->u
.inet
->port
= g_strdup(port
);
3737 addr
->u
.inet
->has_ipv4
= qemu_opt_get(opts
, "ipv4");
3738 addr
->u
.inet
->ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0);
3739 addr
->u
.inet
->has_ipv6
= qemu_opt_get(opts
, "ipv6");
3740 addr
->u
.inet
->ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0);
3741 backend
->u
.udp
->remote
= addr
;
3744 backend
->u
.udp
->has_local
= true;
3745 addr
= g_new0(SocketAddress
, 1);
3746 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
3747 addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
3748 addr
->u
.inet
->host
= g_strdup(localaddr
);
3749 addr
->u
.inet
->port
= g_strdup(localport
);
3750 backend
->u
.udp
->local
= addr
;
3754 typedef struct CharDriver
{
3756 ChardevBackendKind kind
;
3757 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3758 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3759 ChardevReturn
*ret
, Error
**errp
);
3762 static GSList
*backends
;
3764 void register_char_driver(const char *name
, ChardevBackendKind kind
,
3765 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
),
3766 CharDriverState
*(*create
)(const char *id
, ChardevBackend
*backend
,
3767 ChardevReturn
*ret
, Error
**errp
))
3771 s
= g_malloc0(sizeof(*s
));
3772 s
->name
= g_strdup(name
);
3777 backends
= g_slist_append(backends
, s
);
3780 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3781 void (*init
)(struct CharDriverState
*s
),
3784 Error
*local_err
= NULL
;
3786 CharDriverState
*chr
;
3788 ChardevReturn
*ret
= NULL
;
3789 ChardevBackend
*backend
;
3790 const char *id
= qemu_opts_id(opts
);
3794 error_setg(errp
, "chardev: no id specified");
3798 if (qemu_opt_get(opts
, "backend") == NULL
) {
3799 error_setg(errp
, "chardev: \"%s\" missing backend",
3800 qemu_opts_id(opts
));
3803 for (i
= backends
; i
; i
= i
->next
) {
3806 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3811 error_setg(errp
, "chardev: backend \"%s\" not found",
3812 qemu_opt_get(opts
, "backend"));
3816 backend
= g_new0(ChardevBackend
, 1);
3818 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3819 bid
= g_strdup_printf("%s-base", id
);
3823 backend
->type
= cd
->kind
;
3825 cd
->parse(opts
, backend
, &local_err
);
3827 error_propagate(errp
, local_err
);
3831 ChardevCommon
*cc
= g_new0(ChardevCommon
, 1);
3832 qemu_chr_parse_common(opts
, cc
);
3833 backend
->u
.data
= cc
;
3836 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3842 qapi_free_ChardevBackend(backend
);
3843 qapi_free_ChardevReturn(ret
);
3844 backend
= g_new0(ChardevBackend
, 1);
3845 backend
->u
.mux
= g_new0(ChardevMux
, 1);
3846 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
3847 backend
->u
.mux
->chardev
= g_strdup(bid
);
3848 ret
= qmp_chardev_add(id
, backend
, errp
);
3850 chr
= qemu_chr_find(bid
);
3851 qemu_chr_delete(chr
);
3857 chr
= qemu_chr_find(id
);
3861 qapi_free_ChardevBackend(backend
);
3862 qapi_free_ChardevReturn(ret
);
3867 qemu_opts_del(opts
);
3871 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3874 CharDriverState
*chr
;
3878 if (strstart(filename
, "chardev:", &p
)) {
3879 return qemu_chr_find(p
);
3882 opts
= qemu_chr_parse_compat(label
, filename
);
3886 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3888 error_report_err(err
);
3890 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3891 qemu_chr_fe_claim_no_fail(chr
);
3892 monitor_init(chr
, MONITOR_USE_READLINE
);
3897 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3899 if (chr
->chr_set_echo
) {
3900 chr
->chr_set_echo(chr
, echo
);
3904 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3906 if (chr
->fe_open
== fe_open
) {
3909 chr
->fe_open
= fe_open
;
3910 if (chr
->chr_set_fe_open
) {
3911 chr
->chr_set_fe_open(chr
, fe_open
);
3915 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3917 if (chr
->chr_fe_event
) {
3918 chr
->chr_fe_event(chr
, event
);
3922 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3923 GIOFunc func
, void *user_data
)
3928 if (s
->chr_add_watch
== NULL
) {
3932 src
= s
->chr_add_watch(s
, cond
);
3937 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3938 tag
= g_source_attach(src
, NULL
);
3939 g_source_unref(src
);
3944 int qemu_chr_fe_claim(CharDriverState
*s
)
3946 if (s
->avail_connections
< 1) {
3949 s
->avail_connections
--;
3953 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3955 if (qemu_chr_fe_claim(s
) != 0) {
3956 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3957 __func__
, s
->label
);
3962 void qemu_chr_fe_release(CharDriverState
*s
)
3964 s
->avail_connections
++;
3967 static void qemu_chr_free_common(CharDriverState
*chr
)
3969 g_free(chr
->filename
);
3971 qemu_opts_del(chr
->opts
);
3972 if (chr
->logfd
!= -1) {
3975 qemu_mutex_destroy(&chr
->chr_write_lock
);
3979 void qemu_chr_free(CharDriverState
*chr
)
3981 if (chr
->chr_close
) {
3982 chr
->chr_close(chr
);
3984 qemu_chr_free_common(chr
);
3987 void qemu_chr_delete(CharDriverState
*chr
)
3989 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3993 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3995 ChardevInfoList
*chr_list
= NULL
;
3996 CharDriverState
*chr
;
3998 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3999 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
4000 info
->value
= g_malloc0(sizeof(*info
->value
));
4001 info
->value
->label
= g_strdup(chr
->label
);
4002 info
->value
->filename
= g_strdup(chr
->filename
);
4003 info
->value
->frontend_open
= chr
->fe_open
;
4005 info
->next
= chr_list
;
4012 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
4014 ChardevBackendInfoList
*backend_list
= NULL
;
4015 CharDriver
*c
= NULL
;
4018 for (i
= backends
; i
; i
= i
->next
) {
4019 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
4021 info
->value
= g_malloc0(sizeof(*info
->value
));
4022 info
->value
->name
= g_strdup(c
->name
);
4024 info
->next
= backend_list
;
4025 backend_list
= info
;
4028 return backend_list
;
4031 CharDriverState
*qemu_chr_find(const char *name
)
4033 CharDriverState
*chr
;
4035 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
4036 if (strcmp(chr
->label
, name
) != 0)
4043 /* Get a character (serial) device interface. */
4044 CharDriverState
*qemu_char_get_next_serial(void)
4046 static int next_serial
;
4047 CharDriverState
*chr
;
4049 /* FIXME: This function needs to go away: use chardev properties! */
4051 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
4052 chr
= serial_hds
[next_serial
++];
4053 qemu_chr_fe_claim_no_fail(chr
);
4059 QemuOptsList qemu_chardev_opts
= {
4061 .implied_opt_name
= "backend",
4062 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
4066 .type
= QEMU_OPT_STRING
,
4069 .type
= QEMU_OPT_STRING
,
4072 .type
= QEMU_OPT_STRING
,
4075 .type
= QEMU_OPT_STRING
,
4077 .name
= "localaddr",
4078 .type
= QEMU_OPT_STRING
,
4080 .name
= "localport",
4081 .type
= QEMU_OPT_STRING
,
4084 .type
= QEMU_OPT_NUMBER
,
4087 .type
= QEMU_OPT_BOOL
,
4090 .type
= QEMU_OPT_BOOL
,
4093 .type
= QEMU_OPT_BOOL
,
4096 .type
= QEMU_OPT_BOOL
,
4099 .type
= QEMU_OPT_BOOL
,
4101 .name
= "reconnect",
4102 .type
= QEMU_OPT_NUMBER
,
4105 .type
= QEMU_OPT_BOOL
,
4108 .type
= QEMU_OPT_NUMBER
,
4111 .type
= QEMU_OPT_NUMBER
,
4114 .type
= QEMU_OPT_NUMBER
,
4117 .type
= QEMU_OPT_NUMBER
,
4120 .type
= QEMU_OPT_BOOL
,
4123 .type
= QEMU_OPT_BOOL
,
4126 .type
= QEMU_OPT_STRING
,
4129 .type
= QEMU_OPT_NUMBER
,
4132 .type
= QEMU_OPT_SIZE
,
4135 .type
= QEMU_OPT_STRING
,
4138 .type
= QEMU_OPT_BOOL
,
4141 .type
= QEMU_OPT_STRING
,
4143 .name
= "logappend",
4144 .type
= QEMU_OPT_BOOL
,
4146 { /* end of list */ }
4152 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4153 ChardevBackend
*backend
,
4157 ChardevFile
*file
= backend
->u
.file
;
4158 ChardevCommon
*common
= qapi_ChardevFile_base(backend
->u
.file
);
4162 error_setg(errp
, "input file not supported");
4166 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
4167 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
4168 if (out
== INVALID_HANDLE_VALUE
) {
4169 error_setg(errp
, "open %s failed", file
->out
);
4172 return qemu_chr_open_win_file(out
, common
, errp
);
4175 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4176 ChardevBackend
*backend
,
4180 ChardevHostdev
*serial
= backend
->u
.serial
;
4181 ChardevCommon
*common
= qapi_ChardevHostdev_base(backend
->u
.serial
);
4182 return qemu_chr_open_win_path(serial
->device
, common
, errp
);
4187 static int qmp_chardev_open_file_source(char *src
, int flags
,
4192 TFR(fd
= qemu_open(src
, flags
, 0666));
4194 error_setg_file_open(errp
, errno
, src
);
4199 static CharDriverState
*qmp_chardev_open_file(const char *id
,
4200 ChardevBackend
*backend
,
4204 ChardevFile
*file
= backend
->u
.file
;
4205 ChardevCommon
*common
= qapi_ChardevFile_base(backend
->u
.file
);
4206 int flags
, in
= -1, out
;
4208 flags
= O_WRONLY
| O_CREAT
| O_BINARY
;
4209 if (file
->has_append
&& file
->append
) {
4215 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
4222 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
4229 return qemu_chr_open_fd(in
, out
, common
, errp
);
4232 #ifdef HAVE_CHARDEV_SERIAL
4233 static CharDriverState
*qmp_chardev_open_serial(const char *id
,
4234 ChardevBackend
*backend
,
4238 ChardevHostdev
*serial
= backend
->u
.serial
;
4239 ChardevCommon
*common
= qapi_ChardevHostdev_base(backend
->u
.serial
);
4242 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
4246 qemu_set_nonblock(fd
);
4247 return qemu_chr_open_tty_fd(fd
, common
, errp
);
4251 #ifdef HAVE_CHARDEV_PARPORT
4252 static CharDriverState
*qmp_chardev_open_parallel(const char *id
,
4253 ChardevBackend
*backend
,
4257 ChardevHostdev
*parallel
= backend
->u
.parallel
;
4258 ChardevCommon
*common
= qapi_ChardevHostdev_base(backend
->u
.parallel
);
4261 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
4265 return qemu_chr_open_pp_fd(fd
, common
, errp
);
4271 static void socket_try_connect(CharDriverState
*chr
)
4275 if (!qemu_chr_open_socket_fd(chr
, &err
)) {
4276 check_report_connect_error(chr
, err
);
4280 static gboolean
socket_reconnect_timeout(gpointer opaque
)
4282 CharDriverState
*chr
= opaque
;
4283 TCPCharDriver
*s
= chr
->opaque
;
4285 s
->reconnect_timer
= 0;
4291 socket_try_connect(chr
);
4296 static CharDriverState
*qmp_chardev_open_socket(const char *id
,
4297 ChardevBackend
*backend
,
4301 CharDriverState
*chr
;
4303 ChardevSocket
*sock
= backend
->u
.socket
;
4304 SocketAddress
*addr
= sock
->addr
;
4305 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
4306 bool is_listen
= sock
->has_server
? sock
->server
: true;
4307 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
4308 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
4309 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
4310 ChardevCommon
*common
= qapi_ChardevSocket_base(backend
->u
.socket
);
4312 chr
= qemu_chr_alloc(common
, errp
);
4316 s
= g_new0(TCPCharDriver
, 1);
4320 s
->is_unix
= addr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
4321 s
->is_listen
= is_listen
;
4322 s
->is_telnet
= is_telnet
;
4323 s
->do_nodelay
= do_nodelay
;
4324 qapi_copy_SocketAddress(&s
->addr
, sock
->addr
);
4327 chr
->chr_write
= tcp_chr_write
;
4328 chr
->chr_sync_read
= tcp_chr_sync_read
;
4329 chr
->chr_close
= tcp_chr_close
;
4330 chr
->get_msgfds
= tcp_get_msgfds
;
4331 chr
->set_msgfds
= tcp_set_msgfds
;
4332 chr
->chr_add_client
= tcp_chr_add_client
;
4333 chr
->chr_add_watch
= tcp_chr_add_watch
;
4334 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
4335 /* be isn't opened until we get a connection */
4336 chr
->explicit_be_open
= true;
4338 chr
->filename
= g_malloc(CHR_MAX_FILENAME_SIZE
);
4339 SocketAddress_to_str(chr
->filename
, CHR_MAX_FILENAME_SIZE
, "disconnected:",
4340 addr
, is_listen
, is_telnet
);
4344 s
->do_telnetopt
= 1;
4346 } else if (reconnect
> 0) {
4347 s
->reconnect_time
= reconnect
;
4350 if (s
->reconnect_time
) {
4351 socket_try_connect(chr
);
4352 } else if (!qemu_chr_open_socket_fd(chr
, errp
)) {
4354 qemu_chr_free_common(chr
);
4358 if (is_listen
&& is_waitconnect
) {
4359 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
4361 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
4362 qemu_set_nonblock(s
->listen_fd
);
4368 static CharDriverState
*qmp_chardev_open_udp(const char *id
,
4369 ChardevBackend
*backend
,
4373 ChardevUdp
*udp
= backend
->u
.udp
;
4374 ChardevCommon
*common
= qapi_ChardevUdp_base(backend
->u
.udp
);
4377 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
4381 return qemu_chr_open_udp_fd(fd
, common
, errp
);
4384 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
4387 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4388 CharDriverState
*chr
= NULL
;
4389 Error
*local_err
= NULL
;
4393 chr
= qemu_chr_find(id
);
4395 error_setg(errp
, "Chardev '%s' already exists", id
);
4400 for (i
= backends
; i
; i
= i
->next
) {
4403 if (cd
->kind
== backend
->type
) {
4404 chr
= cd
->create(id
, backend
, ret
, &local_err
);
4406 error_propagate(errp
, local_err
);
4415 error_setg(errp
, "chardev backend not available");
4419 chr
->label
= g_strdup(id
);
4420 chr
->avail_connections
=
4421 (backend
->type
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4422 if (!chr
->filename
) {
4423 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->type
]);
4425 if (!chr
->explicit_be_open
) {
4426 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4428 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4436 void qmp_chardev_remove(const char *id
, Error
**errp
)
4438 CharDriverState
*chr
;
4440 chr
= qemu_chr_find(id
);
4442 error_setg(errp
, "Chardev '%s' not found", id
);
4445 if (chr
->chr_can_read
|| chr
->chr_read
||
4446 chr
->chr_event
|| chr
->handler_opaque
) {
4447 error_setg(errp
, "Chardev '%s' is busy", id
);
4450 qemu_chr_delete(chr
);
4453 static void register_types(void)
4455 register_char_driver("null", CHARDEV_BACKEND_KIND_NULL
, NULL
,
4456 qemu_chr_open_null
);
4457 register_char_driver("socket", CHARDEV_BACKEND_KIND_SOCKET
,
4458 qemu_chr_parse_socket
, qmp_chardev_open_socket
);
4459 register_char_driver("udp", CHARDEV_BACKEND_KIND_UDP
, qemu_chr_parse_udp
,
4460 qmp_chardev_open_udp
);
4461 register_char_driver("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4462 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4463 register_char_driver("file", CHARDEV_BACKEND_KIND_FILE
,
4464 qemu_chr_parse_file_out
, qmp_chardev_open_file
);
4465 register_char_driver("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4466 qemu_chr_parse_stdio
, qemu_chr_open_stdio
);
4467 #if defined HAVE_CHARDEV_SERIAL
4468 register_char_driver("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4469 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4470 register_char_driver("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4471 qemu_chr_parse_serial
, qmp_chardev_open_serial
);
4473 #ifdef HAVE_CHARDEV_PARPORT
4474 register_char_driver("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4475 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4476 register_char_driver("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4477 qemu_chr_parse_parallel
, qmp_chardev_open_parallel
);
4479 #ifdef HAVE_CHARDEV_PTY
4480 register_char_driver("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
,
4484 register_char_driver("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
,
4485 qemu_chr_open_win_con
);
4487 register_char_driver("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4488 qemu_chr_parse_pipe
, qemu_chr_open_pipe
);
4489 register_char_driver("mux", CHARDEV_BACKEND_KIND_MUX
, qemu_chr_parse_mux
,
4491 /* Bug-compatibility: */
4492 register_char_driver("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4493 qemu_chr_parse_ringbuf
, qemu_chr_open_ringbuf
);
4494 /* this must be done after machine init, since we register FEs with muxes
4495 * as part of realize functions like serial_isa_realizefn when -nographic
4498 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4501 type_init(register_types
);