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/timer.h"
28 #include "sysemu/char.h"
30 #include "qmp-commands.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #include <arpa/inet.h>
52 #include <sys/select.h>
55 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56 #include <dev/ppbus/ppi.h>
57 #include <dev/ppbus/ppbconf.h>
58 #elif defined(__DragonFly__)
59 #include <dev/misc/ppi/ppi.h>
60 #include <bus/ppbus/ppbconf.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
82 #include "qemu/sockets.h"
83 #include "ui/qemu-spice.h"
85 #define READ_BUF_LEN 4096
86 #define READ_RETRIES 10
88 /***********************************************************/
89 /* character device */
91 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
92 QTAILQ_HEAD_INITIALIZER(chardevs
);
94 CharDriverState
*qemu_chr_alloc(void)
96 CharDriverState
*chr
= g_malloc0(sizeof(CharDriverState
));
97 qemu_mutex_init(&chr
->chr_write_lock
);
101 void qemu_chr_be_event(CharDriverState
*s
, int event
)
103 /* Keep track if the char device is open */
105 case CHR_EVENT_OPENED
:
108 case CHR_EVENT_CLOSED
:
115 s
->chr_event(s
->handler_opaque
, event
);
118 void qemu_chr_be_generic_open(CharDriverState
*s
)
120 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
123 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
127 qemu_mutex_lock(&s
->chr_write_lock
);
128 ret
= s
->chr_write(s
, buf
, len
);
129 qemu_mutex_unlock(&s
->chr_write_lock
);
133 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
138 qemu_mutex_lock(&s
->chr_write_lock
);
139 while (offset
< len
) {
141 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
142 if (res
== -1 && errno
== EAGAIN
) {
145 } while (res
== -1 && errno
== EAGAIN
);
153 qemu_mutex_unlock(&s
->chr_write_lock
);
161 int qemu_chr_fe_read_all(CharDriverState
*s
, uint8_t *buf
, int len
)
163 int offset
= 0, counter
= 10;
166 if (!s
->chr_sync_read
) {
170 while (offset
< len
) {
172 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
173 if (res
== -1 && errno
== EAGAIN
) {
176 } while (res
== -1 && errno
== EAGAIN
);
196 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
200 return s
->chr_ioctl(s
, cmd
, arg
);
203 int qemu_chr_be_can_write(CharDriverState
*s
)
205 if (!s
->chr_can_read
)
207 return s
->chr_can_read(s
->handler_opaque
);
210 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
213 s
->chr_read(s
->handler_opaque
, buf
, len
);
217 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
220 return (qemu_chr_fe_get_msgfds(s
, &fd
, 1) == 1) ? fd
: -1;
223 int qemu_chr_fe_get_msgfds(CharDriverState
*s
, int *fds
, int len
)
225 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
228 int qemu_chr_fe_set_msgfds(CharDriverState
*s
, int *fds
, int num
)
230 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
233 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
235 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
238 void qemu_chr_accept_input(CharDriverState
*s
)
240 if (s
->chr_accept_input
)
241 s
->chr_accept_input(s
);
245 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
247 char buf
[READ_BUF_LEN
];
250 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
251 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
255 static void remove_fd_in_watch(CharDriverState
*chr
);
257 void qemu_chr_add_handlers(CharDriverState
*s
,
258 IOCanReadHandler
*fd_can_read
,
259 IOReadHandler
*fd_read
,
260 IOEventHandler
*fd_event
,
265 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
267 remove_fd_in_watch(s
);
271 s
->chr_can_read
= fd_can_read
;
272 s
->chr_read
= fd_read
;
273 s
->chr_event
= fd_event
;
274 s
->handler_opaque
= opaque
;
275 if (fe_open
&& s
->chr_update_read_handler
)
276 s
->chr_update_read_handler(s
);
278 if (!s
->explicit_fe_open
) {
279 qemu_chr_fe_set_open(s
, fe_open
);
282 /* We're connecting to an already opened device, so let's make sure we
283 also get the open event */
284 if (fe_open
&& s
->be_open
) {
285 qemu_chr_be_generic_open(s
);
289 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
294 static CharDriverState
*qemu_chr_open_null(void)
296 CharDriverState
*chr
;
298 chr
= qemu_chr_alloc();
299 chr
->chr_write
= null_chr_write
;
300 chr
->explicit_be_open
= true;
304 /* MUX driver for serial I/O splitting */
306 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
307 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
309 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
310 IOReadHandler
*chr_read
[MAX_MUX
];
311 IOEventHandler
*chr_event
[MAX_MUX
];
312 void *ext_opaque
[MAX_MUX
];
313 CharDriverState
*drv
;
318 /* Intermediate input buffer allows to catch escape sequences even if the
319 currently active device is not accepting any input - but only until it
321 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
326 /* Protected by the CharDriverState chr_write_lock. */
328 int64_t timestamps_start
;
332 /* Called with chr_write_lock held. */
333 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
335 MuxDriver
*d
= chr
->opaque
;
337 if (!d
->timestamps
) {
338 ret
= qemu_chr_fe_write(d
->drv
, buf
, len
);
343 for (i
= 0; i
< len
; i
++) {
349 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
350 if (d
->timestamps_start
== -1)
351 d
->timestamps_start
= ti
;
352 ti
-= d
->timestamps_start
;
354 snprintf(buf1
, sizeof(buf1
),
355 "[%02d:%02d:%02d.%03d] ",
360 qemu_chr_fe_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
363 ret
+= qemu_chr_fe_write(d
->drv
, buf
+i
, 1);
364 if (buf
[i
] == '\n') {
372 static const char * const mux_help
[] = {
373 "% h print this help\n\r",
374 "% x exit emulator\n\r",
375 "% s save disk data back to file (if -snapshot)\n\r",
376 "% t toggle console timestamps\n\r"
377 "% b send break (magic sysrq)\n\r",
378 "% c switch between console and monitor\n\r",
383 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
384 static void mux_print_help(CharDriverState
*chr
)
387 char ebuf
[15] = "Escape-Char";
388 char cbuf
[50] = "\n\r";
390 if (term_escape_char
> 0 && term_escape_char
< 26) {
391 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
392 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
394 snprintf(cbuf
, sizeof(cbuf
),
395 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
398 qemu_chr_fe_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
399 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
400 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
401 if (mux_help
[i
][j
] == '%')
402 qemu_chr_fe_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
404 qemu_chr_fe_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
409 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
411 if (d
->chr_event
[mux_nr
])
412 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
415 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
417 if (d
->term_got_escape
) {
418 d
->term_got_escape
= 0;
419 if (ch
== term_escape_char
)
428 const char *term
= "QEMU: Terminated\n\r";
429 qemu_chr_fe_write(chr
, (uint8_t *)term
, strlen(term
));
437 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
440 /* Switch to the next registered device */
441 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
443 if (d
->focus
>= d
->mux_cnt
)
445 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
448 d
->timestamps
= !d
->timestamps
;
449 d
->timestamps_start
= -1;
453 } else if (ch
== term_escape_char
) {
454 d
->term_got_escape
= 1;
462 static void mux_chr_accept_input(CharDriverState
*chr
)
464 MuxDriver
*d
= chr
->opaque
;
467 while (d
->prod
[m
] != d
->cons
[m
] &&
468 d
->chr_can_read
[m
] &&
469 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
470 d
->chr_read
[m
](d
->ext_opaque
[m
],
471 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
475 static int mux_chr_can_read(void *opaque
)
477 CharDriverState
*chr
= opaque
;
478 MuxDriver
*d
= chr
->opaque
;
481 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
483 if (d
->chr_can_read
[m
])
484 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
488 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
490 CharDriverState
*chr
= opaque
;
491 MuxDriver
*d
= chr
->opaque
;
495 mux_chr_accept_input (opaque
);
497 for(i
= 0; i
< size
; i
++)
498 if (mux_proc_byte(chr
, d
, buf
[i
])) {
499 if (d
->prod
[m
] == d
->cons
[m
] &&
500 d
->chr_can_read
[m
] &&
501 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
502 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
504 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
508 static void mux_chr_event(void *opaque
, int event
)
510 CharDriverState
*chr
= opaque
;
511 MuxDriver
*d
= chr
->opaque
;
514 /* Send the event to all registered listeners */
515 for (i
= 0; i
< d
->mux_cnt
; i
++)
516 mux_chr_send_event(d
, i
, event
);
519 static void mux_chr_update_read_handler(CharDriverState
*chr
)
521 MuxDriver
*d
= chr
->opaque
;
523 if (d
->mux_cnt
>= MAX_MUX
) {
524 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
527 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
528 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
529 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
530 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
531 /* Fix up the real driver with mux routines */
532 if (d
->mux_cnt
== 0) {
533 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
536 if (d
->focus
!= -1) {
537 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
539 d
->focus
= d
->mux_cnt
;
541 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
544 static bool muxes_realized
;
547 * Called after processing of default and command-line-specified
548 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
549 * to a mux chardev. This is done here to ensure that
550 * output/prompts/banners are only displayed for the FE that has
551 * focus when initial command-line processing/machine init is
554 * After this point, any new FE attached to any new or existing
555 * mux will receive CHR_EVENT_OPENED notifications for the BE
558 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
560 CharDriverState
*chr
;
562 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
564 MuxDriver
*d
= chr
->opaque
;
567 /* send OPENED to all already-attached FEs */
568 for (i
= 0; i
< d
->mux_cnt
; i
++) {
569 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
571 /* mark mux as OPENED so any new FEs will immediately receive
574 qemu_chr_be_generic_open(chr
);
577 muxes_realized
= true;
580 static Notifier muxes_realize_notify
= {
581 .notify
= muxes_realize_done
,
584 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
586 CharDriverState
*chr
;
589 chr
= qemu_chr_alloc();
590 d
= g_malloc0(sizeof(MuxDriver
));
595 chr
->chr_write
= mux_chr_write
;
596 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
597 chr
->chr_accept_input
= mux_chr_accept_input
;
598 /* Frontend guest-open / -close notification is not support with muxes */
599 chr
->chr_set_fe_open
= NULL
;
600 /* only default to opened state if we've realized the initial
603 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
611 int send_all(int fd
, const void *buf
, int len1
)
617 ret
= send(fd
, buf
, len
, 0);
619 errno
= WSAGetLastError();
620 if (errno
!= WSAEWOULDBLOCK
) {
623 } else if (ret
== 0) {
635 int send_all(int fd
, const void *_buf
, int len1
)
638 const uint8_t *buf
= _buf
;
642 ret
= write(fd
, buf
, len
);
644 if (errno
!= EINTR
&& errno
!= EAGAIN
)
646 } else if (ret
== 0) {
656 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
662 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
664 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
681 typedef struct IOWatchPoll
688 IOCanReadHandler
*fd_can_read
;
693 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
695 return container_of(source
, IOWatchPoll
, parent
);
698 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
700 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
701 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
702 bool was_active
= iwp
->src
!= NULL
;
703 if (was_active
== now_active
) {
708 iwp
->src
= g_io_create_watch(iwp
->channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
709 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
710 g_source_attach(iwp
->src
, NULL
);
712 g_source_destroy(iwp
->src
);
713 g_source_unref(iwp
->src
);
719 static gboolean
io_watch_poll_check(GSource
*source
)
724 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
730 static void io_watch_poll_finalize(GSource
*source
)
732 /* Due to a glib bug, removing the last reference to a source
733 * inside a finalize callback causes recursive locking (and a
734 * deadlock). This is not a problem inside other callbacks,
735 * including dispatch callbacks, so we call io_remove_watch_poll
736 * to remove this source. At this point, iwp->src must
737 * be NULL, or we would leak it.
739 * This would be solved much more elegantly by child sources,
740 * but we support older glib versions that do not have them.
742 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
743 assert(iwp
->src
== NULL
);
746 static GSourceFuncs io_watch_poll_funcs
= {
747 .prepare
= io_watch_poll_prepare
,
748 .check
= io_watch_poll_check
,
749 .dispatch
= io_watch_poll_dispatch
,
750 .finalize
= io_watch_poll_finalize
,
753 /* Can only be used for read */
754 static guint
io_add_watch_poll(GIOChannel
*channel
,
755 IOCanReadHandler
*fd_can_read
,
762 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
763 iwp
->fd_can_read
= fd_can_read
;
764 iwp
->opaque
= user_data
;
765 iwp
->channel
= channel
;
766 iwp
->fd_read
= (GSourceFunc
) fd_read
;
769 tag
= g_source_attach(&iwp
->parent
, NULL
);
770 g_source_unref(&iwp
->parent
);
774 static void io_remove_watch_poll(guint tag
)
779 g_return_if_fail (tag
> 0);
781 source
= g_main_context_find_source_by_id(NULL
, tag
);
782 g_return_if_fail (source
!= NULL
);
784 iwp
= io_watch_poll_from_source(source
);
786 g_source_destroy(iwp
->src
);
787 g_source_unref(iwp
->src
);
790 g_source_destroy(&iwp
->parent
);
793 static void remove_fd_in_watch(CharDriverState
*chr
)
795 if (chr
->fd_in_tag
) {
796 io_remove_watch_poll(chr
->fd_in_tag
);
802 static GIOChannel
*io_channel_from_fd(int fd
)
810 chan
= g_io_channel_unix_new(fd
);
812 g_io_channel_set_encoding(chan
, NULL
, NULL
);
813 g_io_channel_set_buffered(chan
, FALSE
);
819 static GIOChannel
*io_channel_from_socket(int fd
)
828 chan
= g_io_channel_win32_new_socket(fd
);
830 chan
= g_io_channel_unix_new(fd
);
833 g_io_channel_set_encoding(chan
, NULL
, NULL
);
834 g_io_channel_set_buffered(chan
, FALSE
);
839 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
842 GIOStatus status
= G_IO_STATUS_NORMAL
;
844 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
845 gsize bytes_written
= 0;
847 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
848 &bytes_written
, NULL
);
849 offset
+= bytes_written
;
856 case G_IO_STATUS_NORMAL
:
859 case G_IO_STATUS_AGAIN
:
871 typedef struct FDCharDriver
{
872 CharDriverState
*chr
;
873 GIOChannel
*fd_in
, *fd_out
;
875 QTAILQ_ENTRY(FDCharDriver
) node
;
878 /* Called with chr_write_lock held. */
879 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
881 FDCharDriver
*s
= chr
->opaque
;
883 return io_channel_send(s
->fd_out
, buf
, len
);
886 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
888 CharDriverState
*chr
= opaque
;
889 FDCharDriver
*s
= chr
->opaque
;
891 uint8_t buf
[READ_BUF_LEN
];
896 if (len
> s
->max_size
) {
903 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
904 len
, &bytes_read
, NULL
);
905 if (status
== G_IO_STATUS_EOF
) {
906 remove_fd_in_watch(chr
);
907 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
910 if (status
== G_IO_STATUS_NORMAL
) {
911 qemu_chr_be_write(chr
, buf
, bytes_read
);
917 static int fd_chr_read_poll(void *opaque
)
919 CharDriverState
*chr
= opaque
;
920 FDCharDriver
*s
= chr
->opaque
;
922 s
->max_size
= qemu_chr_be_can_write(chr
);
926 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
928 FDCharDriver
*s
= chr
->opaque
;
929 return g_io_create_watch(s
->fd_out
, cond
);
932 static void fd_chr_update_read_handler(CharDriverState
*chr
)
934 FDCharDriver
*s
= chr
->opaque
;
936 remove_fd_in_watch(chr
);
938 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
943 static void fd_chr_close(struct CharDriverState
*chr
)
945 FDCharDriver
*s
= chr
->opaque
;
947 remove_fd_in_watch(chr
);
949 g_io_channel_unref(s
->fd_in
);
952 g_io_channel_unref(s
->fd_out
);
956 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
959 /* open a character device to a unix fd */
960 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
962 CharDriverState
*chr
;
965 chr
= qemu_chr_alloc();
966 s
= g_malloc0(sizeof(FDCharDriver
));
967 s
->fd_in
= io_channel_from_fd(fd_in
);
968 s
->fd_out
= io_channel_from_fd(fd_out
);
969 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
972 chr
->chr_add_watch
= fd_chr_add_watch
;
973 chr
->chr_write
= fd_chr_write
;
974 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
975 chr
->chr_close
= fd_chr_close
;
980 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
983 char filename_in
[256], filename_out
[256];
984 const char *filename
= opts
->device
;
986 if (filename
== NULL
) {
987 fprintf(stderr
, "chardev: pipe: no filename given\n");
991 snprintf(filename_in
, 256, "%s.in", filename
);
992 snprintf(filename_out
, 256, "%s.out", filename
);
993 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
994 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
995 if (fd_in
< 0 || fd_out
< 0) {
1000 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1005 return qemu_chr_open_fd(fd_in
, fd_out
);
1008 /* init terminal so that we can grab keys */
1009 static struct termios oldtty
;
1010 static int old_fd0_flags
;
1011 static bool stdio_allow_signal
;
1013 static void term_exit(void)
1015 tcsetattr (0, TCSANOW
, &oldtty
);
1016 fcntl(0, F_SETFL
, old_fd0_flags
);
1019 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1025 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1026 |INLCR
|IGNCR
|ICRNL
|IXON
);
1027 tty
.c_oflag
|= OPOST
;
1028 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1029 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1032 tty
.c_cc
[VTIME
] = 0;
1034 if (!stdio_allow_signal
)
1035 tty
.c_lflag
&= ~ISIG
;
1037 tcsetattr (0, TCSANOW
, &tty
);
1040 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1046 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
1048 CharDriverState
*chr
;
1050 if (is_daemonized()) {
1051 error_report("cannot use stdio with -daemonize");
1054 old_fd0_flags
= fcntl(0, F_GETFL
);
1055 tcgetattr (0, &oldtty
);
1056 fcntl(0, F_SETFL
, O_NONBLOCK
);
1059 chr
= qemu_chr_open_fd(0, 1);
1060 chr
->chr_close
= qemu_chr_close_stdio
;
1061 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1062 if (opts
->has_signal
) {
1063 stdio_allow_signal
= opts
->signal
;
1065 qemu_chr_fe_set_echo(chr
, false);
1070 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1071 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1072 || defined(__GLIBC__)
1074 #define HAVE_CHARDEV_TTY 1
1080 /* Protected by the CharDriverState chr_write_lock. */
1085 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1086 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1088 static gboolean
pty_chr_timer(gpointer opaque
)
1090 struct CharDriverState
*chr
= opaque
;
1091 PtyCharDriver
*s
= chr
->opaque
;
1093 qemu_mutex_lock(&chr
->chr_write_lock
);
1095 if (!s
->connected
) {
1097 pty_chr_update_read_handler_locked(chr
);
1099 qemu_mutex_unlock(&chr
->chr_write_lock
);
1103 /* Called with chr_write_lock held. */
1104 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1106 PtyCharDriver
*s
= chr
->opaque
;
1109 g_source_remove(s
->timer_tag
);
1114 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1116 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1120 /* Called with chr_write_lock held. */
1121 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1123 PtyCharDriver
*s
= chr
->opaque
;
1126 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1127 pfd
.events
= G_IO_OUT
;
1130 if (pfd
.revents
& G_IO_HUP
) {
1131 pty_chr_state(chr
, 0);
1133 pty_chr_state(chr
, 1);
1137 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1139 qemu_mutex_lock(&chr
->chr_write_lock
);
1140 pty_chr_update_read_handler_locked(chr
);
1141 qemu_mutex_unlock(&chr
->chr_write_lock
);
1144 /* Called with chr_write_lock held. */
1145 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1147 PtyCharDriver
*s
= chr
->opaque
;
1149 if (!s
->connected
) {
1150 /* guest sends data, check for (re-)connect */
1151 pty_chr_update_read_handler_locked(chr
);
1154 return io_channel_send(s
->fd
, buf
, len
);
1157 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1159 PtyCharDriver
*s
= chr
->opaque
;
1160 return g_io_create_watch(s
->fd
, cond
);
1163 static int pty_chr_read_poll(void *opaque
)
1165 CharDriverState
*chr
= opaque
;
1166 PtyCharDriver
*s
= chr
->opaque
;
1168 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1169 return s
->read_bytes
;
1172 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1174 CharDriverState
*chr
= opaque
;
1175 PtyCharDriver
*s
= chr
->opaque
;
1177 uint8_t buf
[READ_BUF_LEN
];
1181 if (len
> s
->read_bytes
)
1182 len
= s
->read_bytes
;
1186 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1187 if (status
!= G_IO_STATUS_NORMAL
) {
1188 pty_chr_state(chr
, 0);
1191 pty_chr_state(chr
, 1);
1192 qemu_chr_be_write(chr
, buf
, size
);
1197 /* Called with chr_write_lock held. */
1198 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1200 PtyCharDriver
*s
= chr
->opaque
;
1203 remove_fd_in_watch(chr
);
1205 /* (re-)connect poll interval for idle guests: once per second.
1206 * We check more frequently in case the guests sends data to
1207 * the virtual device linked to our pty. */
1208 pty_chr_rearm_timer(chr
, 1000);
1211 g_source_remove(s
->timer_tag
);
1214 if (!s
->connected
) {
1216 qemu_chr_be_generic_open(chr
);
1218 if (!chr
->fd_in_tag
) {
1219 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1225 static void pty_chr_close(struct CharDriverState
*chr
)
1227 PtyCharDriver
*s
= chr
->opaque
;
1230 remove_fd_in_watch(chr
);
1231 fd
= g_io_channel_unix_get_fd(s
->fd
);
1232 g_io_channel_unref(s
->fd
);
1235 g_source_remove(s
->timer_tag
);
1239 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1242 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1245 CharDriverState
*chr
;
1247 int master_fd
, slave_fd
;
1248 char pty_name
[PATH_MAX
];
1250 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1251 if (master_fd
< 0) {
1257 chr
= qemu_chr_alloc();
1259 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1260 ret
->pty
= g_strdup(pty_name
);
1261 ret
->has_pty
= true;
1263 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1266 s
= g_malloc0(sizeof(PtyCharDriver
));
1268 chr
->chr_write
= pty_chr_write
;
1269 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1270 chr
->chr_close
= pty_chr_close
;
1271 chr
->chr_add_watch
= pty_chr_add_watch
;
1272 chr
->explicit_be_open
= true;
1274 s
->fd
= io_channel_from_fd(master_fd
);
1280 static void tty_serial_init(int fd
, int speed
,
1281 int parity
, int data_bits
, int stop_bits
)
1287 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1288 speed
, parity
, data_bits
, stop_bits
);
1290 tcgetattr (fd
, &tty
);
1292 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1293 speed
= speed
* 10 / 11;
1310 /* Non-Posix values follow. They may be unsupported on some systems. */
1312 check_speed(115200);
1314 check_speed(230400);
1317 check_speed(460800);
1320 check_speed(500000);
1323 check_speed(576000);
1326 check_speed(921600);
1329 check_speed(1000000);
1332 check_speed(1152000);
1335 check_speed(1500000);
1338 check_speed(2000000);
1341 check_speed(2500000);
1344 check_speed(3000000);
1347 check_speed(3500000);
1350 check_speed(4000000);
1355 cfsetispeed(&tty
, spd
);
1356 cfsetospeed(&tty
, spd
);
1358 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1359 |INLCR
|IGNCR
|ICRNL
|IXON
);
1360 tty
.c_oflag
|= OPOST
;
1361 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1362 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1383 tty
.c_cflag
|= PARENB
;
1386 tty
.c_cflag
|= PARENB
| PARODD
;
1390 tty
.c_cflag
|= CSTOPB
;
1392 tcsetattr (fd
, TCSANOW
, &tty
);
1395 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1397 FDCharDriver
*s
= chr
->opaque
;
1400 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1402 QEMUSerialSetParams
*ssp
= arg
;
1403 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1404 ssp
->speed
, ssp
->parity
,
1405 ssp
->data_bits
, ssp
->stop_bits
);
1408 case CHR_IOCTL_SERIAL_SET_BREAK
:
1410 int enable
= *(int *)arg
;
1412 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1416 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1419 int *targ
= (int *)arg
;
1420 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1422 if (sarg
& TIOCM_CTS
)
1423 *targ
|= CHR_TIOCM_CTS
;
1424 if (sarg
& TIOCM_CAR
)
1425 *targ
|= CHR_TIOCM_CAR
;
1426 if (sarg
& TIOCM_DSR
)
1427 *targ
|= CHR_TIOCM_DSR
;
1428 if (sarg
& TIOCM_RI
)
1429 *targ
|= CHR_TIOCM_RI
;
1430 if (sarg
& TIOCM_DTR
)
1431 *targ
|= CHR_TIOCM_DTR
;
1432 if (sarg
& TIOCM_RTS
)
1433 *targ
|= CHR_TIOCM_RTS
;
1436 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1438 int sarg
= *(int *)arg
;
1440 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1441 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1442 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1443 if (sarg
& CHR_TIOCM_CTS
)
1445 if (sarg
& CHR_TIOCM_CAR
)
1447 if (sarg
& CHR_TIOCM_DSR
)
1449 if (sarg
& CHR_TIOCM_RI
)
1451 if (sarg
& CHR_TIOCM_DTR
)
1453 if (sarg
& CHR_TIOCM_RTS
)
1455 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1464 static void qemu_chr_close_tty(CharDriverState
*chr
)
1466 FDCharDriver
*s
= chr
->opaque
;
1470 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1480 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1482 CharDriverState
*chr
;
1484 tty_serial_init(fd
, 115200, 'N', 8, 1);
1485 chr
= qemu_chr_open_fd(fd
, fd
);
1486 chr
->chr_ioctl
= tty_serial_ioctl
;
1487 chr
->chr_close
= qemu_chr_close_tty
;
1490 #endif /* __linux__ || __sun__ */
1492 #if defined(__linux__)
1494 #define HAVE_CHARDEV_PARPORT 1
1499 } ParallelCharDriver
;
1501 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1503 if (s
->mode
!= mode
) {
1505 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1512 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1514 ParallelCharDriver
*drv
= chr
->opaque
;
1519 case CHR_IOCTL_PP_READ_DATA
:
1520 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1522 *(uint8_t *)arg
= b
;
1524 case CHR_IOCTL_PP_WRITE_DATA
:
1525 b
= *(uint8_t *)arg
;
1526 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1529 case CHR_IOCTL_PP_READ_CONTROL
:
1530 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1532 /* Linux gives only the lowest bits, and no way to know data
1533 direction! For better compatibility set the fixed upper
1535 *(uint8_t *)arg
= b
| 0xc0;
1537 case CHR_IOCTL_PP_WRITE_CONTROL
:
1538 b
= *(uint8_t *)arg
;
1539 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1542 case CHR_IOCTL_PP_READ_STATUS
:
1543 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1545 *(uint8_t *)arg
= b
;
1547 case CHR_IOCTL_PP_DATA_DIR
:
1548 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1551 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1552 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1553 struct ParallelIOArg
*parg
= arg
;
1554 int n
= read(fd
, parg
->buffer
, parg
->count
);
1555 if (n
!= parg
->count
) {
1560 case CHR_IOCTL_PP_EPP_READ
:
1561 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1562 struct ParallelIOArg
*parg
= arg
;
1563 int n
= read(fd
, parg
->buffer
, parg
->count
);
1564 if (n
!= parg
->count
) {
1569 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1570 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1571 struct ParallelIOArg
*parg
= arg
;
1572 int n
= write(fd
, parg
->buffer
, parg
->count
);
1573 if (n
!= parg
->count
) {
1578 case CHR_IOCTL_PP_EPP_WRITE
:
1579 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1580 struct ParallelIOArg
*parg
= arg
;
1581 int n
= write(fd
, parg
->buffer
, parg
->count
);
1582 if (n
!= parg
->count
) {
1593 static void pp_close(CharDriverState
*chr
)
1595 ParallelCharDriver
*drv
= chr
->opaque
;
1598 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1599 ioctl(fd
, PPRELEASE
);
1602 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1605 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1607 CharDriverState
*chr
;
1608 ParallelCharDriver
*drv
;
1610 if (ioctl(fd
, PPCLAIM
) < 0) {
1615 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1617 drv
->mode
= IEEE1284_MODE_COMPAT
;
1619 chr
= qemu_chr_alloc();
1620 chr
->chr_write
= null_chr_write
;
1621 chr
->chr_ioctl
= pp_ioctl
;
1622 chr
->chr_close
= pp_close
;
1627 #endif /* __linux__ */
1629 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1631 #define HAVE_CHARDEV_PARPORT 1
1633 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1635 int fd
= (int)(intptr_t)chr
->opaque
;
1639 case CHR_IOCTL_PP_READ_DATA
:
1640 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1642 *(uint8_t *)arg
= b
;
1644 case CHR_IOCTL_PP_WRITE_DATA
:
1645 b
= *(uint8_t *)arg
;
1646 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1649 case CHR_IOCTL_PP_READ_CONTROL
:
1650 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1652 *(uint8_t *)arg
= b
;
1654 case CHR_IOCTL_PP_WRITE_CONTROL
:
1655 b
= *(uint8_t *)arg
;
1656 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1659 case CHR_IOCTL_PP_READ_STATUS
:
1660 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1662 *(uint8_t *)arg
= b
;
1670 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1672 CharDriverState
*chr
;
1674 chr
= qemu_chr_alloc();
1675 chr
->opaque
= (void *)(intptr_t)fd
;
1676 chr
->chr_write
= null_chr_write
;
1677 chr
->chr_ioctl
= pp_ioctl
;
1678 chr
->explicit_be_open
= true;
1687 HANDLE hcom
, hrecv
, hsend
;
1692 /* Protected by the CharDriverState chr_write_lock. */
1698 HANDLE hInputReadyEvent
;
1699 HANDLE hInputDoneEvent
;
1700 HANDLE hInputThread
;
1701 uint8_t win_stdio_buf
;
1702 } WinStdioCharState
;
1704 #define NSENDBUF 2048
1705 #define NRECVBUF 2048
1706 #define MAXCONNECT 1
1707 #define NTIMEOUT 5000
1709 static int win_chr_poll(void *opaque
);
1710 static int win_chr_pipe_poll(void *opaque
);
1712 static void win_chr_close(CharDriverState
*chr
)
1714 WinCharState
*s
= chr
->opaque
;
1717 CloseHandle(s
->hsend
);
1721 CloseHandle(s
->hrecv
);
1725 CloseHandle(s
->hcom
);
1729 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1731 qemu_del_polling_cb(win_chr_poll
, chr
);
1733 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1736 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1738 WinCharState
*s
= chr
->opaque
;
1740 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1745 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1747 fprintf(stderr
, "Failed CreateEvent\n");
1750 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1752 fprintf(stderr
, "Failed CreateEvent\n");
1756 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1757 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1758 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1759 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1764 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1765 fprintf(stderr
, "Failed SetupComm\n");
1769 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1770 size
= sizeof(COMMCONFIG
);
1771 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1772 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1773 CommConfigDialog(filename
, NULL
, &comcfg
);
1775 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1776 fprintf(stderr
, "Failed SetCommState\n");
1780 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1781 fprintf(stderr
, "Failed SetCommMask\n");
1785 cto
.ReadIntervalTimeout
= MAXDWORD
;
1786 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1787 fprintf(stderr
, "Failed SetCommTimeouts\n");
1791 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1792 fprintf(stderr
, "Failed ClearCommError\n");
1795 qemu_add_polling_cb(win_chr_poll
, chr
);
1803 /* Called with chr_write_lock held. */
1804 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1806 WinCharState
*s
= chr
->opaque
;
1807 DWORD len
, ret
, size
, err
;
1810 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1811 s
->osend
.hEvent
= s
->hsend
;
1814 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1816 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1818 err
= GetLastError();
1819 if (err
== ERROR_IO_PENDING
) {
1820 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1838 static int win_chr_read_poll(CharDriverState
*chr
)
1840 WinCharState
*s
= chr
->opaque
;
1842 s
->max_size
= qemu_chr_be_can_write(chr
);
1846 static void win_chr_readfile(CharDriverState
*chr
)
1848 WinCharState
*s
= chr
->opaque
;
1850 uint8_t buf
[READ_BUF_LEN
];
1853 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1854 s
->orecv
.hEvent
= s
->hrecv
;
1855 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1857 err
= GetLastError();
1858 if (err
== ERROR_IO_PENDING
) {
1859 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1864 qemu_chr_be_write(chr
, buf
, size
);
1868 static void win_chr_read(CharDriverState
*chr
)
1870 WinCharState
*s
= chr
->opaque
;
1872 if (s
->len
> s
->max_size
)
1873 s
->len
= s
->max_size
;
1877 win_chr_readfile(chr
);
1880 static int win_chr_poll(void *opaque
)
1882 CharDriverState
*chr
= opaque
;
1883 WinCharState
*s
= chr
->opaque
;
1887 ClearCommError(s
->hcom
, &comerr
, &status
);
1888 if (status
.cbInQue
> 0) {
1889 s
->len
= status
.cbInQue
;
1890 win_chr_read_poll(chr
);
1897 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1899 CharDriverState
*chr
;
1902 chr
= qemu_chr_alloc();
1903 s
= g_malloc0(sizeof(WinCharState
));
1905 chr
->chr_write
= win_chr_write
;
1906 chr
->chr_close
= win_chr_close
;
1908 if (win_chr_init(chr
, filename
) < 0) {
1916 static int win_chr_pipe_poll(void *opaque
)
1918 CharDriverState
*chr
= opaque
;
1919 WinCharState
*s
= chr
->opaque
;
1922 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1925 win_chr_read_poll(chr
);
1932 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1934 WinCharState
*s
= chr
->opaque
;
1942 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1944 fprintf(stderr
, "Failed CreateEvent\n");
1947 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1949 fprintf(stderr
, "Failed CreateEvent\n");
1953 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1954 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1955 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1957 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1958 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1959 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1964 ZeroMemory(&ov
, sizeof(ov
));
1965 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1966 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1968 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1972 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1974 fprintf(stderr
, "Failed GetOverlappedResult\n");
1976 CloseHandle(ov
.hEvent
);
1983 CloseHandle(ov
.hEvent
);
1986 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1995 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1997 const char *filename
= opts
->device
;
1998 CharDriverState
*chr
;
2001 chr
= qemu_chr_alloc();
2002 s
= g_malloc0(sizeof(WinCharState
));
2004 chr
->chr_write
= win_chr_write
;
2005 chr
->chr_close
= win_chr_close
;
2007 if (win_chr_pipe_init(chr
, filename
) < 0) {
2015 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2017 CharDriverState
*chr
;
2020 chr
= qemu_chr_alloc();
2021 s
= g_malloc0(sizeof(WinCharState
));
2024 chr
->chr_write
= win_chr_write
;
2028 static CharDriverState
*qemu_chr_open_win_con(void)
2030 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2033 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2035 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2042 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2052 static void win_stdio_wait_func(void *opaque
)
2054 CharDriverState
*chr
= opaque
;
2055 WinStdioCharState
*stdio
= chr
->opaque
;
2056 INPUT_RECORD buf
[4];
2061 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2064 /* Avoid error storm */
2065 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2069 for (i
= 0; i
< dwSize
; i
++) {
2070 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2072 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2074 if (kev
->uChar
.AsciiChar
!= 0) {
2075 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2076 if (qemu_chr_be_can_write(chr
)) {
2077 uint8_t c
= kev
->uChar
.AsciiChar
;
2078 qemu_chr_be_write(chr
, &c
, 1);
2086 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2088 CharDriverState
*chr
= param
;
2089 WinStdioCharState
*stdio
= chr
->opaque
;
2095 /* Wait for one byte */
2096 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2098 /* Exit in case of error, continue if nothing read */
2106 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2107 if (stdio
->win_stdio_buf
== '\r') {
2111 /* Signal the main thread and wait until the byte was eaten */
2112 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2115 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2121 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2125 static void win_stdio_thread_wait_func(void *opaque
)
2127 CharDriverState
*chr
= opaque
;
2128 WinStdioCharState
*stdio
= chr
->opaque
;
2130 if (qemu_chr_be_can_write(chr
)) {
2131 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2134 SetEvent(stdio
->hInputDoneEvent
);
2137 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2139 WinStdioCharState
*stdio
= chr
->opaque
;
2142 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2145 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2147 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2151 static void win_stdio_close(CharDriverState
*chr
)
2153 WinStdioCharState
*stdio
= chr
->opaque
;
2155 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2156 CloseHandle(stdio
->hInputReadyEvent
);
2158 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2159 CloseHandle(stdio
->hInputDoneEvent
);
2161 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2162 TerminateThread(stdio
->hInputThread
, 0);
2165 g_free(chr
->opaque
);
2169 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2171 CharDriverState
*chr
;
2172 WinStdioCharState
*stdio
;
2176 chr
= qemu_chr_alloc();
2177 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2179 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2180 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2181 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2185 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2187 chr
->opaque
= stdio
;
2188 chr
->chr_write
= win_stdio_write
;
2189 chr
->chr_close
= win_stdio_close
;
2192 if (qemu_add_wait_object(stdio
->hStdIn
,
2193 win_stdio_wait_func
, chr
)) {
2194 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2199 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2200 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2201 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2204 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2205 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2206 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2207 fprintf(stderr
, "cannot create stdio thread or event\n");
2210 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2211 win_stdio_thread_wait_func
, chr
)) {
2212 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2216 dwMode
|= ENABLE_LINE_INPUT
;
2219 /* set the terminal in raw mode */
2220 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2221 dwMode
|= ENABLE_PROCESSED_INPUT
;
2224 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2226 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2227 qemu_chr_fe_set_echo(chr
, false);
2231 #endif /* !_WIN32 */
2234 /***********************************************************/
2235 /* UDP Net console */
2240 uint8_t buf
[READ_BUF_LEN
];
2246 /* Called with chr_write_lock held. */
2247 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2249 NetCharDriver
*s
= chr
->opaque
;
2250 gsize bytes_written
;
2253 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2254 if (status
== G_IO_STATUS_EOF
) {
2256 } else if (status
!= G_IO_STATUS_NORMAL
) {
2260 return bytes_written
;
2263 static int udp_chr_read_poll(void *opaque
)
2265 CharDriverState
*chr
= opaque
;
2266 NetCharDriver
*s
= chr
->opaque
;
2268 s
->max_size
= qemu_chr_be_can_write(chr
);
2270 /* If there were any stray characters in the queue process them
2273 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2274 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2276 s
->max_size
= qemu_chr_be_can_write(chr
);
2281 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2283 CharDriverState
*chr
= opaque
;
2284 NetCharDriver
*s
= chr
->opaque
;
2285 gsize bytes_read
= 0;
2288 if (s
->max_size
== 0) {
2291 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2293 s
->bufcnt
= bytes_read
;
2294 s
->bufptr
= s
->bufcnt
;
2295 if (status
!= G_IO_STATUS_NORMAL
) {
2296 remove_fd_in_watch(chr
);
2301 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2302 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2304 s
->max_size
= qemu_chr_be_can_write(chr
);
2310 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2312 NetCharDriver
*s
= chr
->opaque
;
2314 remove_fd_in_watch(chr
);
2316 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2321 static void udp_chr_close(CharDriverState
*chr
)
2323 NetCharDriver
*s
= chr
->opaque
;
2325 remove_fd_in_watch(chr
);
2327 g_io_channel_unref(s
->chan
);
2331 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2334 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2336 CharDriverState
*chr
= NULL
;
2337 NetCharDriver
*s
= NULL
;
2339 chr
= qemu_chr_alloc();
2340 s
= g_malloc0(sizeof(NetCharDriver
));
2343 s
->chan
= io_channel_from_socket(s
->fd
);
2347 chr
->chr_write
= udp_chr_write
;
2348 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2349 chr
->chr_close
= udp_chr_close
;
2350 /* be isn't opened until we get a connection */
2351 chr
->explicit_be_open
= true;
2355 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2357 Error
*local_err
= NULL
;
2360 fd
= inet_dgram_opts(opts
, &local_err
);
2362 qerror_report_err(local_err
);
2363 error_free(local_err
);
2366 return qemu_chr_open_udp_fd(fd
);
2369 /***********************************************************/
2370 /* TCP Net console */
2374 GIOChannel
*chan
, *listen_chan
;
2383 int read_msgfds_num
;
2385 int write_msgfds_num
;
2388 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2391 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2393 TCPCharDriver
*s
= chr
->opaque
;
2398 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2399 char control
[CMSG_SPACE(fd_size
)];
2400 struct cmsghdr
*cmsg
;
2402 memset(&msgh
, 0, sizeof(msgh
));
2403 memset(control
, 0, sizeof(control
));
2405 /* set the payload */
2406 iov
.iov_base
= (uint8_t *) buf
;
2409 msgh
.msg_iov
= &iov
;
2410 msgh
.msg_iovlen
= 1;
2412 msgh
.msg_control
= control
;
2413 msgh
.msg_controllen
= sizeof(control
);
2415 cmsg
= CMSG_FIRSTHDR(&msgh
);
2417 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2418 cmsg
->cmsg_level
= SOL_SOCKET
;
2419 cmsg
->cmsg_type
= SCM_RIGHTS
;
2420 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2423 r
= sendmsg(s
->fd
, &msgh
, 0);
2424 } while (r
< 0 && errno
== EINTR
);
2426 /* free the written msgfds, no matter what */
2427 if (s
->write_msgfds_num
) {
2428 g_free(s
->write_msgfds
);
2429 s
->write_msgfds
= 0;
2430 s
->write_msgfds_num
= 0;
2437 /* Called with chr_write_lock held. */
2438 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2440 TCPCharDriver
*s
= chr
->opaque
;
2443 if (s
->is_unix
&& s
->write_msgfds_num
) {
2444 return unix_send_msgfds(chr
, buf
, len
);
2448 return io_channel_send(s
->chan
, buf
, len
);
2451 /* XXX: indicate an error ? */
2456 static int tcp_chr_read_poll(void *opaque
)
2458 CharDriverState
*chr
= opaque
;
2459 TCPCharDriver
*s
= chr
->opaque
;
2462 s
->max_size
= qemu_chr_be_can_write(chr
);
2467 #define IAC_BREAK 243
2468 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2470 uint8_t *buf
, int *size
)
2472 /* Handle any telnet client's basic IAC options to satisfy char by
2473 * char mode with no echo. All IAC options will be removed from
2474 * the buf and the do_telnetopt variable will be used to track the
2475 * state of the width of the IAC information.
2477 * IAC commands come in sets of 3 bytes with the exception of the
2478 * "IAC BREAK" command and the double IAC.
2484 for (i
= 0; i
< *size
; i
++) {
2485 if (s
->do_telnetopt
> 1) {
2486 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2487 /* Double IAC means send an IAC */
2491 s
->do_telnetopt
= 1;
2493 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2494 /* Handle IAC break commands by sending a serial break */
2495 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2500 if (s
->do_telnetopt
>= 4) {
2501 s
->do_telnetopt
= 1;
2504 if ((unsigned char)buf
[i
] == IAC
) {
2505 s
->do_telnetopt
= 2;
2516 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2518 TCPCharDriver
*s
= chr
->opaque
;
2519 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2524 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2526 /* Close unused fds */
2527 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2528 close(s
->read_msgfds
[i
]);
2531 g_free(s
->read_msgfds
);
2533 s
->read_msgfds_num
= 0;
2539 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2541 TCPCharDriver
*s
= chr
->opaque
;
2543 /* clear old pending fd array */
2544 if (s
->write_msgfds
) {
2545 g_free(s
->write_msgfds
);
2549 s
->write_msgfds
= g_malloc(num
* sizeof(int));
2550 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2553 s
->write_msgfds_num
= num
;
2559 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2561 TCPCharDriver
*s
= chr
->opaque
;
2562 struct cmsghdr
*cmsg
;
2564 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2567 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2568 cmsg
->cmsg_level
!= SOL_SOCKET
||
2569 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2573 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2579 /* close and clean read_msgfds */
2580 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2581 close(s
->read_msgfds
[i
]);
2584 if (s
->read_msgfds_num
) {
2585 g_free(s
->read_msgfds
);
2588 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2589 s
->read_msgfds
= g_malloc(fd_size
);
2590 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2592 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2593 int fd
= s
->read_msgfds
[i
];
2598 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2601 #ifndef MSG_CMSG_CLOEXEC
2602 qemu_set_cloexec(fd
);
2608 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2610 TCPCharDriver
*s
= chr
->opaque
;
2611 struct msghdr msg
= { NULL
, };
2612 struct iovec iov
[1];
2614 struct cmsghdr cmsg
;
2615 char control
[CMSG_SPACE(sizeof(int))];
2620 iov
[0].iov_base
= buf
;
2621 iov
[0].iov_len
= len
;
2625 msg
.msg_control
= &msg_control
;
2626 msg
.msg_controllen
= sizeof(msg_control
);
2628 #ifdef MSG_CMSG_CLOEXEC
2629 flags
|= MSG_CMSG_CLOEXEC
;
2631 ret
= recvmsg(s
->fd
, &msg
, flags
);
2632 if (ret
> 0 && s
->is_unix
) {
2633 unix_process_msgfd(chr
, &msg
);
2639 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2641 TCPCharDriver
*s
= chr
->opaque
;
2642 return qemu_recv(s
->fd
, buf
, len
, 0);
2646 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2648 TCPCharDriver
*s
= chr
->opaque
;
2649 return g_io_create_watch(s
->chan
, cond
);
2652 static void tcp_chr_disconnect(CharDriverState
*chr
)
2654 TCPCharDriver
*s
= chr
->opaque
;
2657 if (s
->listen_chan
) {
2658 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2659 tcp_chr_accept
, chr
);
2661 remove_fd_in_watch(chr
);
2662 g_io_channel_unref(s
->chan
);
2666 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2669 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2671 CharDriverState
*chr
= opaque
;
2672 TCPCharDriver
*s
= chr
->opaque
;
2673 uint8_t buf
[READ_BUF_LEN
];
2676 if (!s
->connected
|| s
->max_size
<= 0) {
2680 if (len
> s
->max_size
)
2682 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2684 /* connection closed */
2685 tcp_chr_disconnect(chr
);
2686 } else if (size
> 0) {
2687 if (s
->do_telnetopt
)
2688 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2690 qemu_chr_be_write(chr
, buf
, size
);
2696 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2698 TCPCharDriver
*s
= chr
->opaque
;
2701 if (!s
->connected
) {
2705 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2707 /* connection closed */
2708 tcp_chr_disconnect(chr
);
2715 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2717 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2720 chr
->avail_connections
= 1;
2727 static gboolean
tcp_chr_chan_close(GIOChannel
*channel
, GIOCondition cond
,
2730 CharDriverState
*chr
= opaque
;
2732 if (cond
!= G_IO_HUP
) {
2736 /* connection closed */
2737 tcp_chr_disconnect(chr
);
2738 if (chr
->fd_hup_tag
) {
2739 g_source_remove(chr
->fd_hup_tag
);
2740 chr
->fd_hup_tag
= 0;
2746 static void tcp_chr_connect(void *opaque
)
2748 CharDriverState
*chr
= opaque
;
2749 TCPCharDriver
*s
= chr
->opaque
;
2753 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2755 chr
->fd_hup_tag
= g_io_add_watch(s
->chan
, G_IO_HUP
, tcp_chr_chan_close
,
2758 qemu_chr_be_generic_open(chr
);
2761 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2763 TCPCharDriver
*s
= chr
->opaque
;
2765 remove_fd_in_watch(chr
);
2767 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2772 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2773 static void tcp_chr_telnet_init(int fd
)
2776 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2777 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2778 send(fd
, (char *)buf
, 3, 0);
2779 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2780 send(fd
, (char *)buf
, 3, 0);
2781 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2782 send(fd
, (char *)buf
, 3, 0);
2783 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2784 send(fd
, (char *)buf
, 3, 0);
2787 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2789 TCPCharDriver
*s
= chr
->opaque
;
2793 qemu_set_nonblock(fd
);
2795 socket_set_nodelay(fd
);
2797 s
->chan
= io_channel_from_socket(fd
);
2798 if (s
->listen_tag
) {
2799 g_source_remove(s
->listen_tag
);
2802 tcp_chr_connect(chr
);
2807 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2809 CharDriverState
*chr
= opaque
;
2810 TCPCharDriver
*s
= chr
->opaque
;
2811 struct sockaddr_in saddr
;
2813 struct sockaddr_un uaddr
;
2815 struct sockaddr
*addr
;
2822 len
= sizeof(uaddr
);
2823 addr
= (struct sockaddr
*)&uaddr
;
2827 len
= sizeof(saddr
);
2828 addr
= (struct sockaddr
*)&saddr
;
2830 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2831 if (fd
< 0 && errno
!= EINTR
) {
2834 } else if (fd
>= 0) {
2835 if (s
->do_telnetopt
)
2836 tcp_chr_telnet_init(fd
);
2840 if (tcp_chr_add_client(chr
, fd
) < 0)
2846 static void tcp_chr_close(CharDriverState
*chr
)
2848 TCPCharDriver
*s
= chr
->opaque
;
2851 remove_fd_in_watch(chr
);
2853 g_io_channel_unref(s
->chan
);
2857 if (s
->listen_fd
>= 0) {
2858 if (s
->listen_tag
) {
2859 g_source_remove(s
->listen_tag
);
2862 if (s
->listen_chan
) {
2863 g_io_channel_unref(s
->listen_chan
);
2865 closesocket(s
->listen_fd
);
2867 if (s
->read_msgfds_num
) {
2868 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2869 close(s
->read_msgfds
[i
]);
2871 g_free(s
->read_msgfds
);
2873 if (s
->write_msgfds_num
) {
2874 g_free(s
->write_msgfds
);
2877 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2880 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2881 bool is_listen
, bool is_telnet
,
2882 bool is_waitconnect
,
2885 CharDriverState
*chr
= NULL
;
2886 TCPCharDriver
*s
= NULL
;
2887 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2888 const char *left
= "", *right
= "";
2889 struct sockaddr_storage ss
;
2890 socklen_t ss_len
= sizeof(ss
);
2892 memset(&ss
, 0, ss_len
);
2893 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2894 error_setg_errno(errp
, errno
, "getsockname");
2898 chr
= qemu_chr_alloc();
2899 s
= g_malloc0(sizeof(TCPCharDriver
));
2905 s
->read_msgfds_num
= 0;
2906 s
->write_msgfds
= 0;
2907 s
->write_msgfds_num
= 0;
2909 chr
->filename
= g_malloc(256);
2910 switch (ss
.ss_family
) {
2914 snprintf(chr
->filename
, 256, "unix:%s%s",
2915 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2916 is_listen
? ",server" : "");
2924 s
->do_nodelay
= do_nodelay
;
2925 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2926 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2927 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2928 is_telnet
? "telnet" : "tcp",
2929 left
, host
, right
, serv
,
2930 is_listen
? ",server" : "");
2935 chr
->chr_write
= tcp_chr_write
;
2936 chr
->chr_sync_read
= tcp_chr_sync_read
;
2937 chr
->chr_close
= tcp_chr_close
;
2938 chr
->get_msgfds
= tcp_get_msgfds
;
2939 chr
->set_msgfds
= tcp_set_msgfds
;
2940 chr
->chr_add_client
= tcp_chr_add_client
;
2941 chr
->chr_add_watch
= tcp_chr_add_watch
;
2942 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
2943 /* be isn't opened until we get a connection */
2944 chr
->explicit_be_open
= true;
2948 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2949 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2951 s
->do_telnetopt
= 1;
2956 socket_set_nodelay(fd
);
2957 s
->chan
= io_channel_from_socket(s
->fd
);
2958 tcp_chr_connect(chr
);
2961 if (is_listen
&& is_waitconnect
) {
2962 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2964 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2965 qemu_set_nonblock(s
->listen_fd
);
2970 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2972 CharDriverState
*chr
= NULL
;
2973 Error
*local_err
= NULL
;
2976 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2977 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2978 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2979 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2980 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2984 fd
= unix_listen_opts(opts
, &local_err
);
2986 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2990 fd
= inet_listen_opts(opts
, 0, &local_err
);
2992 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2999 if (!is_waitconnect
)
3000 qemu_set_nonblock(fd
);
3002 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
3003 is_waitconnect
, &local_err
);
3012 qerror_report_err(local_err
);
3013 error_free(local_err
);
3019 g_free(chr
->opaque
);
3025 /*********************************************************/
3026 /* Ring buffer chardev */
3033 } RingBufCharDriver
;
3035 static size_t ringbuf_count(const CharDriverState
*chr
)
3037 const RingBufCharDriver
*d
= chr
->opaque
;
3039 return d
->prod
- d
->cons
;
3042 /* Called with chr_write_lock held. */
3043 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3045 RingBufCharDriver
*d
= chr
->opaque
;
3048 if (!buf
|| (len
< 0)) {
3052 for (i
= 0; i
< len
; i
++ ) {
3053 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3054 if (d
->prod
- d
->cons
> d
->size
) {
3055 d
->cons
= d
->prod
- d
->size
;
3062 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3064 RingBufCharDriver
*d
= chr
->opaque
;
3067 qemu_mutex_lock(&chr
->chr_write_lock
);
3068 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3069 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3071 qemu_mutex_unlock(&chr
->chr_write_lock
);
3076 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3078 RingBufCharDriver
*d
= chr
->opaque
;
3085 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3088 CharDriverState
*chr
;
3089 RingBufCharDriver
*d
;
3091 chr
= qemu_chr_alloc();
3092 d
= g_malloc(sizeof(*d
));
3094 d
->size
= opts
->has_size
? opts
->size
: 65536;
3096 /* The size must be power of 2 */
3097 if (d
->size
& (d
->size
- 1)) {
3098 error_setg(errp
, "size of ringbuf chardev must be power of two");
3104 d
->cbuf
= g_malloc0(d
->size
);
3107 chr
->chr_write
= ringbuf_chr_write
;
3108 chr
->chr_close
= ringbuf_chr_close
;
3118 bool chr_is_ringbuf(const CharDriverState
*chr
)
3120 return chr
->chr_write
== ringbuf_chr_write
;
3123 void qmp_ringbuf_write(const char *device
, const char *data
,
3124 bool has_format
, enum DataFormat format
,
3127 CharDriverState
*chr
;
3128 const uint8_t *write_data
;
3132 chr
= qemu_chr_find(device
);
3134 error_setg(errp
, "Device '%s' not found", device
);
3138 if (!chr_is_ringbuf(chr
)) {
3139 error_setg(errp
,"%s is not a ringbuf device", device
);
3143 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3144 write_data
= g_base64_decode(data
, &write_count
);
3146 write_data
= (uint8_t *)data
;
3147 write_count
= strlen(data
);
3150 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3152 if (write_data
!= (uint8_t *)data
) {
3153 g_free((void *)write_data
);
3157 error_setg(errp
, "Failed to write to device %s", device
);
3162 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3163 bool has_format
, enum DataFormat format
,
3166 CharDriverState
*chr
;
3171 chr
= qemu_chr_find(device
);
3173 error_setg(errp
, "Device '%s' not found", device
);
3177 if (!chr_is_ringbuf(chr
)) {
3178 error_setg(errp
,"%s is not a ringbuf device", device
);
3183 error_setg(errp
, "size must be greater than zero");
3187 count
= ringbuf_count(chr
);
3188 size
= size
> count
? count
: size
;
3189 read_data
= g_malloc(size
+ 1);
3191 ringbuf_chr_read(chr
, read_data
, size
);
3193 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3194 data
= g_base64_encode(read_data
, size
);
3198 * FIXME should read only complete, valid UTF-8 characters up
3199 * to @size bytes. Invalid sequences should be replaced by a
3200 * suitable replacement character. Except when (and only
3201 * when) ring buffer lost characters since last read, initial
3202 * continuation characters should be dropped.
3204 read_data
[size
] = 0;
3205 data
= (char *)read_data
;
3211 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3213 char host
[65], port
[33], width
[8], height
[8];
3217 Error
*local_err
= NULL
;
3219 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3221 qerror_report_err(local_err
);
3222 error_free(local_err
);
3226 if (strstart(filename
, "mon:", &p
)) {
3228 qemu_opt_set(opts
, "mux", "on");
3229 if (strcmp(filename
, "stdio") == 0) {
3230 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3231 * but pass it to the guest. Handle this only for compat syntax,
3232 * for -chardev syntax we have special option for this.
3233 * This is what -nographic did, redirecting+muxing serial+monitor
3234 * to stdio causing Ctrl+C to be passed to guest. */
3235 qemu_opt_set(opts
, "signal", "off");
3239 if (strcmp(filename
, "null") == 0 ||
3240 strcmp(filename
, "pty") == 0 ||
3241 strcmp(filename
, "msmouse") == 0 ||
3242 strcmp(filename
, "braille") == 0 ||
3243 strcmp(filename
, "stdio") == 0) {
3244 qemu_opt_set(opts
, "backend", filename
);
3247 if (strstart(filename
, "vc", &p
)) {
3248 qemu_opt_set(opts
, "backend", "vc");
3250 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3252 qemu_opt_set(opts
, "width", width
);
3253 qemu_opt_set(opts
, "height", height
);
3254 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3256 qemu_opt_set(opts
, "cols", width
);
3257 qemu_opt_set(opts
, "rows", height
);
3264 if (strcmp(filename
, "con:") == 0) {
3265 qemu_opt_set(opts
, "backend", "console");
3268 if (strstart(filename
, "COM", NULL
)) {
3269 qemu_opt_set(opts
, "backend", "serial");
3270 qemu_opt_set(opts
, "path", filename
);
3273 if (strstart(filename
, "file:", &p
)) {
3274 qemu_opt_set(opts
, "backend", "file");
3275 qemu_opt_set(opts
, "path", p
);
3278 if (strstart(filename
, "pipe:", &p
)) {
3279 qemu_opt_set(opts
, "backend", "pipe");
3280 qemu_opt_set(opts
, "path", p
);
3283 if (strstart(filename
, "tcp:", &p
) ||
3284 strstart(filename
, "telnet:", &p
)) {
3285 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3287 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3290 qemu_opt_set(opts
, "backend", "socket");
3291 qemu_opt_set(opts
, "host", host
);
3292 qemu_opt_set(opts
, "port", port
);
3293 if (p
[pos
] == ',') {
3294 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3297 if (strstart(filename
, "telnet:", &p
))
3298 qemu_opt_set(opts
, "telnet", "on");
3301 if (strstart(filename
, "udp:", &p
)) {
3302 qemu_opt_set(opts
, "backend", "udp");
3303 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3305 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3309 qemu_opt_set(opts
, "host", host
);
3310 qemu_opt_set(opts
, "port", port
);
3311 if (p
[pos
] == '@') {
3313 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3315 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3319 qemu_opt_set(opts
, "localaddr", host
);
3320 qemu_opt_set(opts
, "localport", port
);
3324 if (strstart(filename
, "unix:", &p
)) {
3325 qemu_opt_set(opts
, "backend", "socket");
3326 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3330 if (strstart(filename
, "/dev/parport", NULL
) ||
3331 strstart(filename
, "/dev/ppi", NULL
)) {
3332 qemu_opt_set(opts
, "backend", "parport");
3333 qemu_opt_set(opts
, "path", filename
);
3336 if (strstart(filename
, "/dev/", NULL
)) {
3337 qemu_opt_set(opts
, "backend", "tty");
3338 qemu_opt_set(opts
, "path", filename
);
3343 qemu_opts_del(opts
);
3347 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3350 const char *path
= qemu_opt_get(opts
, "path");
3353 error_setg(errp
, "chardev: file: no filename given");
3356 backend
->file
= g_new0(ChardevFile
, 1);
3357 backend
->file
->out
= g_strdup(path
);
3360 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3363 backend
->stdio
= g_new0(ChardevStdio
, 1);
3364 backend
->stdio
->has_signal
= true;
3365 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3368 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3371 const char *device
= qemu_opt_get(opts
, "path");
3373 if (device
== NULL
) {
3374 error_setg(errp
, "chardev: serial/tty: no device path given");
3377 backend
->serial
= g_new0(ChardevHostdev
, 1);
3378 backend
->serial
->device
= g_strdup(device
);
3381 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3384 const char *device
= qemu_opt_get(opts
, "path");
3386 if (device
== NULL
) {
3387 error_setg(errp
, "chardev: parallel: no device path given");
3390 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3391 backend
->parallel
->device
= g_strdup(device
);
3394 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3397 const char *device
= qemu_opt_get(opts
, "path");
3399 if (device
== NULL
) {
3400 error_setg(errp
, "chardev: pipe: no device path given");
3403 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3404 backend
->pipe
->device
= g_strdup(device
);
3407 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3412 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3414 val
= qemu_opt_get_size(opts
, "size", 0);
3416 backend
->ringbuf
->has_size
= true;
3417 backend
->ringbuf
->size
= val
;
3421 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3424 const char *chardev
= qemu_opt_get(opts
, "chardev");
3426 if (chardev
== NULL
) {
3427 error_setg(errp
, "chardev: mux: no chardev given");
3430 backend
->mux
= g_new0(ChardevMux
, 1);
3431 backend
->mux
->chardev
= g_strdup(chardev
);
3434 typedef struct CharDriver
{
3437 CharDriverState
*(*open
)(QemuOpts
*opts
);
3438 /* new, qapi-based */
3439 ChardevBackendKind kind
;
3440 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3443 static GSList
*backends
;
3445 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3449 s
= g_malloc0(sizeof(*s
));
3450 s
->name
= g_strdup(name
);
3453 backends
= g_slist_append(backends
, s
);
3456 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3457 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3461 s
= g_malloc0(sizeof(*s
));
3462 s
->name
= g_strdup(name
);
3466 backends
= g_slist_append(backends
, s
);
3469 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3470 void (*init
)(struct CharDriverState
*s
),
3473 Error
*local_err
= NULL
;
3475 CharDriverState
*chr
;
3478 if (qemu_opts_id(opts
) == NULL
) {
3479 error_setg(errp
, "chardev: no id specified");
3483 if (qemu_opt_get(opts
, "backend") == NULL
) {
3484 error_setg(errp
, "chardev: \"%s\" missing backend",
3485 qemu_opts_id(opts
));
3488 for (i
= backends
; i
; i
= i
->next
) {
3491 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3496 error_setg(errp
, "chardev: backend \"%s\" not found",
3497 qemu_opt_get(opts
, "backend"));
3502 /* using new, qapi init */
3503 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3504 ChardevReturn
*ret
= NULL
;
3505 const char *id
= qemu_opts_id(opts
);
3508 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3509 bid
= g_strdup_printf("%s-base", id
);
3513 backend
->kind
= cd
->kind
;
3515 cd
->parse(opts
, backend
, &local_err
);
3517 error_propagate(errp
, local_err
);
3521 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3527 qapi_free_ChardevBackend(backend
);
3528 qapi_free_ChardevReturn(ret
);
3529 backend
= g_new0(ChardevBackend
, 1);
3530 backend
->mux
= g_new0(ChardevMux
, 1);
3531 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3532 backend
->mux
->chardev
= g_strdup(bid
);
3533 ret
= qmp_chardev_add(id
, backend
, errp
);
3535 chr
= qemu_chr_find(bid
);
3536 qemu_chr_delete(chr
);
3542 chr
= qemu_chr_find(id
);
3546 qapi_free_ChardevBackend(backend
);
3547 qapi_free_ChardevReturn(ret
);
3552 chr
= cd
->open(opts
);
3554 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3555 qemu_opt_get(opts
, "backend"));
3560 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3562 /* if we didn't create the chardev via qmp_chardev_add, we
3563 * need to send the OPENED event here
3565 if (!chr
->explicit_be_open
) {
3566 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3568 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3570 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3571 CharDriverState
*base
= chr
;
3572 int len
= strlen(qemu_opts_id(opts
)) + 6;
3573 base
->label
= g_malloc(len
);
3574 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3575 chr
= qemu_chr_open_mux(base
);
3576 chr
->filename
= base
->filename
;
3577 chr
->avail_connections
= MAX_MUX
;
3578 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3580 chr
->avail_connections
= 1;
3582 chr
->label
= g_strdup(qemu_opts_id(opts
));
3587 qemu_opts_del(opts
);
3591 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3594 CharDriverState
*chr
;
3598 if (strstart(filename
, "chardev:", &p
)) {
3599 return qemu_chr_find(p
);
3602 opts
= qemu_chr_parse_compat(label
, filename
);
3606 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3608 error_report("%s", error_get_pretty(err
));
3611 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3612 qemu_chr_fe_claim_no_fail(chr
);
3613 monitor_init(chr
, MONITOR_USE_READLINE
);
3618 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3620 if (chr
->chr_set_echo
) {
3621 chr
->chr_set_echo(chr
, echo
);
3625 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3627 if (chr
->fe_open
== fe_open
) {
3630 chr
->fe_open
= fe_open
;
3631 if (chr
->chr_set_fe_open
) {
3632 chr
->chr_set_fe_open(chr
, fe_open
);
3636 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3638 if (chr
->chr_fe_event
) {
3639 chr
->chr_fe_event(chr
, event
);
3643 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3644 GIOFunc func
, void *user_data
)
3649 if (s
->chr_add_watch
== NULL
) {
3653 src
= s
->chr_add_watch(s
, cond
);
3654 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3655 tag
= g_source_attach(src
, NULL
);
3656 g_source_unref(src
);
3661 int qemu_chr_fe_claim(CharDriverState
*s
)
3663 if (s
->avail_connections
< 1) {
3666 s
->avail_connections
--;
3670 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3672 if (qemu_chr_fe_claim(s
) != 0) {
3673 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3674 __func__
, s
->label
);
3679 void qemu_chr_fe_release(CharDriverState
*s
)
3681 s
->avail_connections
++;
3684 void qemu_chr_delete(CharDriverState
*chr
)
3686 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3687 if (chr
->chr_close
) {
3688 chr
->chr_close(chr
);
3690 g_free(chr
->filename
);
3693 qemu_opts_del(chr
->opts
);
3698 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3700 ChardevInfoList
*chr_list
= NULL
;
3701 CharDriverState
*chr
;
3703 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3704 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3705 info
->value
= g_malloc0(sizeof(*info
->value
));
3706 info
->value
->label
= g_strdup(chr
->label
);
3707 info
->value
->filename
= g_strdup(chr
->filename
);
3708 info
->value
->frontend_open
= chr
->fe_open
;
3710 info
->next
= chr_list
;
3717 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3719 ChardevBackendInfoList
*backend_list
= NULL
;
3720 CharDriver
*c
= NULL
;
3723 for (i
= backends
; i
; i
= i
->next
) {
3724 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3726 info
->value
= g_malloc0(sizeof(*info
->value
));
3727 info
->value
->name
= g_strdup(c
->name
);
3729 info
->next
= backend_list
;
3730 backend_list
= info
;
3733 return backend_list
;
3736 CharDriverState
*qemu_chr_find(const char *name
)
3738 CharDriverState
*chr
;
3740 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3741 if (strcmp(chr
->label
, name
) != 0)
3748 /* Get a character (serial) device interface. */
3749 CharDriverState
*qemu_char_get_next_serial(void)
3751 static int next_serial
;
3752 CharDriverState
*chr
;
3754 /* FIXME: This function needs to go away: use chardev properties! */
3756 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3757 chr
= serial_hds
[next_serial
++];
3758 qemu_chr_fe_claim_no_fail(chr
);
3764 QemuOptsList qemu_chardev_opts
= {
3766 .implied_opt_name
= "backend",
3767 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3771 .type
= QEMU_OPT_STRING
,
3774 .type
= QEMU_OPT_STRING
,
3777 .type
= QEMU_OPT_STRING
,
3780 .type
= QEMU_OPT_STRING
,
3782 .name
= "localaddr",
3783 .type
= QEMU_OPT_STRING
,
3785 .name
= "localport",
3786 .type
= QEMU_OPT_STRING
,
3789 .type
= QEMU_OPT_NUMBER
,
3792 .type
= QEMU_OPT_BOOL
,
3795 .type
= QEMU_OPT_BOOL
,
3798 .type
= QEMU_OPT_BOOL
,
3801 .type
= QEMU_OPT_BOOL
,
3804 .type
= QEMU_OPT_BOOL
,
3807 .type
= QEMU_OPT_BOOL
,
3810 .type
= QEMU_OPT_NUMBER
,
3813 .type
= QEMU_OPT_NUMBER
,
3816 .type
= QEMU_OPT_NUMBER
,
3819 .type
= QEMU_OPT_NUMBER
,
3822 .type
= QEMU_OPT_BOOL
,
3825 .type
= QEMU_OPT_BOOL
,
3828 .type
= QEMU_OPT_STRING
,
3831 .type
= QEMU_OPT_NUMBER
,
3834 .type
= QEMU_OPT_SIZE
,
3837 .type
= QEMU_OPT_STRING
,
3839 { /* end of list */ }
3845 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3850 error_setg(errp
, "input file not supported");
3854 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3855 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3856 if (out
== INVALID_HANDLE_VALUE
) {
3857 error_setg(errp
, "open %s failed", file
->out
);
3860 return qemu_chr_open_win_file(out
);
3863 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3866 return qemu_chr_open_win_path(serial
->device
);
3869 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3872 error_setg(errp
, "character device backend type 'parallel' not supported");
3878 static int qmp_chardev_open_file_source(char *src
, int flags
,
3883 TFR(fd
= qemu_open(src
, flags
, 0666));
3885 error_setg_file_open(errp
, errno
, src
);
3890 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3892 int flags
, in
= -1, out
;
3894 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3895 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3902 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3909 return qemu_chr_open_fd(in
, out
);
3912 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3915 #ifdef HAVE_CHARDEV_TTY
3918 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3922 qemu_set_nonblock(fd
);
3923 return qemu_chr_open_tty_fd(fd
);
3925 error_setg(errp
, "character device backend type 'serial' not supported");
3930 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3933 #ifdef HAVE_CHARDEV_PARPORT
3936 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3940 return qemu_chr_open_pp_fd(fd
);
3942 error_setg(errp
, "character device backend type 'parallel' not supported");
3949 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3952 SocketAddress
*addr
= sock
->addr
;
3953 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3954 bool is_listen
= sock
->has_server
? sock
->server
: true;
3955 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3956 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3960 fd
= socket_listen(addr
, errp
);
3962 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3967 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3968 is_telnet
, is_waitconnect
, errp
);
3971 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3976 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3980 return qemu_chr_open_udp_fd(fd
);
3983 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3986 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3987 CharDriverState
*base
, *chr
= NULL
;
3989 chr
= qemu_chr_find(id
);
3991 error_setg(errp
, "Chardev '%s' already exists", id
);
3996 switch (backend
->kind
) {
3997 case CHARDEV_BACKEND_KIND_FILE
:
3998 chr
= qmp_chardev_open_file(backend
->file
, errp
);
4000 case CHARDEV_BACKEND_KIND_SERIAL
:
4001 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
4003 case CHARDEV_BACKEND_KIND_PARALLEL
:
4004 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
4006 case CHARDEV_BACKEND_KIND_PIPE
:
4007 chr
= qemu_chr_open_pipe(backend
->pipe
);
4009 case CHARDEV_BACKEND_KIND_SOCKET
:
4010 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
4012 case CHARDEV_BACKEND_KIND_UDP
:
4013 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
4015 #ifdef HAVE_CHARDEV_TTY
4016 case CHARDEV_BACKEND_KIND_PTY
:
4017 chr
= qemu_chr_open_pty(id
, ret
);
4020 case CHARDEV_BACKEND_KIND_NULL
:
4021 chr
= qemu_chr_open_null();
4023 case CHARDEV_BACKEND_KIND_MUX
:
4024 base
= qemu_chr_find(backend
->mux
->chardev
);
4026 error_setg(errp
, "mux: base chardev %s not found",
4027 backend
->mux
->chardev
);
4030 chr
= qemu_chr_open_mux(base
);
4032 case CHARDEV_BACKEND_KIND_MSMOUSE
:
4033 chr
= qemu_chr_open_msmouse();
4035 #ifdef CONFIG_BRLAPI
4036 case CHARDEV_BACKEND_KIND_BRAILLE
:
4037 chr
= chr_baum_init();
4040 case CHARDEV_BACKEND_KIND_STDIO
:
4041 chr
= qemu_chr_open_stdio(backend
->stdio
);
4044 case CHARDEV_BACKEND_KIND_CONSOLE
:
4045 chr
= qemu_chr_open_win_con();
4049 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4050 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
4052 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4053 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
4056 case CHARDEV_BACKEND_KIND_VC
:
4057 chr
= vc_init(backend
->vc
);
4059 case CHARDEV_BACKEND_KIND_RINGBUF
:
4060 case CHARDEV_BACKEND_KIND_MEMORY
:
4061 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
4064 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4069 * Character backend open hasn't been fully converted to the Error
4070 * API. Some opens fail without setting an error. Set a generic
4072 * TODO full conversion to Error API
4074 if (chr
== NULL
&& errp
&& !*errp
) {
4075 error_setg(errp
, "Failed to create chardev");
4078 chr
->label
= g_strdup(id
);
4079 chr
->avail_connections
=
4080 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4081 if (!chr
->filename
) {
4082 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4084 if (!chr
->explicit_be_open
) {
4085 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4087 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4095 void qmp_chardev_remove(const char *id
, Error
**errp
)
4097 CharDriverState
*chr
;
4099 chr
= qemu_chr_find(id
);
4101 error_setg(errp
, "Chardev '%s' not found", id
);
4104 if (chr
->chr_can_read
|| chr
->chr_read
||
4105 chr
->chr_event
|| chr
->handler_opaque
) {
4106 error_setg(errp
, "Chardev '%s' is busy", id
);
4109 qemu_chr_delete(chr
);
4112 static void register_types(void)
4114 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
4115 register_char_driver("socket", qemu_chr_open_socket
);
4116 register_char_driver("udp", qemu_chr_open_udp
);
4117 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4118 qemu_chr_parse_ringbuf
);
4119 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
4120 qemu_chr_parse_file_out
);
4121 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4122 qemu_chr_parse_stdio
);
4123 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4124 qemu_chr_parse_serial
);
4125 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4126 qemu_chr_parse_serial
);
4127 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4128 qemu_chr_parse_parallel
);
4129 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4130 qemu_chr_parse_parallel
);
4131 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
4132 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
4133 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4134 qemu_chr_parse_pipe
);
4135 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
4136 qemu_chr_parse_mux
);
4137 /* Bug-compatibility: */
4138 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4139 qemu_chr_parse_ringbuf
);
4140 /* this must be done after machine init, since we register FEs with muxes
4141 * as part of realize functions like serial_isa_realizefn when -nographic
4144 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4147 type_init(register_types
);