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 GSource
*mux_chr_add_watch(CharDriverState
*s
, GIOCondition cond
)
586 MuxDriver
*d
= s
->opaque
;
587 return d
->drv
->chr_add_watch(d
->drv
, cond
);
590 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
592 CharDriverState
*chr
;
595 chr
= qemu_chr_alloc();
596 d
= g_malloc0(sizeof(MuxDriver
));
601 chr
->chr_write
= mux_chr_write
;
602 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
603 chr
->chr_accept_input
= mux_chr_accept_input
;
604 /* Frontend guest-open / -close notification is not support with muxes */
605 chr
->chr_set_fe_open
= NULL
;
606 if (drv
->chr_add_watch
) {
607 chr
->chr_add_watch
= mux_chr_add_watch
;
609 /* only default to opened state if we've realized the initial
612 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
620 int send_all(int fd
, const void *buf
, int len1
)
626 ret
= send(fd
, buf
, len
, 0);
628 errno
= WSAGetLastError();
629 if (errno
!= WSAEWOULDBLOCK
) {
632 } else if (ret
== 0) {
644 int send_all(int fd
, const void *_buf
, int len1
)
647 const uint8_t *buf
= _buf
;
651 ret
= write(fd
, buf
, len
);
653 if (errno
!= EINTR
&& errno
!= EAGAIN
)
655 } else if (ret
== 0) {
665 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
671 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
673 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
690 typedef struct IOWatchPoll
697 IOCanReadHandler
*fd_can_read
;
702 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
704 return container_of(source
, IOWatchPoll
, parent
);
707 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
709 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
710 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
711 bool was_active
= iwp
->src
!= NULL
;
712 if (was_active
== now_active
) {
717 iwp
->src
= g_io_create_watch(iwp
->channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
718 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
719 g_source_attach(iwp
->src
, NULL
);
721 g_source_destroy(iwp
->src
);
722 g_source_unref(iwp
->src
);
728 static gboolean
io_watch_poll_check(GSource
*source
)
733 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
739 static void io_watch_poll_finalize(GSource
*source
)
741 /* Due to a glib bug, removing the last reference to a source
742 * inside a finalize callback causes recursive locking (and a
743 * deadlock). This is not a problem inside other callbacks,
744 * including dispatch callbacks, so we call io_remove_watch_poll
745 * to remove this source. At this point, iwp->src must
746 * be NULL, or we would leak it.
748 * This would be solved much more elegantly by child sources,
749 * but we support older glib versions that do not have them.
751 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
752 assert(iwp
->src
== NULL
);
755 static GSourceFuncs io_watch_poll_funcs
= {
756 .prepare
= io_watch_poll_prepare
,
757 .check
= io_watch_poll_check
,
758 .dispatch
= io_watch_poll_dispatch
,
759 .finalize
= io_watch_poll_finalize
,
762 /* Can only be used for read */
763 static guint
io_add_watch_poll(GIOChannel
*channel
,
764 IOCanReadHandler
*fd_can_read
,
771 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
772 iwp
->fd_can_read
= fd_can_read
;
773 iwp
->opaque
= user_data
;
774 iwp
->channel
= channel
;
775 iwp
->fd_read
= (GSourceFunc
) fd_read
;
778 tag
= g_source_attach(&iwp
->parent
, NULL
);
779 g_source_unref(&iwp
->parent
);
783 static void io_remove_watch_poll(guint tag
)
788 g_return_if_fail (tag
> 0);
790 source
= g_main_context_find_source_by_id(NULL
, tag
);
791 g_return_if_fail (source
!= NULL
);
793 iwp
= io_watch_poll_from_source(source
);
795 g_source_destroy(iwp
->src
);
796 g_source_unref(iwp
->src
);
799 g_source_destroy(&iwp
->parent
);
802 static void remove_fd_in_watch(CharDriverState
*chr
)
804 if (chr
->fd_in_tag
) {
805 io_remove_watch_poll(chr
->fd_in_tag
);
811 static GIOChannel
*io_channel_from_fd(int fd
)
819 chan
= g_io_channel_unix_new(fd
);
821 g_io_channel_set_encoding(chan
, NULL
, NULL
);
822 g_io_channel_set_buffered(chan
, FALSE
);
828 static GIOChannel
*io_channel_from_socket(int fd
)
837 chan
= g_io_channel_win32_new_socket(fd
);
839 chan
= g_io_channel_unix_new(fd
);
842 g_io_channel_set_encoding(chan
, NULL
, NULL
);
843 g_io_channel_set_buffered(chan
, FALSE
);
848 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
851 GIOStatus status
= G_IO_STATUS_NORMAL
;
853 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
854 gsize bytes_written
= 0;
856 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
857 &bytes_written
, NULL
);
858 offset
+= bytes_written
;
865 case G_IO_STATUS_NORMAL
:
868 case G_IO_STATUS_AGAIN
:
880 typedef struct FDCharDriver
{
881 CharDriverState
*chr
;
882 GIOChannel
*fd_in
, *fd_out
;
884 QTAILQ_ENTRY(FDCharDriver
) node
;
887 /* Called with chr_write_lock held. */
888 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
890 FDCharDriver
*s
= chr
->opaque
;
892 return io_channel_send(s
->fd_out
, buf
, len
);
895 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
897 CharDriverState
*chr
= opaque
;
898 FDCharDriver
*s
= chr
->opaque
;
900 uint8_t buf
[READ_BUF_LEN
];
905 if (len
> s
->max_size
) {
912 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
913 len
, &bytes_read
, NULL
);
914 if (status
== G_IO_STATUS_EOF
) {
915 remove_fd_in_watch(chr
);
916 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
919 if (status
== G_IO_STATUS_NORMAL
) {
920 qemu_chr_be_write(chr
, buf
, bytes_read
);
926 static int fd_chr_read_poll(void *opaque
)
928 CharDriverState
*chr
= opaque
;
929 FDCharDriver
*s
= chr
->opaque
;
931 s
->max_size
= qemu_chr_be_can_write(chr
);
935 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
937 FDCharDriver
*s
= chr
->opaque
;
938 return g_io_create_watch(s
->fd_out
, cond
);
941 static void fd_chr_update_read_handler(CharDriverState
*chr
)
943 FDCharDriver
*s
= chr
->opaque
;
945 remove_fd_in_watch(chr
);
947 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
952 static void fd_chr_close(struct CharDriverState
*chr
)
954 FDCharDriver
*s
= chr
->opaque
;
956 remove_fd_in_watch(chr
);
958 g_io_channel_unref(s
->fd_in
);
961 g_io_channel_unref(s
->fd_out
);
965 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
968 /* open a character device to a unix fd */
969 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
971 CharDriverState
*chr
;
974 chr
= qemu_chr_alloc();
975 s
= g_malloc0(sizeof(FDCharDriver
));
976 s
->fd_in
= io_channel_from_fd(fd_in
);
977 s
->fd_out
= io_channel_from_fd(fd_out
);
978 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
981 chr
->chr_add_watch
= fd_chr_add_watch
;
982 chr
->chr_write
= fd_chr_write
;
983 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
984 chr
->chr_close
= fd_chr_close
;
989 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
992 char filename_in
[256], filename_out
[256];
993 const char *filename
= opts
->device
;
995 if (filename
== NULL
) {
996 fprintf(stderr
, "chardev: pipe: no filename given\n");
1000 snprintf(filename_in
, 256, "%s.in", filename
);
1001 snprintf(filename_out
, 256, "%s.out", filename
);
1002 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
1003 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
1004 if (fd_in
< 0 || fd_out
< 0) {
1009 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
1014 return qemu_chr_open_fd(fd_in
, fd_out
);
1017 /* init terminal so that we can grab keys */
1018 static struct termios oldtty
;
1019 static int old_fd0_flags
;
1020 static bool stdio_allow_signal
;
1022 static void term_exit(void)
1024 tcsetattr (0, TCSANOW
, &oldtty
);
1025 fcntl(0, F_SETFL
, old_fd0_flags
);
1028 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1034 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1035 |INLCR
|IGNCR
|ICRNL
|IXON
);
1036 tty
.c_oflag
|= OPOST
;
1037 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1038 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1041 tty
.c_cc
[VTIME
] = 0;
1043 if (!stdio_allow_signal
)
1044 tty
.c_lflag
&= ~ISIG
;
1046 tcsetattr (0, TCSANOW
, &tty
);
1049 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1055 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
1057 CharDriverState
*chr
;
1059 if (is_daemonized()) {
1060 error_report("cannot use stdio with -daemonize");
1063 old_fd0_flags
= fcntl(0, F_GETFL
);
1064 tcgetattr (0, &oldtty
);
1065 fcntl(0, F_SETFL
, O_NONBLOCK
);
1068 chr
= qemu_chr_open_fd(0, 1);
1069 chr
->chr_close
= qemu_chr_close_stdio
;
1070 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1071 if (opts
->has_signal
) {
1072 stdio_allow_signal
= opts
->signal
;
1074 qemu_chr_fe_set_echo(chr
, false);
1079 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1080 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1081 || defined(__GLIBC__)
1083 #define HAVE_CHARDEV_TTY 1
1089 /* Protected by the CharDriverState chr_write_lock. */
1095 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
);
1096 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1098 static gboolean
pty_chr_timer(gpointer opaque
)
1100 struct CharDriverState
*chr
= opaque
;
1101 PtyCharDriver
*s
= chr
->opaque
;
1103 qemu_mutex_lock(&chr
->chr_write_lock
);
1106 if (!s
->connected
) {
1108 pty_chr_update_read_handler_locked(chr
);
1110 qemu_mutex_unlock(&chr
->chr_write_lock
);
1114 /* Called with chr_write_lock held. */
1115 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1117 PtyCharDriver
*s
= chr
->opaque
;
1120 g_source_remove(s
->timer_tag
);
1125 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1127 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1131 /* Called with chr_write_lock held. */
1132 static void pty_chr_update_read_handler_locked(CharDriverState
*chr
)
1134 PtyCharDriver
*s
= chr
->opaque
;
1137 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1138 pfd
.events
= G_IO_OUT
;
1141 if (pfd
.revents
& G_IO_HUP
) {
1142 pty_chr_state(chr
, 0);
1144 pty_chr_state(chr
, 1);
1148 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1150 qemu_mutex_lock(&chr
->chr_write_lock
);
1151 pty_chr_update_read_handler_locked(chr
);
1152 qemu_mutex_unlock(&chr
->chr_write_lock
);
1155 /* Called with chr_write_lock held. */
1156 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1158 PtyCharDriver
*s
= chr
->opaque
;
1160 if (!s
->connected
) {
1161 /* guest sends data, check for (re-)connect */
1162 pty_chr_update_read_handler_locked(chr
);
1165 return io_channel_send(s
->fd
, buf
, len
);
1168 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1170 PtyCharDriver
*s
= chr
->opaque
;
1171 return g_io_create_watch(s
->fd
, cond
);
1174 static int pty_chr_read_poll(void *opaque
)
1176 CharDriverState
*chr
= opaque
;
1177 PtyCharDriver
*s
= chr
->opaque
;
1179 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1180 return s
->read_bytes
;
1183 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1185 CharDriverState
*chr
= opaque
;
1186 PtyCharDriver
*s
= chr
->opaque
;
1188 uint8_t buf
[READ_BUF_LEN
];
1192 if (len
> s
->read_bytes
)
1193 len
= s
->read_bytes
;
1197 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1198 if (status
!= G_IO_STATUS_NORMAL
) {
1199 pty_chr_state(chr
, 0);
1202 pty_chr_state(chr
, 1);
1203 qemu_chr_be_write(chr
, buf
, size
);
1208 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
1210 CharDriverState
*chr
= opaque
;
1211 PtyCharDriver
*s
= chr
->opaque
;
1214 qemu_chr_be_generic_open(chr
);
1218 /* Called with chr_write_lock held. */
1219 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1221 PtyCharDriver
*s
= chr
->opaque
;
1225 g_source_remove(s
->open_tag
);
1228 remove_fd_in_watch(chr
);
1230 /* (re-)connect poll interval for idle guests: once per second.
1231 * We check more frequently in case the guests sends data to
1232 * the virtual device linked to our pty. */
1233 pty_chr_rearm_timer(chr
, 1000);
1236 g_source_remove(s
->timer_tag
);
1239 if (!s
->connected
) {
1240 g_assert(s
->open_tag
== 0);
1242 s
->open_tag
= g_idle_add(qemu_chr_be_generic_open_func
, chr
);
1244 if (!chr
->fd_in_tag
) {
1245 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1251 static void pty_chr_close(struct CharDriverState
*chr
)
1253 PtyCharDriver
*s
= chr
->opaque
;
1256 qemu_mutex_lock(&chr
->chr_write_lock
);
1257 pty_chr_state(chr
, 0);
1258 fd
= g_io_channel_unix_get_fd(s
->fd
);
1259 g_io_channel_unref(s
->fd
);
1262 g_source_remove(s
->timer_tag
);
1265 qemu_mutex_unlock(&chr
->chr_write_lock
);
1267 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1270 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1273 CharDriverState
*chr
;
1275 int master_fd
, slave_fd
;
1276 char pty_name
[PATH_MAX
];
1278 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1279 if (master_fd
< 0) {
1285 chr
= qemu_chr_alloc();
1287 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1288 ret
->pty
= g_strdup(pty_name
);
1289 ret
->has_pty
= true;
1291 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1294 s
= g_malloc0(sizeof(PtyCharDriver
));
1296 chr
->chr_write
= pty_chr_write
;
1297 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1298 chr
->chr_close
= pty_chr_close
;
1299 chr
->chr_add_watch
= pty_chr_add_watch
;
1300 chr
->explicit_be_open
= true;
1302 s
->fd
= io_channel_from_fd(master_fd
);
1308 static void tty_serial_init(int fd
, int speed
,
1309 int parity
, int data_bits
, int stop_bits
)
1315 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1316 speed
, parity
, data_bits
, stop_bits
);
1318 tcgetattr (fd
, &tty
);
1320 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1321 speed
= speed
* 10 / 11;
1338 /* Non-Posix values follow. They may be unsupported on some systems. */
1340 check_speed(115200);
1342 check_speed(230400);
1345 check_speed(460800);
1348 check_speed(500000);
1351 check_speed(576000);
1354 check_speed(921600);
1357 check_speed(1000000);
1360 check_speed(1152000);
1363 check_speed(1500000);
1366 check_speed(2000000);
1369 check_speed(2500000);
1372 check_speed(3000000);
1375 check_speed(3500000);
1378 check_speed(4000000);
1383 cfsetispeed(&tty
, spd
);
1384 cfsetospeed(&tty
, spd
);
1386 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1387 |INLCR
|IGNCR
|ICRNL
|IXON
);
1388 tty
.c_oflag
|= OPOST
;
1389 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1390 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1411 tty
.c_cflag
|= PARENB
;
1414 tty
.c_cflag
|= PARENB
| PARODD
;
1418 tty
.c_cflag
|= CSTOPB
;
1420 tcsetattr (fd
, TCSANOW
, &tty
);
1423 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1425 FDCharDriver
*s
= chr
->opaque
;
1428 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1430 QEMUSerialSetParams
*ssp
= arg
;
1431 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1432 ssp
->speed
, ssp
->parity
,
1433 ssp
->data_bits
, ssp
->stop_bits
);
1436 case CHR_IOCTL_SERIAL_SET_BREAK
:
1438 int enable
= *(int *)arg
;
1440 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1444 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1447 int *targ
= (int *)arg
;
1448 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1450 if (sarg
& TIOCM_CTS
)
1451 *targ
|= CHR_TIOCM_CTS
;
1452 if (sarg
& TIOCM_CAR
)
1453 *targ
|= CHR_TIOCM_CAR
;
1454 if (sarg
& TIOCM_DSR
)
1455 *targ
|= CHR_TIOCM_DSR
;
1456 if (sarg
& TIOCM_RI
)
1457 *targ
|= CHR_TIOCM_RI
;
1458 if (sarg
& TIOCM_DTR
)
1459 *targ
|= CHR_TIOCM_DTR
;
1460 if (sarg
& TIOCM_RTS
)
1461 *targ
|= CHR_TIOCM_RTS
;
1464 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1466 int sarg
= *(int *)arg
;
1468 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1469 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1470 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1471 if (sarg
& CHR_TIOCM_CTS
)
1473 if (sarg
& CHR_TIOCM_CAR
)
1475 if (sarg
& CHR_TIOCM_DSR
)
1477 if (sarg
& CHR_TIOCM_RI
)
1479 if (sarg
& CHR_TIOCM_DTR
)
1481 if (sarg
& CHR_TIOCM_RTS
)
1483 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1492 static void qemu_chr_close_tty(CharDriverState
*chr
)
1494 FDCharDriver
*s
= chr
->opaque
;
1498 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1508 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1510 CharDriverState
*chr
;
1512 tty_serial_init(fd
, 115200, 'N', 8, 1);
1513 chr
= qemu_chr_open_fd(fd
, fd
);
1514 chr
->chr_ioctl
= tty_serial_ioctl
;
1515 chr
->chr_close
= qemu_chr_close_tty
;
1518 #endif /* __linux__ || __sun__ */
1520 #if defined(__linux__)
1522 #define HAVE_CHARDEV_PARPORT 1
1527 } ParallelCharDriver
;
1529 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1531 if (s
->mode
!= mode
) {
1533 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1540 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1542 ParallelCharDriver
*drv
= chr
->opaque
;
1547 case CHR_IOCTL_PP_READ_DATA
:
1548 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1550 *(uint8_t *)arg
= b
;
1552 case CHR_IOCTL_PP_WRITE_DATA
:
1553 b
= *(uint8_t *)arg
;
1554 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1557 case CHR_IOCTL_PP_READ_CONTROL
:
1558 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1560 /* Linux gives only the lowest bits, and no way to know data
1561 direction! For better compatibility set the fixed upper
1563 *(uint8_t *)arg
= b
| 0xc0;
1565 case CHR_IOCTL_PP_WRITE_CONTROL
:
1566 b
= *(uint8_t *)arg
;
1567 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1570 case CHR_IOCTL_PP_READ_STATUS
:
1571 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1573 *(uint8_t *)arg
= b
;
1575 case CHR_IOCTL_PP_DATA_DIR
:
1576 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1579 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1580 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1581 struct ParallelIOArg
*parg
= arg
;
1582 int n
= read(fd
, parg
->buffer
, parg
->count
);
1583 if (n
!= parg
->count
) {
1588 case CHR_IOCTL_PP_EPP_READ
:
1589 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1590 struct ParallelIOArg
*parg
= arg
;
1591 int n
= read(fd
, parg
->buffer
, parg
->count
);
1592 if (n
!= parg
->count
) {
1597 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1598 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1599 struct ParallelIOArg
*parg
= arg
;
1600 int n
= write(fd
, parg
->buffer
, parg
->count
);
1601 if (n
!= parg
->count
) {
1606 case CHR_IOCTL_PP_EPP_WRITE
:
1607 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1608 struct ParallelIOArg
*parg
= arg
;
1609 int n
= write(fd
, parg
->buffer
, parg
->count
);
1610 if (n
!= parg
->count
) {
1621 static void pp_close(CharDriverState
*chr
)
1623 ParallelCharDriver
*drv
= chr
->opaque
;
1626 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1627 ioctl(fd
, PPRELEASE
);
1630 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1633 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1635 CharDriverState
*chr
;
1636 ParallelCharDriver
*drv
;
1638 if (ioctl(fd
, PPCLAIM
) < 0) {
1643 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1645 drv
->mode
= IEEE1284_MODE_COMPAT
;
1647 chr
= qemu_chr_alloc();
1648 chr
->chr_write
= null_chr_write
;
1649 chr
->chr_ioctl
= pp_ioctl
;
1650 chr
->chr_close
= pp_close
;
1655 #endif /* __linux__ */
1657 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1659 #define HAVE_CHARDEV_PARPORT 1
1661 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1663 int fd
= (int)(intptr_t)chr
->opaque
;
1667 case CHR_IOCTL_PP_READ_DATA
:
1668 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1670 *(uint8_t *)arg
= b
;
1672 case CHR_IOCTL_PP_WRITE_DATA
:
1673 b
= *(uint8_t *)arg
;
1674 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1677 case CHR_IOCTL_PP_READ_CONTROL
:
1678 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1680 *(uint8_t *)arg
= b
;
1682 case CHR_IOCTL_PP_WRITE_CONTROL
:
1683 b
= *(uint8_t *)arg
;
1684 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1687 case CHR_IOCTL_PP_READ_STATUS
:
1688 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1690 *(uint8_t *)arg
= b
;
1698 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1700 CharDriverState
*chr
;
1702 chr
= qemu_chr_alloc();
1703 chr
->opaque
= (void *)(intptr_t)fd
;
1704 chr
->chr_write
= null_chr_write
;
1705 chr
->chr_ioctl
= pp_ioctl
;
1706 chr
->explicit_be_open
= true;
1715 HANDLE hcom
, hrecv
, hsend
;
1720 /* Protected by the CharDriverState chr_write_lock. */
1726 HANDLE hInputReadyEvent
;
1727 HANDLE hInputDoneEvent
;
1728 HANDLE hInputThread
;
1729 uint8_t win_stdio_buf
;
1730 } WinStdioCharState
;
1732 #define NSENDBUF 2048
1733 #define NRECVBUF 2048
1734 #define MAXCONNECT 1
1735 #define NTIMEOUT 5000
1737 static int win_chr_poll(void *opaque
);
1738 static int win_chr_pipe_poll(void *opaque
);
1740 static void win_chr_close(CharDriverState
*chr
)
1742 WinCharState
*s
= chr
->opaque
;
1745 CloseHandle(s
->hsend
);
1749 CloseHandle(s
->hrecv
);
1753 CloseHandle(s
->hcom
);
1757 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1759 qemu_del_polling_cb(win_chr_poll
, chr
);
1761 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1764 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1766 WinCharState
*s
= chr
->opaque
;
1768 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1773 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1775 fprintf(stderr
, "Failed CreateEvent\n");
1778 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1780 fprintf(stderr
, "Failed CreateEvent\n");
1784 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1785 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1786 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1787 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1792 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1793 fprintf(stderr
, "Failed SetupComm\n");
1797 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1798 size
= sizeof(COMMCONFIG
);
1799 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1800 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1801 CommConfigDialog(filename
, NULL
, &comcfg
);
1803 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1804 fprintf(stderr
, "Failed SetCommState\n");
1808 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1809 fprintf(stderr
, "Failed SetCommMask\n");
1813 cto
.ReadIntervalTimeout
= MAXDWORD
;
1814 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1815 fprintf(stderr
, "Failed SetCommTimeouts\n");
1819 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1820 fprintf(stderr
, "Failed ClearCommError\n");
1823 qemu_add_polling_cb(win_chr_poll
, chr
);
1831 /* Called with chr_write_lock held. */
1832 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1834 WinCharState
*s
= chr
->opaque
;
1835 DWORD len
, ret
, size
, err
;
1838 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1839 s
->osend
.hEvent
= s
->hsend
;
1842 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1844 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1846 err
= GetLastError();
1847 if (err
== ERROR_IO_PENDING
) {
1848 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1866 static int win_chr_read_poll(CharDriverState
*chr
)
1868 WinCharState
*s
= chr
->opaque
;
1870 s
->max_size
= qemu_chr_be_can_write(chr
);
1874 static void win_chr_readfile(CharDriverState
*chr
)
1876 WinCharState
*s
= chr
->opaque
;
1878 uint8_t buf
[READ_BUF_LEN
];
1881 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1882 s
->orecv
.hEvent
= s
->hrecv
;
1883 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1885 err
= GetLastError();
1886 if (err
== ERROR_IO_PENDING
) {
1887 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1892 qemu_chr_be_write(chr
, buf
, size
);
1896 static void win_chr_read(CharDriverState
*chr
)
1898 WinCharState
*s
= chr
->opaque
;
1900 if (s
->len
> s
->max_size
)
1901 s
->len
= s
->max_size
;
1905 win_chr_readfile(chr
);
1908 static int win_chr_poll(void *opaque
)
1910 CharDriverState
*chr
= opaque
;
1911 WinCharState
*s
= chr
->opaque
;
1915 ClearCommError(s
->hcom
, &comerr
, &status
);
1916 if (status
.cbInQue
> 0) {
1917 s
->len
= status
.cbInQue
;
1918 win_chr_read_poll(chr
);
1925 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1927 CharDriverState
*chr
;
1930 chr
= qemu_chr_alloc();
1931 s
= g_malloc0(sizeof(WinCharState
));
1933 chr
->chr_write
= win_chr_write
;
1934 chr
->chr_close
= win_chr_close
;
1936 if (win_chr_init(chr
, filename
) < 0) {
1944 static int win_chr_pipe_poll(void *opaque
)
1946 CharDriverState
*chr
= opaque
;
1947 WinCharState
*s
= chr
->opaque
;
1950 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1953 win_chr_read_poll(chr
);
1960 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1962 WinCharState
*s
= chr
->opaque
;
1970 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1972 fprintf(stderr
, "Failed CreateEvent\n");
1975 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1977 fprintf(stderr
, "Failed CreateEvent\n");
1981 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1982 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1983 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1985 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1986 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1987 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1992 ZeroMemory(&ov
, sizeof(ov
));
1993 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1994 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1996 fprintf(stderr
, "Failed ConnectNamedPipe\n");
2000 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
2002 fprintf(stderr
, "Failed GetOverlappedResult\n");
2004 CloseHandle(ov
.hEvent
);
2011 CloseHandle(ov
.hEvent
);
2014 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
2023 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
2025 const char *filename
= opts
->device
;
2026 CharDriverState
*chr
;
2029 chr
= qemu_chr_alloc();
2030 s
= g_malloc0(sizeof(WinCharState
));
2032 chr
->chr_write
= win_chr_write
;
2033 chr
->chr_close
= win_chr_close
;
2035 if (win_chr_pipe_init(chr
, filename
) < 0) {
2043 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
2045 CharDriverState
*chr
;
2048 chr
= qemu_chr_alloc();
2049 s
= g_malloc0(sizeof(WinCharState
));
2052 chr
->chr_write
= win_chr_write
;
2056 static CharDriverState
*qemu_chr_open_win_con(void)
2058 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
2061 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2063 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2070 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2080 static void win_stdio_wait_func(void *opaque
)
2082 CharDriverState
*chr
= opaque
;
2083 WinStdioCharState
*stdio
= chr
->opaque
;
2084 INPUT_RECORD buf
[4];
2089 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2092 /* Avoid error storm */
2093 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2097 for (i
= 0; i
< dwSize
; i
++) {
2098 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2100 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2102 if (kev
->uChar
.AsciiChar
!= 0) {
2103 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2104 if (qemu_chr_be_can_write(chr
)) {
2105 uint8_t c
= kev
->uChar
.AsciiChar
;
2106 qemu_chr_be_write(chr
, &c
, 1);
2114 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2116 CharDriverState
*chr
= param
;
2117 WinStdioCharState
*stdio
= chr
->opaque
;
2123 /* Wait for one byte */
2124 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2126 /* Exit in case of error, continue if nothing read */
2134 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2135 if (stdio
->win_stdio_buf
== '\r') {
2139 /* Signal the main thread and wait until the byte was eaten */
2140 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2143 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2149 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2153 static void win_stdio_thread_wait_func(void *opaque
)
2155 CharDriverState
*chr
= opaque
;
2156 WinStdioCharState
*stdio
= chr
->opaque
;
2158 if (qemu_chr_be_can_write(chr
)) {
2159 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2162 SetEvent(stdio
->hInputDoneEvent
);
2165 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2167 WinStdioCharState
*stdio
= chr
->opaque
;
2170 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2173 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2175 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2179 static void win_stdio_close(CharDriverState
*chr
)
2181 WinStdioCharState
*stdio
= chr
->opaque
;
2183 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2184 CloseHandle(stdio
->hInputReadyEvent
);
2186 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2187 CloseHandle(stdio
->hInputDoneEvent
);
2189 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2190 TerminateThread(stdio
->hInputThread
, 0);
2193 g_free(chr
->opaque
);
2197 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2199 CharDriverState
*chr
;
2200 WinStdioCharState
*stdio
;
2204 chr
= qemu_chr_alloc();
2205 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2207 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2208 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2209 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2213 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2215 chr
->opaque
= stdio
;
2216 chr
->chr_write
= win_stdio_write
;
2217 chr
->chr_close
= win_stdio_close
;
2220 if (qemu_add_wait_object(stdio
->hStdIn
,
2221 win_stdio_wait_func
, chr
)) {
2222 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2227 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2228 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2229 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2232 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2233 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2234 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2235 fprintf(stderr
, "cannot create stdio thread or event\n");
2238 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2239 win_stdio_thread_wait_func
, chr
)) {
2240 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2244 dwMode
|= ENABLE_LINE_INPUT
;
2247 /* set the terminal in raw mode */
2248 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2249 dwMode
|= ENABLE_PROCESSED_INPUT
;
2252 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2254 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2255 qemu_chr_fe_set_echo(chr
, false);
2259 #endif /* !_WIN32 */
2262 /***********************************************************/
2263 /* UDP Net console */
2268 uint8_t buf
[READ_BUF_LEN
];
2274 /* Called with chr_write_lock held. */
2275 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2277 NetCharDriver
*s
= chr
->opaque
;
2278 gsize bytes_written
;
2281 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2282 if (status
== G_IO_STATUS_EOF
) {
2284 } else if (status
!= G_IO_STATUS_NORMAL
) {
2288 return bytes_written
;
2291 static int udp_chr_read_poll(void *opaque
)
2293 CharDriverState
*chr
= opaque
;
2294 NetCharDriver
*s
= chr
->opaque
;
2296 s
->max_size
= qemu_chr_be_can_write(chr
);
2298 /* If there were any stray characters in the queue process them
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
);
2309 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2311 CharDriverState
*chr
= opaque
;
2312 NetCharDriver
*s
= chr
->opaque
;
2313 gsize bytes_read
= 0;
2316 if (s
->max_size
== 0) {
2319 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2321 s
->bufcnt
= bytes_read
;
2322 s
->bufptr
= s
->bufcnt
;
2323 if (status
!= G_IO_STATUS_NORMAL
) {
2324 remove_fd_in_watch(chr
);
2329 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2330 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2332 s
->max_size
= qemu_chr_be_can_write(chr
);
2338 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2340 NetCharDriver
*s
= chr
->opaque
;
2342 remove_fd_in_watch(chr
);
2344 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2349 static void udp_chr_close(CharDriverState
*chr
)
2351 NetCharDriver
*s
= chr
->opaque
;
2353 remove_fd_in_watch(chr
);
2355 g_io_channel_unref(s
->chan
);
2359 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2362 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2364 CharDriverState
*chr
= NULL
;
2365 NetCharDriver
*s
= NULL
;
2367 chr
= qemu_chr_alloc();
2368 s
= g_malloc0(sizeof(NetCharDriver
));
2371 s
->chan
= io_channel_from_socket(s
->fd
);
2375 chr
->chr_write
= udp_chr_write
;
2376 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2377 chr
->chr_close
= udp_chr_close
;
2378 /* be isn't opened until we get a connection */
2379 chr
->explicit_be_open
= true;
2383 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2385 Error
*local_err
= NULL
;
2388 fd
= inet_dgram_opts(opts
, &local_err
);
2390 qerror_report_err(local_err
);
2391 error_free(local_err
);
2394 return qemu_chr_open_udp_fd(fd
);
2397 /***********************************************************/
2398 /* TCP Net console */
2402 GIOChannel
*chan
, *listen_chan
;
2411 int read_msgfds_num
;
2413 int write_msgfds_num
;
2416 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2419 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2421 TCPCharDriver
*s
= chr
->opaque
;
2426 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2427 char control
[CMSG_SPACE(fd_size
)];
2428 struct cmsghdr
*cmsg
;
2430 memset(&msgh
, 0, sizeof(msgh
));
2431 memset(control
, 0, sizeof(control
));
2433 /* set the payload */
2434 iov
.iov_base
= (uint8_t *) buf
;
2437 msgh
.msg_iov
= &iov
;
2438 msgh
.msg_iovlen
= 1;
2440 msgh
.msg_control
= control
;
2441 msgh
.msg_controllen
= sizeof(control
);
2443 cmsg
= CMSG_FIRSTHDR(&msgh
);
2445 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2446 cmsg
->cmsg_level
= SOL_SOCKET
;
2447 cmsg
->cmsg_type
= SCM_RIGHTS
;
2448 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2451 r
= sendmsg(s
->fd
, &msgh
, 0);
2452 } while (r
< 0 && errno
== EINTR
);
2454 /* free the written msgfds, no matter what */
2455 if (s
->write_msgfds_num
) {
2456 g_free(s
->write_msgfds
);
2457 s
->write_msgfds
= 0;
2458 s
->write_msgfds_num
= 0;
2465 /* Called with chr_write_lock held. */
2466 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2468 TCPCharDriver
*s
= chr
->opaque
;
2471 if (s
->is_unix
&& s
->write_msgfds_num
) {
2472 return unix_send_msgfds(chr
, buf
, len
);
2476 return io_channel_send(s
->chan
, buf
, len
);
2479 /* XXX: indicate an error ? */
2484 static int tcp_chr_read_poll(void *opaque
)
2486 CharDriverState
*chr
= opaque
;
2487 TCPCharDriver
*s
= chr
->opaque
;
2490 s
->max_size
= qemu_chr_be_can_write(chr
);
2495 #define IAC_BREAK 243
2496 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2498 uint8_t *buf
, int *size
)
2500 /* Handle any telnet client's basic IAC options to satisfy char by
2501 * char mode with no echo. All IAC options will be removed from
2502 * the buf and the do_telnetopt variable will be used to track the
2503 * state of the width of the IAC information.
2505 * IAC commands come in sets of 3 bytes with the exception of the
2506 * "IAC BREAK" command and the double IAC.
2512 for (i
= 0; i
< *size
; i
++) {
2513 if (s
->do_telnetopt
> 1) {
2514 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2515 /* Double IAC means send an IAC */
2519 s
->do_telnetopt
= 1;
2521 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2522 /* Handle IAC break commands by sending a serial break */
2523 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2528 if (s
->do_telnetopt
>= 4) {
2529 s
->do_telnetopt
= 1;
2532 if ((unsigned char)buf
[i
] == IAC
) {
2533 s
->do_telnetopt
= 2;
2544 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2546 TCPCharDriver
*s
= chr
->opaque
;
2547 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2552 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2554 /* Close unused fds */
2555 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2556 close(s
->read_msgfds
[i
]);
2559 g_free(s
->read_msgfds
);
2561 s
->read_msgfds_num
= 0;
2567 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2569 TCPCharDriver
*s
= chr
->opaque
;
2571 /* clear old pending fd array */
2572 if (s
->write_msgfds
) {
2573 g_free(s
->write_msgfds
);
2577 s
->write_msgfds
= g_malloc(num
* sizeof(int));
2578 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2581 s
->write_msgfds_num
= num
;
2587 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2589 TCPCharDriver
*s
= chr
->opaque
;
2590 struct cmsghdr
*cmsg
;
2592 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2595 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2596 cmsg
->cmsg_level
!= SOL_SOCKET
||
2597 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2601 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2607 /* close and clean read_msgfds */
2608 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2609 close(s
->read_msgfds
[i
]);
2612 if (s
->read_msgfds_num
) {
2613 g_free(s
->read_msgfds
);
2616 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2617 s
->read_msgfds
= g_malloc(fd_size
);
2618 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2620 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2621 int fd
= s
->read_msgfds
[i
];
2626 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2629 #ifndef MSG_CMSG_CLOEXEC
2630 qemu_set_cloexec(fd
);
2636 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2638 TCPCharDriver
*s
= chr
->opaque
;
2639 struct msghdr msg
= { NULL
, };
2640 struct iovec iov
[1];
2642 struct cmsghdr cmsg
;
2643 char control
[CMSG_SPACE(sizeof(int))];
2648 iov
[0].iov_base
= buf
;
2649 iov
[0].iov_len
= len
;
2653 msg
.msg_control
= &msg_control
;
2654 msg
.msg_controllen
= sizeof(msg_control
);
2656 #ifdef MSG_CMSG_CLOEXEC
2657 flags
|= MSG_CMSG_CLOEXEC
;
2659 ret
= recvmsg(s
->fd
, &msg
, flags
);
2660 if (ret
> 0 && s
->is_unix
) {
2661 unix_process_msgfd(chr
, &msg
);
2667 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2669 TCPCharDriver
*s
= chr
->opaque
;
2670 return qemu_recv(s
->fd
, buf
, len
, 0);
2674 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2676 TCPCharDriver
*s
= chr
->opaque
;
2677 return g_io_create_watch(s
->chan
, cond
);
2680 static void tcp_chr_disconnect(CharDriverState
*chr
)
2682 TCPCharDriver
*s
= chr
->opaque
;
2685 if (s
->listen_chan
) {
2686 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2687 tcp_chr_accept
, chr
);
2689 remove_fd_in_watch(chr
);
2690 g_io_channel_unref(s
->chan
);
2694 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2697 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2699 CharDriverState
*chr
= opaque
;
2700 TCPCharDriver
*s
= chr
->opaque
;
2701 uint8_t buf
[READ_BUF_LEN
];
2704 if (cond
& G_IO_HUP
) {
2705 /* connection closed */
2706 tcp_chr_disconnect(chr
);
2710 if (!s
->connected
|| s
->max_size
<= 0) {
2714 if (len
> s
->max_size
)
2716 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2718 /* connection closed */
2719 tcp_chr_disconnect(chr
);
2720 } else if (size
> 0) {
2721 if (s
->do_telnetopt
)
2722 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2724 qemu_chr_be_write(chr
, buf
, size
);
2730 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2732 TCPCharDriver
*s
= chr
->opaque
;
2735 if (!s
->connected
) {
2739 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2741 /* connection closed */
2742 tcp_chr_disconnect(chr
);
2749 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2751 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2754 chr
->avail_connections
= 1;
2761 static void tcp_chr_connect(void *opaque
)
2763 CharDriverState
*chr
= opaque
;
2764 TCPCharDriver
*s
= chr
->opaque
;
2768 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2771 qemu_chr_be_generic_open(chr
);
2774 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2776 TCPCharDriver
*s
= chr
->opaque
;
2778 remove_fd_in_watch(chr
);
2780 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2785 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2786 static void tcp_chr_telnet_init(int fd
)
2789 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2790 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2791 send(fd
, (char *)buf
, 3, 0);
2792 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2793 send(fd
, (char *)buf
, 3, 0);
2794 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2795 send(fd
, (char *)buf
, 3, 0);
2796 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2797 send(fd
, (char *)buf
, 3, 0);
2800 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2802 TCPCharDriver
*s
= chr
->opaque
;
2806 qemu_set_nonblock(fd
);
2808 socket_set_nodelay(fd
);
2810 s
->chan
= io_channel_from_socket(fd
);
2811 if (s
->listen_tag
) {
2812 g_source_remove(s
->listen_tag
);
2815 tcp_chr_connect(chr
);
2820 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2822 CharDriverState
*chr
= opaque
;
2823 TCPCharDriver
*s
= chr
->opaque
;
2824 struct sockaddr_in saddr
;
2826 struct sockaddr_un uaddr
;
2828 struct sockaddr
*addr
;
2835 len
= sizeof(uaddr
);
2836 addr
= (struct sockaddr
*)&uaddr
;
2840 len
= sizeof(saddr
);
2841 addr
= (struct sockaddr
*)&saddr
;
2843 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2844 if (fd
< 0 && errno
!= EINTR
) {
2847 } else if (fd
>= 0) {
2848 if (s
->do_telnetopt
)
2849 tcp_chr_telnet_init(fd
);
2853 if (tcp_chr_add_client(chr
, fd
) < 0)
2859 static void tcp_chr_close(CharDriverState
*chr
)
2861 TCPCharDriver
*s
= chr
->opaque
;
2864 remove_fd_in_watch(chr
);
2866 g_io_channel_unref(s
->chan
);
2870 if (s
->listen_fd
>= 0) {
2871 if (s
->listen_tag
) {
2872 g_source_remove(s
->listen_tag
);
2875 if (s
->listen_chan
) {
2876 g_io_channel_unref(s
->listen_chan
);
2878 closesocket(s
->listen_fd
);
2880 if (s
->read_msgfds_num
) {
2881 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2882 close(s
->read_msgfds
[i
]);
2884 g_free(s
->read_msgfds
);
2886 if (s
->write_msgfds_num
) {
2887 g_free(s
->write_msgfds
);
2890 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2893 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2894 bool is_listen
, bool is_telnet
,
2895 bool is_waitconnect
,
2898 CharDriverState
*chr
= NULL
;
2899 TCPCharDriver
*s
= NULL
;
2900 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2901 const char *left
= "", *right
= "";
2902 struct sockaddr_storage ss
;
2903 socklen_t ss_len
= sizeof(ss
);
2905 memset(&ss
, 0, ss_len
);
2906 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2907 error_setg_errno(errp
, errno
, "getsockname");
2911 chr
= qemu_chr_alloc();
2912 s
= g_malloc0(sizeof(TCPCharDriver
));
2918 s
->read_msgfds_num
= 0;
2919 s
->write_msgfds
= 0;
2920 s
->write_msgfds_num
= 0;
2922 chr
->filename
= g_malloc(256);
2923 switch (ss
.ss_family
) {
2927 snprintf(chr
->filename
, 256, "unix:%s%s",
2928 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2929 is_listen
? ",server" : "");
2937 s
->do_nodelay
= do_nodelay
;
2938 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2939 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2940 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2941 is_telnet
? "telnet" : "tcp",
2942 left
, host
, right
, serv
,
2943 is_listen
? ",server" : "");
2948 chr
->chr_write
= tcp_chr_write
;
2949 chr
->chr_sync_read
= tcp_chr_sync_read
;
2950 chr
->chr_close
= tcp_chr_close
;
2951 chr
->get_msgfds
= tcp_get_msgfds
;
2952 chr
->set_msgfds
= tcp_set_msgfds
;
2953 chr
->chr_add_client
= tcp_chr_add_client
;
2954 chr
->chr_add_watch
= tcp_chr_add_watch
;
2955 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
2956 /* be isn't opened until we get a connection */
2957 chr
->explicit_be_open
= true;
2961 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2962 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2964 s
->do_telnetopt
= 1;
2969 socket_set_nodelay(fd
);
2970 s
->chan
= io_channel_from_socket(s
->fd
);
2971 tcp_chr_connect(chr
);
2974 if (is_listen
&& is_waitconnect
) {
2975 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2977 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2978 qemu_set_nonblock(s
->listen_fd
);
2983 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2985 CharDriverState
*chr
= NULL
;
2986 Error
*local_err
= NULL
;
2989 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2990 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2991 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2992 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2993 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2997 fd
= unix_listen_opts(opts
, &local_err
);
2999 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
3003 fd
= inet_listen_opts(opts
, 0, &local_err
);
3005 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
3012 if (!is_waitconnect
)
3013 qemu_set_nonblock(fd
);
3015 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
3016 is_waitconnect
, &local_err
);
3025 qerror_report_err(local_err
);
3026 error_free(local_err
);
3032 g_free(chr
->opaque
);
3038 /*********************************************************/
3039 /* Ring buffer chardev */
3046 } RingBufCharDriver
;
3048 static size_t ringbuf_count(const CharDriverState
*chr
)
3050 const RingBufCharDriver
*d
= chr
->opaque
;
3052 return d
->prod
- d
->cons
;
3055 /* Called with chr_write_lock held. */
3056 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3058 RingBufCharDriver
*d
= chr
->opaque
;
3061 if (!buf
|| (len
< 0)) {
3065 for (i
= 0; i
< len
; i
++ ) {
3066 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3067 if (d
->prod
- d
->cons
> d
->size
) {
3068 d
->cons
= d
->prod
- d
->size
;
3075 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3077 RingBufCharDriver
*d
= chr
->opaque
;
3080 qemu_mutex_lock(&chr
->chr_write_lock
);
3081 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3082 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3084 qemu_mutex_unlock(&chr
->chr_write_lock
);
3089 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3091 RingBufCharDriver
*d
= chr
->opaque
;
3098 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3101 CharDriverState
*chr
;
3102 RingBufCharDriver
*d
;
3104 chr
= qemu_chr_alloc();
3105 d
= g_malloc(sizeof(*d
));
3107 d
->size
= opts
->has_size
? opts
->size
: 65536;
3109 /* The size must be power of 2 */
3110 if (d
->size
& (d
->size
- 1)) {
3111 error_setg(errp
, "size of ringbuf chardev must be power of two");
3117 d
->cbuf
= g_malloc0(d
->size
);
3120 chr
->chr_write
= ringbuf_chr_write
;
3121 chr
->chr_close
= ringbuf_chr_close
;
3131 bool chr_is_ringbuf(const CharDriverState
*chr
)
3133 return chr
->chr_write
== ringbuf_chr_write
;
3136 void qmp_ringbuf_write(const char *device
, const char *data
,
3137 bool has_format
, enum DataFormat format
,
3140 CharDriverState
*chr
;
3141 const uint8_t *write_data
;
3145 chr
= qemu_chr_find(device
);
3147 error_setg(errp
, "Device '%s' not found", device
);
3151 if (!chr_is_ringbuf(chr
)) {
3152 error_setg(errp
,"%s is not a ringbuf device", device
);
3156 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3157 write_data
= g_base64_decode(data
, &write_count
);
3159 write_data
= (uint8_t *)data
;
3160 write_count
= strlen(data
);
3163 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3165 if (write_data
!= (uint8_t *)data
) {
3166 g_free((void *)write_data
);
3170 error_setg(errp
, "Failed to write to device %s", device
);
3175 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3176 bool has_format
, enum DataFormat format
,
3179 CharDriverState
*chr
;
3184 chr
= qemu_chr_find(device
);
3186 error_setg(errp
, "Device '%s' not found", device
);
3190 if (!chr_is_ringbuf(chr
)) {
3191 error_setg(errp
,"%s is not a ringbuf device", device
);
3196 error_setg(errp
, "size must be greater than zero");
3200 count
= ringbuf_count(chr
);
3201 size
= size
> count
? count
: size
;
3202 read_data
= g_malloc(size
+ 1);
3204 ringbuf_chr_read(chr
, read_data
, size
);
3206 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3207 data
= g_base64_encode(read_data
, size
);
3211 * FIXME should read only complete, valid UTF-8 characters up
3212 * to @size bytes. Invalid sequences should be replaced by a
3213 * suitable replacement character. Except when (and only
3214 * when) ring buffer lost characters since last read, initial
3215 * continuation characters should be dropped.
3217 read_data
[size
] = 0;
3218 data
= (char *)read_data
;
3224 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3226 char host
[65], port
[33], width
[8], height
[8];
3230 Error
*local_err
= NULL
;
3232 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3234 qerror_report_err(local_err
);
3235 error_free(local_err
);
3239 if (strstart(filename
, "mon:", &p
)) {
3241 qemu_opt_set(opts
, "mux", "on");
3242 if (strcmp(filename
, "stdio") == 0) {
3243 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3244 * but pass it to the guest. Handle this only for compat syntax,
3245 * for -chardev syntax we have special option for this.
3246 * This is what -nographic did, redirecting+muxing serial+monitor
3247 * to stdio causing Ctrl+C to be passed to guest. */
3248 qemu_opt_set(opts
, "signal", "off");
3252 if (strcmp(filename
, "null") == 0 ||
3253 strcmp(filename
, "pty") == 0 ||
3254 strcmp(filename
, "msmouse") == 0 ||
3255 strcmp(filename
, "braille") == 0 ||
3256 strcmp(filename
, "stdio") == 0) {
3257 qemu_opt_set(opts
, "backend", filename
);
3260 if (strstart(filename
, "vc", &p
)) {
3261 qemu_opt_set(opts
, "backend", "vc");
3263 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3265 qemu_opt_set(opts
, "width", width
);
3266 qemu_opt_set(opts
, "height", height
);
3267 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3269 qemu_opt_set(opts
, "cols", width
);
3270 qemu_opt_set(opts
, "rows", height
);
3277 if (strcmp(filename
, "con:") == 0) {
3278 qemu_opt_set(opts
, "backend", "console");
3281 if (strstart(filename
, "COM", NULL
)) {
3282 qemu_opt_set(opts
, "backend", "serial");
3283 qemu_opt_set(opts
, "path", filename
);
3286 if (strstart(filename
, "file:", &p
)) {
3287 qemu_opt_set(opts
, "backend", "file");
3288 qemu_opt_set(opts
, "path", p
);
3291 if (strstart(filename
, "pipe:", &p
)) {
3292 qemu_opt_set(opts
, "backend", "pipe");
3293 qemu_opt_set(opts
, "path", p
);
3296 if (strstart(filename
, "tcp:", &p
) ||
3297 strstart(filename
, "telnet:", &p
)) {
3298 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3300 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3303 qemu_opt_set(opts
, "backend", "socket");
3304 qemu_opt_set(opts
, "host", host
);
3305 qemu_opt_set(opts
, "port", port
);
3306 if (p
[pos
] == ',') {
3307 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3310 if (strstart(filename
, "telnet:", &p
))
3311 qemu_opt_set(opts
, "telnet", "on");
3314 if (strstart(filename
, "udp:", &p
)) {
3315 qemu_opt_set(opts
, "backend", "udp");
3316 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3318 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3322 qemu_opt_set(opts
, "host", host
);
3323 qemu_opt_set(opts
, "port", port
);
3324 if (p
[pos
] == '@') {
3326 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3328 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3332 qemu_opt_set(opts
, "localaddr", host
);
3333 qemu_opt_set(opts
, "localport", port
);
3337 if (strstart(filename
, "unix:", &p
)) {
3338 qemu_opt_set(opts
, "backend", "socket");
3339 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3343 if (strstart(filename
, "/dev/parport", NULL
) ||
3344 strstart(filename
, "/dev/ppi", NULL
)) {
3345 qemu_opt_set(opts
, "backend", "parport");
3346 qemu_opt_set(opts
, "path", filename
);
3349 if (strstart(filename
, "/dev/", NULL
)) {
3350 qemu_opt_set(opts
, "backend", "tty");
3351 qemu_opt_set(opts
, "path", filename
);
3356 qemu_opts_del(opts
);
3360 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3363 const char *path
= qemu_opt_get(opts
, "path");
3366 error_setg(errp
, "chardev: file: no filename given");
3369 backend
->file
= g_new0(ChardevFile
, 1);
3370 backend
->file
->out
= g_strdup(path
);
3373 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3376 backend
->stdio
= g_new0(ChardevStdio
, 1);
3377 backend
->stdio
->has_signal
= true;
3378 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3381 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3384 const char *device
= qemu_opt_get(opts
, "path");
3386 if (device
== NULL
) {
3387 error_setg(errp
, "chardev: serial/tty: no device path given");
3390 backend
->serial
= g_new0(ChardevHostdev
, 1);
3391 backend
->serial
->device
= g_strdup(device
);
3394 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3397 const char *device
= qemu_opt_get(opts
, "path");
3399 if (device
== NULL
) {
3400 error_setg(errp
, "chardev: parallel: no device path given");
3403 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3404 backend
->parallel
->device
= g_strdup(device
);
3407 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3410 const char *device
= qemu_opt_get(opts
, "path");
3412 if (device
== NULL
) {
3413 error_setg(errp
, "chardev: pipe: no device path given");
3416 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3417 backend
->pipe
->device
= g_strdup(device
);
3420 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3425 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3427 val
= qemu_opt_get_size(opts
, "size", 0);
3429 backend
->ringbuf
->has_size
= true;
3430 backend
->ringbuf
->size
= val
;
3434 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3437 const char *chardev
= qemu_opt_get(opts
, "chardev");
3439 if (chardev
== NULL
) {
3440 error_setg(errp
, "chardev: mux: no chardev given");
3443 backend
->mux
= g_new0(ChardevMux
, 1);
3444 backend
->mux
->chardev
= g_strdup(chardev
);
3447 typedef struct CharDriver
{
3450 CharDriverState
*(*open
)(QemuOpts
*opts
);
3451 /* new, qapi-based */
3452 ChardevBackendKind kind
;
3453 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3456 static GSList
*backends
;
3458 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3462 s
= g_malloc0(sizeof(*s
));
3463 s
->name
= g_strdup(name
);
3466 backends
= g_slist_append(backends
, s
);
3469 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3470 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3474 s
= g_malloc0(sizeof(*s
));
3475 s
->name
= g_strdup(name
);
3479 backends
= g_slist_append(backends
, s
);
3482 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3483 void (*init
)(struct CharDriverState
*s
),
3486 Error
*local_err
= NULL
;
3488 CharDriverState
*chr
;
3491 if (qemu_opts_id(opts
) == NULL
) {
3492 error_setg(errp
, "chardev: no id specified");
3496 if (qemu_opt_get(opts
, "backend") == NULL
) {
3497 error_setg(errp
, "chardev: \"%s\" missing backend",
3498 qemu_opts_id(opts
));
3501 for (i
= backends
; i
; i
= i
->next
) {
3504 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3509 error_setg(errp
, "chardev: backend \"%s\" not found",
3510 qemu_opt_get(opts
, "backend"));
3515 /* using new, qapi init */
3516 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3517 ChardevReturn
*ret
= NULL
;
3518 const char *id
= qemu_opts_id(opts
);
3521 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3522 bid
= g_strdup_printf("%s-base", id
);
3526 backend
->kind
= cd
->kind
;
3528 cd
->parse(opts
, backend
, &local_err
);
3530 error_propagate(errp
, local_err
);
3534 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3540 qapi_free_ChardevBackend(backend
);
3541 qapi_free_ChardevReturn(ret
);
3542 backend
= g_new0(ChardevBackend
, 1);
3543 backend
->mux
= g_new0(ChardevMux
, 1);
3544 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3545 backend
->mux
->chardev
= g_strdup(bid
);
3546 ret
= qmp_chardev_add(id
, backend
, errp
);
3548 chr
= qemu_chr_find(bid
);
3549 qemu_chr_delete(chr
);
3555 chr
= qemu_chr_find(id
);
3559 qapi_free_ChardevBackend(backend
);
3560 qapi_free_ChardevReturn(ret
);
3565 chr
= cd
->open(opts
);
3567 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3568 qemu_opt_get(opts
, "backend"));
3573 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3575 /* if we didn't create the chardev via qmp_chardev_add, we
3576 * need to send the OPENED event here
3578 if (!chr
->explicit_be_open
) {
3579 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3581 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3583 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3584 CharDriverState
*base
= chr
;
3585 int len
= strlen(qemu_opts_id(opts
)) + 6;
3586 base
->label
= g_malloc(len
);
3587 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3588 chr
= qemu_chr_open_mux(base
);
3589 chr
->filename
= base
->filename
;
3590 chr
->avail_connections
= MAX_MUX
;
3591 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3593 chr
->avail_connections
= 1;
3595 chr
->label
= g_strdup(qemu_opts_id(opts
));
3600 qemu_opts_del(opts
);
3604 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3607 CharDriverState
*chr
;
3611 if (strstart(filename
, "chardev:", &p
)) {
3612 return qemu_chr_find(p
);
3615 opts
= qemu_chr_parse_compat(label
, filename
);
3619 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3621 error_report("%s", error_get_pretty(err
));
3624 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3625 qemu_chr_fe_claim_no_fail(chr
);
3626 monitor_init(chr
, MONITOR_USE_READLINE
);
3631 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3633 if (chr
->chr_set_echo
) {
3634 chr
->chr_set_echo(chr
, echo
);
3638 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3640 if (chr
->fe_open
== fe_open
) {
3643 chr
->fe_open
= fe_open
;
3644 if (chr
->chr_set_fe_open
) {
3645 chr
->chr_set_fe_open(chr
, fe_open
);
3649 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3651 if (chr
->chr_fe_event
) {
3652 chr
->chr_fe_event(chr
, event
);
3656 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3657 GIOFunc func
, void *user_data
)
3662 if (s
->chr_add_watch
== NULL
) {
3666 src
= s
->chr_add_watch(s
, cond
);
3667 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3668 tag
= g_source_attach(src
, NULL
);
3669 g_source_unref(src
);
3674 int qemu_chr_fe_claim(CharDriverState
*s
)
3676 if (s
->avail_connections
< 1) {
3679 s
->avail_connections
--;
3683 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3685 if (qemu_chr_fe_claim(s
) != 0) {
3686 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3687 __func__
, s
->label
);
3692 void qemu_chr_fe_release(CharDriverState
*s
)
3694 s
->avail_connections
++;
3697 void qemu_chr_delete(CharDriverState
*chr
)
3699 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3700 if (chr
->chr_close
) {
3701 chr
->chr_close(chr
);
3703 g_free(chr
->filename
);
3706 qemu_opts_del(chr
->opts
);
3711 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3713 ChardevInfoList
*chr_list
= NULL
;
3714 CharDriverState
*chr
;
3716 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3717 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3718 info
->value
= g_malloc0(sizeof(*info
->value
));
3719 info
->value
->label
= g_strdup(chr
->label
);
3720 info
->value
->filename
= g_strdup(chr
->filename
);
3721 info
->value
->frontend_open
= chr
->fe_open
;
3723 info
->next
= chr_list
;
3730 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3732 ChardevBackendInfoList
*backend_list
= NULL
;
3733 CharDriver
*c
= NULL
;
3736 for (i
= backends
; i
; i
= i
->next
) {
3737 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3739 info
->value
= g_malloc0(sizeof(*info
->value
));
3740 info
->value
->name
= g_strdup(c
->name
);
3742 info
->next
= backend_list
;
3743 backend_list
= info
;
3746 return backend_list
;
3749 CharDriverState
*qemu_chr_find(const char *name
)
3751 CharDriverState
*chr
;
3753 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3754 if (strcmp(chr
->label
, name
) != 0)
3761 /* Get a character (serial) device interface. */
3762 CharDriverState
*qemu_char_get_next_serial(void)
3764 static int next_serial
;
3765 CharDriverState
*chr
;
3767 /* FIXME: This function needs to go away: use chardev properties! */
3769 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3770 chr
= serial_hds
[next_serial
++];
3771 qemu_chr_fe_claim_no_fail(chr
);
3777 QemuOptsList qemu_chardev_opts
= {
3779 .implied_opt_name
= "backend",
3780 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3784 .type
= QEMU_OPT_STRING
,
3787 .type
= QEMU_OPT_STRING
,
3790 .type
= QEMU_OPT_STRING
,
3793 .type
= QEMU_OPT_STRING
,
3795 .name
= "localaddr",
3796 .type
= QEMU_OPT_STRING
,
3798 .name
= "localport",
3799 .type
= QEMU_OPT_STRING
,
3802 .type
= QEMU_OPT_NUMBER
,
3805 .type
= QEMU_OPT_BOOL
,
3808 .type
= QEMU_OPT_BOOL
,
3811 .type
= QEMU_OPT_BOOL
,
3814 .type
= QEMU_OPT_BOOL
,
3817 .type
= QEMU_OPT_BOOL
,
3820 .type
= QEMU_OPT_BOOL
,
3823 .type
= QEMU_OPT_NUMBER
,
3826 .type
= QEMU_OPT_NUMBER
,
3829 .type
= QEMU_OPT_NUMBER
,
3832 .type
= QEMU_OPT_NUMBER
,
3835 .type
= QEMU_OPT_BOOL
,
3838 .type
= QEMU_OPT_BOOL
,
3841 .type
= QEMU_OPT_STRING
,
3844 .type
= QEMU_OPT_NUMBER
,
3847 .type
= QEMU_OPT_SIZE
,
3850 .type
= QEMU_OPT_STRING
,
3852 { /* end of list */ }
3858 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3863 error_setg(errp
, "input file not supported");
3867 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3868 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3869 if (out
== INVALID_HANDLE_VALUE
) {
3870 error_setg(errp
, "open %s failed", file
->out
);
3873 return qemu_chr_open_win_file(out
);
3876 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3879 return qemu_chr_open_win_path(serial
->device
);
3882 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3885 error_setg(errp
, "character device backend type 'parallel' not supported");
3891 static int qmp_chardev_open_file_source(char *src
, int flags
,
3896 TFR(fd
= qemu_open(src
, flags
, 0666));
3898 error_setg_file_open(errp
, errno
, src
);
3903 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3905 int flags
, in
= -1, out
;
3907 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3908 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3915 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3922 return qemu_chr_open_fd(in
, out
);
3925 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3928 #ifdef HAVE_CHARDEV_TTY
3931 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3935 qemu_set_nonblock(fd
);
3936 return qemu_chr_open_tty_fd(fd
);
3938 error_setg(errp
, "character device backend type 'serial' not supported");
3943 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3946 #ifdef HAVE_CHARDEV_PARPORT
3949 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3953 return qemu_chr_open_pp_fd(fd
);
3955 error_setg(errp
, "character device backend type 'parallel' not supported");
3962 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3965 SocketAddress
*addr
= sock
->addr
;
3966 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3967 bool is_listen
= sock
->has_server
? sock
->server
: true;
3968 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3969 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3973 fd
= socket_listen(addr
, errp
);
3975 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3980 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3981 is_telnet
, is_waitconnect
, errp
);
3984 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3989 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3993 return qemu_chr_open_udp_fd(fd
);
3996 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3999 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
4000 CharDriverState
*base
, *chr
= NULL
;
4002 chr
= qemu_chr_find(id
);
4004 error_setg(errp
, "Chardev '%s' already exists", id
);
4009 switch (backend
->kind
) {
4010 case CHARDEV_BACKEND_KIND_FILE
:
4011 chr
= qmp_chardev_open_file(backend
->file
, errp
);
4013 case CHARDEV_BACKEND_KIND_SERIAL
:
4014 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
4016 case CHARDEV_BACKEND_KIND_PARALLEL
:
4017 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
4019 case CHARDEV_BACKEND_KIND_PIPE
:
4020 chr
= qemu_chr_open_pipe(backend
->pipe
);
4022 case CHARDEV_BACKEND_KIND_SOCKET
:
4023 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
4025 case CHARDEV_BACKEND_KIND_UDP
:
4026 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
4028 #ifdef HAVE_CHARDEV_TTY
4029 case CHARDEV_BACKEND_KIND_PTY
:
4030 chr
= qemu_chr_open_pty(id
, ret
);
4033 case CHARDEV_BACKEND_KIND_NULL
:
4034 chr
= qemu_chr_open_null();
4036 case CHARDEV_BACKEND_KIND_MUX
:
4037 base
= qemu_chr_find(backend
->mux
->chardev
);
4039 error_setg(errp
, "mux: base chardev %s not found",
4040 backend
->mux
->chardev
);
4043 chr
= qemu_chr_open_mux(base
);
4045 case CHARDEV_BACKEND_KIND_MSMOUSE
:
4046 chr
= qemu_chr_open_msmouse();
4048 #ifdef CONFIG_BRLAPI
4049 case CHARDEV_BACKEND_KIND_BRAILLE
:
4050 chr
= chr_baum_init();
4053 case CHARDEV_BACKEND_KIND_STDIO
:
4054 chr
= qemu_chr_open_stdio(backend
->stdio
);
4057 case CHARDEV_BACKEND_KIND_CONSOLE
:
4058 chr
= qemu_chr_open_win_con();
4062 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4063 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
4065 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4066 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
4069 case CHARDEV_BACKEND_KIND_VC
:
4070 chr
= vc_init(backend
->vc
);
4072 case CHARDEV_BACKEND_KIND_RINGBUF
:
4073 case CHARDEV_BACKEND_KIND_MEMORY
:
4074 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
4077 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4082 * Character backend open hasn't been fully converted to the Error
4083 * API. Some opens fail without setting an error. Set a generic
4085 * TODO full conversion to Error API
4087 if (chr
== NULL
&& errp
&& !*errp
) {
4088 error_setg(errp
, "Failed to create chardev");
4091 chr
->label
= g_strdup(id
);
4092 chr
->avail_connections
=
4093 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4094 if (!chr
->filename
) {
4095 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4097 if (!chr
->explicit_be_open
) {
4098 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4100 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4108 void qmp_chardev_remove(const char *id
, Error
**errp
)
4110 CharDriverState
*chr
;
4112 chr
= qemu_chr_find(id
);
4114 error_setg(errp
, "Chardev '%s' not found", id
);
4117 if (chr
->chr_can_read
|| chr
->chr_read
||
4118 chr
->chr_event
|| chr
->handler_opaque
) {
4119 error_setg(errp
, "Chardev '%s' is busy", id
);
4122 qemu_chr_delete(chr
);
4125 static void register_types(void)
4127 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
4128 register_char_driver("socket", qemu_chr_open_socket
);
4129 register_char_driver("udp", qemu_chr_open_udp
);
4130 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4131 qemu_chr_parse_ringbuf
);
4132 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
4133 qemu_chr_parse_file_out
);
4134 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4135 qemu_chr_parse_stdio
);
4136 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4137 qemu_chr_parse_serial
);
4138 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4139 qemu_chr_parse_serial
);
4140 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4141 qemu_chr_parse_parallel
);
4142 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4143 qemu_chr_parse_parallel
);
4144 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
4145 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
4146 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4147 qemu_chr_parse_pipe
);
4148 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
4149 qemu_chr_parse_mux
);
4150 /* Bug-compatibility: */
4151 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4152 qemu_chr_parse_ringbuf
);
4153 /* this must be done after machine init, since we register FEs with muxes
4154 * as part of realize functions like serial_isa_realizefn when -nographic
4157 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4160 type_init(register_types
);