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 "ui/console.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/timer.h"
29 #include "sysemu/char.h"
31 #include "qmp-commands.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <arpa/inet.h>
53 #include <sys/select.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57 #include <dev/ppbus/ppi.h>
58 #include <dev/ppbus/ppbconf.h>
59 #elif defined(__DragonFly__)
60 #include <dev/misc/ppi/ppi.h>
61 #include <bus/ppbus/ppbconf.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
83 #include "qemu/sockets.h"
84 #include "ui/qemu-spice.h"
86 #define READ_BUF_LEN 4096
88 /***********************************************************/
89 /* character device */
91 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
92 QTAILQ_HEAD_INITIALIZER(chardevs
);
94 void qemu_chr_be_event(CharDriverState
*s
, int event
)
96 /* Keep track if the char device is open */
98 case CHR_EVENT_OPENED
:
101 case CHR_EVENT_CLOSED
:
108 s
->chr_event(s
->handler_opaque
, event
);
111 void qemu_chr_be_generic_open(CharDriverState
*s
)
113 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
116 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
118 return s
->chr_write(s
, buf
, len
);
121 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
126 while (offset
< len
) {
128 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
129 if (res
== -1 && errno
== EAGAIN
) {
132 } while (res
== -1 && errno
== EAGAIN
);
148 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
152 return s
->chr_ioctl(s
, cmd
, arg
);
155 int qemu_chr_be_can_write(CharDriverState
*s
)
157 if (!s
->chr_can_read
)
159 return s
->chr_can_read(s
->handler_opaque
);
162 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
165 s
->chr_read(s
->handler_opaque
, buf
, len
);
169 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
171 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
174 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
176 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
179 void qemu_chr_accept_input(CharDriverState
*s
)
181 if (s
->chr_accept_input
)
182 s
->chr_accept_input(s
);
186 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
188 char buf
[READ_BUF_LEN
];
191 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
192 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
196 static void remove_fd_in_watch(CharDriverState
*chr
);
198 void qemu_chr_add_handlers(CharDriverState
*s
,
199 IOCanReadHandler
*fd_can_read
,
200 IOReadHandler
*fd_read
,
201 IOEventHandler
*fd_event
,
206 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
208 remove_fd_in_watch(s
);
212 s
->chr_can_read
= fd_can_read
;
213 s
->chr_read
= fd_read
;
214 s
->chr_event
= fd_event
;
215 s
->handler_opaque
= opaque
;
216 if (s
->chr_update_read_handler
)
217 s
->chr_update_read_handler(s
);
219 if (!s
->explicit_fe_open
) {
220 qemu_chr_fe_set_open(s
, fe_open
);
223 /* We're connecting to an already opened device, so let's make sure we
224 also get the open event */
225 if (fe_open
&& s
->be_open
) {
226 qemu_chr_be_generic_open(s
);
230 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
235 static CharDriverState
*qemu_chr_open_null(void)
237 CharDriverState
*chr
;
239 chr
= g_malloc0(sizeof(CharDriverState
));
240 chr
->chr_write
= null_chr_write
;
241 chr
->explicit_be_open
= true;
245 /* MUX driver for serial I/O splitting */
247 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
248 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
250 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
251 IOReadHandler
*chr_read
[MAX_MUX
];
252 IOEventHandler
*chr_event
[MAX_MUX
];
253 void *ext_opaque
[MAX_MUX
];
254 CharDriverState
*drv
;
259 /* Intermediate input buffer allows to catch escape sequences even if the
260 currently active device is not accepting any input - but only until it
262 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
267 int64_t timestamps_start
;
271 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
273 MuxDriver
*d
= chr
->opaque
;
275 if (!d
->timestamps
) {
276 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
281 for (i
= 0; i
< len
; i
++) {
287 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
288 if (d
->timestamps_start
== -1)
289 d
->timestamps_start
= ti
;
290 ti
-= d
->timestamps_start
;
292 snprintf(buf1
, sizeof(buf1
),
293 "[%02d:%02d:%02d.%03d] ",
298 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
301 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
302 if (buf
[i
] == '\n') {
310 static const char * const mux_help
[] = {
311 "% h print this help\n\r",
312 "% x exit emulator\n\r",
313 "% s save disk data back to file (if -snapshot)\n\r",
314 "% t toggle console timestamps\n\r"
315 "% b send break (magic sysrq)\n\r",
316 "% c switch between console and monitor\n\r",
321 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
322 static void mux_print_help(CharDriverState
*chr
)
325 char ebuf
[15] = "Escape-Char";
326 char cbuf
[50] = "\n\r";
328 if (term_escape_char
> 0 && term_escape_char
< 26) {
329 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
330 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
332 snprintf(cbuf
, sizeof(cbuf
),
333 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
336 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
337 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
338 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
339 if (mux_help
[i
][j
] == '%')
340 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
342 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
347 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
349 if (d
->chr_event
[mux_nr
])
350 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
353 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
355 if (d
->term_got_escape
) {
356 d
->term_got_escape
= 0;
357 if (ch
== term_escape_char
)
366 const char *term
= "QEMU: Terminated\n\r";
367 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
375 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
378 /* Switch to the next registered device */
379 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
381 if (d
->focus
>= d
->mux_cnt
)
383 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
386 d
->timestamps
= !d
->timestamps
;
387 d
->timestamps_start
= -1;
391 } else if (ch
== term_escape_char
) {
392 d
->term_got_escape
= 1;
400 static void mux_chr_accept_input(CharDriverState
*chr
)
402 MuxDriver
*d
= chr
->opaque
;
405 while (d
->prod
[m
] != d
->cons
[m
] &&
406 d
->chr_can_read
[m
] &&
407 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
408 d
->chr_read
[m
](d
->ext_opaque
[m
],
409 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
413 static int mux_chr_can_read(void *opaque
)
415 CharDriverState
*chr
= opaque
;
416 MuxDriver
*d
= chr
->opaque
;
419 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
421 if (d
->chr_can_read
[m
])
422 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
426 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
428 CharDriverState
*chr
= opaque
;
429 MuxDriver
*d
= chr
->opaque
;
433 mux_chr_accept_input (opaque
);
435 for(i
= 0; i
< size
; i
++)
436 if (mux_proc_byte(chr
, d
, buf
[i
])) {
437 if (d
->prod
[m
] == d
->cons
[m
] &&
438 d
->chr_can_read
[m
] &&
439 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
440 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
442 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
446 static void mux_chr_event(void *opaque
, int event
)
448 CharDriverState
*chr
= opaque
;
449 MuxDriver
*d
= chr
->opaque
;
452 /* Send the event to all registered listeners */
453 for (i
= 0; i
< d
->mux_cnt
; i
++)
454 mux_chr_send_event(d
, i
, event
);
457 static void mux_chr_update_read_handler(CharDriverState
*chr
)
459 MuxDriver
*d
= chr
->opaque
;
461 if (d
->mux_cnt
>= MAX_MUX
) {
462 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
465 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
466 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
467 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
468 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
469 /* Fix up the real driver with mux routines */
470 if (d
->mux_cnt
== 0) {
471 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
474 if (d
->focus
!= -1) {
475 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
477 d
->focus
= d
->mux_cnt
;
479 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
482 static bool muxes_realized
;
485 * Called after processing of default and command-line-specified
486 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
487 * to a mux chardev. This is done here to ensure that
488 * output/prompts/banners are only displayed for the FE that has
489 * focus when initial command-line processing/machine init is
492 * After this point, any new FE attached to any new or existing
493 * mux will receive CHR_EVENT_OPENED notifications for the BE
496 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
498 CharDriverState
*chr
;
500 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
502 MuxDriver
*d
= chr
->opaque
;
505 /* send OPENED to all already-attached FEs */
506 for (i
= 0; i
< d
->mux_cnt
; i
++) {
507 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
509 /* mark mux as OPENED so any new FEs will immediately receive
512 qemu_chr_be_generic_open(chr
);
515 muxes_realized
= true;
518 static Notifier muxes_realize_notify
= {
519 .notify
= muxes_realize_done
,
522 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
524 CharDriverState
*chr
;
527 chr
= g_malloc0(sizeof(CharDriverState
));
528 d
= g_malloc0(sizeof(MuxDriver
));
533 chr
->chr_write
= mux_chr_write
;
534 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
535 chr
->chr_accept_input
= mux_chr_accept_input
;
536 /* Frontend guest-open / -close notification is not support with muxes */
537 chr
->chr_set_fe_open
= NULL
;
538 /* only default to opened state if we've realized the initial
541 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
549 int send_all(int fd
, const void *buf
, int len1
)
555 ret
= send(fd
, buf
, len
, 0);
557 errno
= WSAGetLastError();
558 if (errno
!= WSAEWOULDBLOCK
) {
561 } else if (ret
== 0) {
573 int send_all(int fd
, const void *_buf
, int len1
)
576 const uint8_t *buf
= _buf
;
580 ret
= write(fd
, buf
, len
);
582 if (errno
!= EINTR
&& errno
!= EAGAIN
)
584 } else if (ret
== 0) {
594 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
600 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
602 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
619 typedef struct IOWatchPoll
626 IOCanReadHandler
*fd_can_read
;
631 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
633 return container_of(source
, IOWatchPoll
, parent
);
636 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
638 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
639 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
640 bool was_active
= iwp
->src
!= NULL
;
641 if (was_active
== now_active
) {
646 iwp
->src
= g_io_create_watch(iwp
->channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
647 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
648 g_source_attach(iwp
->src
, NULL
);
650 g_source_destroy(iwp
->src
);
651 g_source_unref(iwp
->src
);
657 static gboolean
io_watch_poll_check(GSource
*source
)
662 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
668 static void io_watch_poll_finalize(GSource
*source
)
670 /* Due to a glib bug, removing the last reference to a source
671 * inside a finalize callback causes recursive locking (and a
672 * deadlock). This is not a problem inside other callbacks,
673 * including dispatch callbacks, so we call io_remove_watch_poll
674 * to remove this source. At this point, iwp->src must
675 * be NULL, or we would leak it.
677 * This would be solved much more elegantly by child sources,
678 * but we support older glib versions that do not have them.
680 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
681 assert(iwp
->src
== NULL
);
684 static GSourceFuncs io_watch_poll_funcs
= {
685 .prepare
= io_watch_poll_prepare
,
686 .check
= io_watch_poll_check
,
687 .dispatch
= io_watch_poll_dispatch
,
688 .finalize
= io_watch_poll_finalize
,
691 /* Can only be used for read */
692 static guint
io_add_watch_poll(GIOChannel
*channel
,
693 IOCanReadHandler
*fd_can_read
,
700 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
701 iwp
->fd_can_read
= fd_can_read
;
702 iwp
->opaque
= user_data
;
703 iwp
->channel
= channel
;
704 iwp
->fd_read
= (GSourceFunc
) fd_read
;
707 tag
= g_source_attach(&iwp
->parent
, NULL
);
708 g_source_unref(&iwp
->parent
);
712 static void io_remove_watch_poll(guint tag
)
717 g_return_if_fail (tag
> 0);
719 source
= g_main_context_find_source_by_id(NULL
, tag
);
720 g_return_if_fail (source
!= NULL
);
722 iwp
= io_watch_poll_from_source(source
);
724 g_source_destroy(iwp
->src
);
725 g_source_unref(iwp
->src
);
728 g_source_destroy(&iwp
->parent
);
731 static void remove_fd_in_watch(CharDriverState
*chr
)
733 if (chr
->fd_in_tag
) {
734 io_remove_watch_poll(chr
->fd_in_tag
);
740 static GIOChannel
*io_channel_from_fd(int fd
)
748 chan
= g_io_channel_unix_new(fd
);
750 g_io_channel_set_encoding(chan
, NULL
, NULL
);
751 g_io_channel_set_buffered(chan
, FALSE
);
757 static GIOChannel
*io_channel_from_socket(int fd
)
766 chan
= g_io_channel_win32_new_socket(fd
);
768 chan
= g_io_channel_unix_new(fd
);
771 g_io_channel_set_encoding(chan
, NULL
, NULL
);
772 g_io_channel_set_buffered(chan
, FALSE
);
777 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
780 GIOStatus status
= G_IO_STATUS_NORMAL
;
782 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
783 gsize bytes_written
= 0;
785 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
786 &bytes_written
, NULL
);
787 offset
+= bytes_written
;
794 case G_IO_STATUS_NORMAL
:
797 case G_IO_STATUS_AGAIN
:
809 typedef struct FDCharDriver
{
810 CharDriverState
*chr
;
811 GIOChannel
*fd_in
, *fd_out
;
813 QTAILQ_ENTRY(FDCharDriver
) node
;
816 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
818 FDCharDriver
*s
= chr
->opaque
;
820 return io_channel_send(s
->fd_out
, buf
, len
);
823 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
825 CharDriverState
*chr
= opaque
;
826 FDCharDriver
*s
= chr
->opaque
;
828 uint8_t buf
[READ_BUF_LEN
];
833 if (len
> s
->max_size
) {
840 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
841 len
, &bytes_read
, NULL
);
842 if (status
== G_IO_STATUS_EOF
) {
843 remove_fd_in_watch(chr
);
844 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
847 if (status
== G_IO_STATUS_NORMAL
) {
848 qemu_chr_be_write(chr
, buf
, bytes_read
);
854 static int fd_chr_read_poll(void *opaque
)
856 CharDriverState
*chr
= opaque
;
857 FDCharDriver
*s
= chr
->opaque
;
859 s
->max_size
= qemu_chr_be_can_write(chr
);
863 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
865 FDCharDriver
*s
= chr
->opaque
;
866 return g_io_create_watch(s
->fd_out
, cond
);
869 static void fd_chr_update_read_handler(CharDriverState
*chr
)
871 FDCharDriver
*s
= chr
->opaque
;
873 remove_fd_in_watch(chr
);
875 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
880 static void fd_chr_close(struct CharDriverState
*chr
)
882 FDCharDriver
*s
= chr
->opaque
;
884 remove_fd_in_watch(chr
);
886 g_io_channel_unref(s
->fd_in
);
889 g_io_channel_unref(s
->fd_out
);
893 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
896 /* open a character device to a unix fd */
897 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
899 CharDriverState
*chr
;
902 chr
= g_malloc0(sizeof(CharDriverState
));
903 s
= g_malloc0(sizeof(FDCharDriver
));
904 s
->fd_in
= io_channel_from_fd(fd_in
);
905 s
->fd_out
= io_channel_from_fd(fd_out
);
906 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
909 chr
->chr_add_watch
= fd_chr_add_watch
;
910 chr
->chr_write
= fd_chr_write
;
911 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
912 chr
->chr_close
= fd_chr_close
;
917 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
920 char filename_in
[256], filename_out
[256];
921 const char *filename
= opts
->device
;
923 if (filename
== NULL
) {
924 fprintf(stderr
, "chardev: pipe: no filename given\n");
928 snprintf(filename_in
, 256, "%s.in", filename
);
929 snprintf(filename_out
, 256, "%s.out", filename
);
930 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
931 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
932 if (fd_in
< 0 || fd_out
< 0) {
937 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
942 return qemu_chr_open_fd(fd_in
, fd_out
);
945 /* init terminal so that we can grab keys */
946 static struct termios oldtty
;
947 static int old_fd0_flags
;
948 static bool stdio_allow_signal
;
950 static void term_exit(void)
952 tcsetattr (0, TCSANOW
, &oldtty
);
953 fcntl(0, F_SETFL
, old_fd0_flags
);
956 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
962 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
963 |INLCR
|IGNCR
|ICRNL
|IXON
);
964 tty
.c_oflag
|= OPOST
;
965 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
966 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
971 if (!stdio_allow_signal
)
972 tty
.c_lflag
&= ~ISIG
;
974 tcsetattr (0, TCSANOW
, &tty
);
977 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
983 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
985 CharDriverState
*chr
;
987 if (is_daemonized()) {
988 error_report("cannot use stdio with -daemonize");
991 old_fd0_flags
= fcntl(0, F_GETFL
);
992 tcgetattr (0, &oldtty
);
993 fcntl(0, F_SETFL
, O_NONBLOCK
);
996 chr
= qemu_chr_open_fd(0, 1);
997 chr
->chr_close
= qemu_chr_close_stdio
;
998 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
999 if (opts
->has_signal
) {
1000 stdio_allow_signal
= opts
->signal
;
1002 qemu_chr_fe_set_echo(chr
, false);
1007 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1008 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1009 || defined(__GLIBC__)
1011 #define HAVE_CHARDEV_TTY 1
1020 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1021 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1023 static gboolean
pty_chr_timer(gpointer opaque
)
1025 struct CharDriverState
*chr
= opaque
;
1026 PtyCharDriver
*s
= chr
->opaque
;
1029 if (!s
->connected
) {
1031 pty_chr_update_read_handler(chr
);
1036 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1038 PtyCharDriver
*s
= chr
->opaque
;
1041 g_source_remove(s
->timer_tag
);
1046 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1048 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1052 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1054 PtyCharDriver
*s
= chr
->opaque
;
1056 if (!s
->connected
) {
1057 /* guest sends data, check for (re-)connect */
1058 pty_chr_update_read_handler(chr
);
1061 return io_channel_send(s
->fd
, buf
, len
);
1064 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1066 PtyCharDriver
*s
= chr
->opaque
;
1067 return g_io_create_watch(s
->fd
, cond
);
1070 static int pty_chr_read_poll(void *opaque
)
1072 CharDriverState
*chr
= opaque
;
1073 PtyCharDriver
*s
= chr
->opaque
;
1075 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1076 return s
->read_bytes
;
1079 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1081 CharDriverState
*chr
= opaque
;
1082 PtyCharDriver
*s
= chr
->opaque
;
1084 uint8_t buf
[READ_BUF_LEN
];
1088 if (len
> s
->read_bytes
)
1089 len
= s
->read_bytes
;
1093 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1094 if (status
!= G_IO_STATUS_NORMAL
) {
1095 pty_chr_state(chr
, 0);
1098 pty_chr_state(chr
, 1);
1099 qemu_chr_be_write(chr
, buf
, size
);
1104 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1106 PtyCharDriver
*s
= chr
->opaque
;
1109 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1110 pfd
.events
= G_IO_OUT
;
1113 if (pfd
.revents
& G_IO_HUP
) {
1114 pty_chr_state(chr
, 0);
1116 pty_chr_state(chr
, 1);
1120 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1122 PtyCharDriver
*s
= chr
->opaque
;
1125 remove_fd_in_watch(chr
);
1127 /* (re-)connect poll interval for idle guests: once per second.
1128 * We check more frequently in case the guests sends data to
1129 * the virtual device linked to our pty. */
1130 pty_chr_rearm_timer(chr
, 1000);
1133 g_source_remove(s
->timer_tag
);
1136 if (!s
->connected
) {
1138 qemu_chr_be_generic_open(chr
);
1139 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1146 static void pty_chr_close(struct CharDriverState
*chr
)
1148 PtyCharDriver
*s
= chr
->opaque
;
1151 remove_fd_in_watch(chr
);
1152 fd
= g_io_channel_unix_get_fd(s
->fd
);
1153 g_io_channel_unref(s
->fd
);
1156 g_source_remove(s
->timer_tag
);
1160 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1163 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1166 CharDriverState
*chr
;
1168 int master_fd
, slave_fd
;
1169 char pty_name
[PATH_MAX
];
1171 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1172 if (master_fd
< 0) {
1178 chr
= g_malloc0(sizeof(CharDriverState
));
1180 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1181 ret
->pty
= g_strdup(pty_name
);
1182 ret
->has_pty
= true;
1184 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1187 s
= g_malloc0(sizeof(PtyCharDriver
));
1189 chr
->chr_write
= pty_chr_write
;
1190 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1191 chr
->chr_close
= pty_chr_close
;
1192 chr
->chr_add_watch
= pty_chr_add_watch
;
1193 chr
->explicit_be_open
= true;
1195 s
->fd
= io_channel_from_fd(master_fd
);
1201 static void tty_serial_init(int fd
, int speed
,
1202 int parity
, int data_bits
, int stop_bits
)
1208 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1209 speed
, parity
, data_bits
, stop_bits
);
1211 tcgetattr (fd
, &tty
);
1213 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1214 speed
= speed
* 10 / 11;
1231 /* Non-Posix values follow. They may be unsupported on some systems. */
1233 check_speed(115200);
1235 check_speed(230400);
1238 check_speed(460800);
1241 check_speed(500000);
1244 check_speed(576000);
1247 check_speed(921600);
1250 check_speed(1000000);
1253 check_speed(1152000);
1256 check_speed(1500000);
1259 check_speed(2000000);
1262 check_speed(2500000);
1265 check_speed(3000000);
1268 check_speed(3500000);
1271 check_speed(4000000);
1276 cfsetispeed(&tty
, spd
);
1277 cfsetospeed(&tty
, spd
);
1279 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1280 |INLCR
|IGNCR
|ICRNL
|IXON
);
1281 tty
.c_oflag
|= OPOST
;
1282 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1283 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1304 tty
.c_cflag
|= PARENB
;
1307 tty
.c_cflag
|= PARENB
| PARODD
;
1311 tty
.c_cflag
|= CSTOPB
;
1313 tcsetattr (fd
, TCSANOW
, &tty
);
1316 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1318 FDCharDriver
*s
= chr
->opaque
;
1321 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1323 QEMUSerialSetParams
*ssp
= arg
;
1324 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1325 ssp
->speed
, ssp
->parity
,
1326 ssp
->data_bits
, ssp
->stop_bits
);
1329 case CHR_IOCTL_SERIAL_SET_BREAK
:
1331 int enable
= *(int *)arg
;
1333 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1337 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1340 int *targ
= (int *)arg
;
1341 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1343 if (sarg
& TIOCM_CTS
)
1344 *targ
|= CHR_TIOCM_CTS
;
1345 if (sarg
& TIOCM_CAR
)
1346 *targ
|= CHR_TIOCM_CAR
;
1347 if (sarg
& TIOCM_DSR
)
1348 *targ
|= CHR_TIOCM_DSR
;
1349 if (sarg
& TIOCM_RI
)
1350 *targ
|= CHR_TIOCM_RI
;
1351 if (sarg
& TIOCM_DTR
)
1352 *targ
|= CHR_TIOCM_DTR
;
1353 if (sarg
& TIOCM_RTS
)
1354 *targ
|= CHR_TIOCM_RTS
;
1357 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1359 int sarg
= *(int *)arg
;
1361 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1362 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1363 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1364 if (sarg
& CHR_TIOCM_CTS
)
1366 if (sarg
& CHR_TIOCM_CAR
)
1368 if (sarg
& CHR_TIOCM_DSR
)
1370 if (sarg
& CHR_TIOCM_RI
)
1372 if (sarg
& CHR_TIOCM_DTR
)
1374 if (sarg
& CHR_TIOCM_RTS
)
1376 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1385 static void qemu_chr_close_tty(CharDriverState
*chr
)
1387 FDCharDriver
*s
= chr
->opaque
;
1391 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1401 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1403 CharDriverState
*chr
;
1405 tty_serial_init(fd
, 115200, 'N', 8, 1);
1406 chr
= qemu_chr_open_fd(fd
, fd
);
1407 chr
->chr_ioctl
= tty_serial_ioctl
;
1408 chr
->chr_close
= qemu_chr_close_tty
;
1411 #endif /* __linux__ || __sun__ */
1413 #if defined(__linux__)
1415 #define HAVE_CHARDEV_PARPORT 1
1420 } ParallelCharDriver
;
1422 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1424 if (s
->mode
!= mode
) {
1426 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1433 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1435 ParallelCharDriver
*drv
= chr
->opaque
;
1440 case CHR_IOCTL_PP_READ_DATA
:
1441 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1443 *(uint8_t *)arg
= b
;
1445 case CHR_IOCTL_PP_WRITE_DATA
:
1446 b
= *(uint8_t *)arg
;
1447 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1450 case CHR_IOCTL_PP_READ_CONTROL
:
1451 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1453 /* Linux gives only the lowest bits, and no way to know data
1454 direction! For better compatibility set the fixed upper
1456 *(uint8_t *)arg
= b
| 0xc0;
1458 case CHR_IOCTL_PP_WRITE_CONTROL
:
1459 b
= *(uint8_t *)arg
;
1460 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1463 case CHR_IOCTL_PP_READ_STATUS
:
1464 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1466 *(uint8_t *)arg
= b
;
1468 case CHR_IOCTL_PP_DATA_DIR
:
1469 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1472 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1473 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1474 struct ParallelIOArg
*parg
= arg
;
1475 int n
= read(fd
, parg
->buffer
, parg
->count
);
1476 if (n
!= parg
->count
) {
1481 case CHR_IOCTL_PP_EPP_READ
:
1482 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1483 struct ParallelIOArg
*parg
= arg
;
1484 int n
= read(fd
, parg
->buffer
, parg
->count
);
1485 if (n
!= parg
->count
) {
1490 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1491 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1492 struct ParallelIOArg
*parg
= arg
;
1493 int n
= write(fd
, parg
->buffer
, parg
->count
);
1494 if (n
!= parg
->count
) {
1499 case CHR_IOCTL_PP_EPP_WRITE
:
1500 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1501 struct ParallelIOArg
*parg
= arg
;
1502 int n
= write(fd
, parg
->buffer
, parg
->count
);
1503 if (n
!= parg
->count
) {
1514 static void pp_close(CharDriverState
*chr
)
1516 ParallelCharDriver
*drv
= chr
->opaque
;
1519 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1520 ioctl(fd
, PPRELEASE
);
1523 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1526 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1528 CharDriverState
*chr
;
1529 ParallelCharDriver
*drv
;
1531 if (ioctl(fd
, PPCLAIM
) < 0) {
1536 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1538 drv
->mode
= IEEE1284_MODE_COMPAT
;
1540 chr
= g_malloc0(sizeof(CharDriverState
));
1541 chr
->chr_write
= null_chr_write
;
1542 chr
->chr_ioctl
= pp_ioctl
;
1543 chr
->chr_close
= pp_close
;
1548 #endif /* __linux__ */
1550 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1552 #define HAVE_CHARDEV_PARPORT 1
1554 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1556 int fd
= (int)(intptr_t)chr
->opaque
;
1560 case CHR_IOCTL_PP_READ_DATA
:
1561 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1563 *(uint8_t *)arg
= b
;
1565 case CHR_IOCTL_PP_WRITE_DATA
:
1566 b
= *(uint8_t *)arg
;
1567 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1570 case CHR_IOCTL_PP_READ_CONTROL
:
1571 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1573 *(uint8_t *)arg
= b
;
1575 case CHR_IOCTL_PP_WRITE_CONTROL
:
1576 b
= *(uint8_t *)arg
;
1577 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1580 case CHR_IOCTL_PP_READ_STATUS
:
1581 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1583 *(uint8_t *)arg
= b
;
1591 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1593 CharDriverState
*chr
;
1595 chr
= g_malloc0(sizeof(CharDriverState
));
1596 chr
->opaque
= (void *)(intptr_t)fd
;
1597 chr
->chr_write
= null_chr_write
;
1598 chr
->chr_ioctl
= pp_ioctl
;
1599 chr
->explicit_be_open
= true;
1608 HANDLE hcom
, hrecv
, hsend
;
1609 OVERLAPPED orecv
, osend
;
1616 HANDLE hInputReadyEvent
;
1617 HANDLE hInputDoneEvent
;
1618 HANDLE hInputThread
;
1619 uint8_t win_stdio_buf
;
1620 } WinStdioCharState
;
1622 #define NSENDBUF 2048
1623 #define NRECVBUF 2048
1624 #define MAXCONNECT 1
1625 #define NTIMEOUT 5000
1627 static int win_chr_poll(void *opaque
);
1628 static int win_chr_pipe_poll(void *opaque
);
1630 static void win_chr_close(CharDriverState
*chr
)
1632 WinCharState
*s
= chr
->opaque
;
1635 CloseHandle(s
->hsend
);
1639 CloseHandle(s
->hrecv
);
1643 CloseHandle(s
->hcom
);
1647 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1649 qemu_del_polling_cb(win_chr_poll
, chr
);
1651 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1654 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1656 WinCharState
*s
= chr
->opaque
;
1658 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1663 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1665 fprintf(stderr
, "Failed CreateEvent\n");
1668 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1670 fprintf(stderr
, "Failed CreateEvent\n");
1674 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1675 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1676 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1677 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1682 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1683 fprintf(stderr
, "Failed SetupComm\n");
1687 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1688 size
= sizeof(COMMCONFIG
);
1689 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1690 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1691 CommConfigDialog(filename
, NULL
, &comcfg
);
1693 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1694 fprintf(stderr
, "Failed SetCommState\n");
1698 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1699 fprintf(stderr
, "Failed SetCommMask\n");
1703 cto
.ReadIntervalTimeout
= MAXDWORD
;
1704 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1705 fprintf(stderr
, "Failed SetCommTimeouts\n");
1709 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1710 fprintf(stderr
, "Failed ClearCommError\n");
1713 qemu_add_polling_cb(win_chr_poll
, chr
);
1721 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1723 WinCharState
*s
= chr
->opaque
;
1724 DWORD len
, ret
, size
, err
;
1727 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1728 s
->osend
.hEvent
= s
->hsend
;
1731 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1733 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1735 err
= GetLastError();
1736 if (err
== ERROR_IO_PENDING
) {
1737 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1755 static int win_chr_read_poll(CharDriverState
*chr
)
1757 WinCharState
*s
= chr
->opaque
;
1759 s
->max_size
= qemu_chr_be_can_write(chr
);
1763 static void win_chr_readfile(CharDriverState
*chr
)
1765 WinCharState
*s
= chr
->opaque
;
1767 uint8_t buf
[READ_BUF_LEN
];
1770 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1771 s
->orecv
.hEvent
= s
->hrecv
;
1772 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1774 err
= GetLastError();
1775 if (err
== ERROR_IO_PENDING
) {
1776 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1781 qemu_chr_be_write(chr
, buf
, size
);
1785 static void win_chr_read(CharDriverState
*chr
)
1787 WinCharState
*s
= chr
->opaque
;
1789 if (s
->len
> s
->max_size
)
1790 s
->len
= s
->max_size
;
1794 win_chr_readfile(chr
);
1797 static int win_chr_poll(void *opaque
)
1799 CharDriverState
*chr
= opaque
;
1800 WinCharState
*s
= chr
->opaque
;
1804 ClearCommError(s
->hcom
, &comerr
, &status
);
1805 if (status
.cbInQue
> 0) {
1806 s
->len
= status
.cbInQue
;
1807 win_chr_read_poll(chr
);
1814 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1816 CharDriverState
*chr
;
1819 chr
= g_malloc0(sizeof(CharDriverState
));
1820 s
= g_malloc0(sizeof(WinCharState
));
1822 chr
->chr_write
= win_chr_write
;
1823 chr
->chr_close
= win_chr_close
;
1825 if (win_chr_init(chr
, filename
) < 0) {
1833 static int win_chr_pipe_poll(void *opaque
)
1835 CharDriverState
*chr
= opaque
;
1836 WinCharState
*s
= chr
->opaque
;
1839 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1842 win_chr_read_poll(chr
);
1849 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1851 WinCharState
*s
= chr
->opaque
;
1859 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1861 fprintf(stderr
, "Failed CreateEvent\n");
1864 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1866 fprintf(stderr
, "Failed CreateEvent\n");
1870 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1871 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1872 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1874 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1875 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1876 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1881 ZeroMemory(&ov
, sizeof(ov
));
1882 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1883 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1885 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1889 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1891 fprintf(stderr
, "Failed GetOverlappedResult\n");
1893 CloseHandle(ov
.hEvent
);
1900 CloseHandle(ov
.hEvent
);
1903 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1912 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1914 const char *filename
= opts
->device
;
1915 CharDriverState
*chr
;
1918 chr
= g_malloc0(sizeof(CharDriverState
));
1919 s
= g_malloc0(sizeof(WinCharState
));
1921 chr
->chr_write
= win_chr_write
;
1922 chr
->chr_close
= win_chr_close
;
1924 if (win_chr_pipe_init(chr
, filename
) < 0) {
1932 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1934 CharDriverState
*chr
;
1937 chr
= g_malloc0(sizeof(CharDriverState
));
1938 s
= g_malloc0(sizeof(WinCharState
));
1941 chr
->chr_write
= win_chr_write
;
1945 static CharDriverState
*qemu_chr_open_win_con(void)
1947 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1950 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1952 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1959 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1969 static void win_stdio_wait_func(void *opaque
)
1971 CharDriverState
*chr
= opaque
;
1972 WinStdioCharState
*stdio
= chr
->opaque
;
1973 INPUT_RECORD buf
[4];
1978 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
1981 /* Avoid error storm */
1982 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1986 for (i
= 0; i
< dwSize
; i
++) {
1987 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
1989 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
1991 if (kev
->uChar
.AsciiChar
!= 0) {
1992 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
1993 if (qemu_chr_be_can_write(chr
)) {
1994 uint8_t c
= kev
->uChar
.AsciiChar
;
1995 qemu_chr_be_write(chr
, &c
, 1);
2003 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2005 CharDriverState
*chr
= param
;
2006 WinStdioCharState
*stdio
= chr
->opaque
;
2012 /* Wait for one byte */
2013 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2015 /* Exit in case of error, continue if nothing read */
2023 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2024 if (stdio
->win_stdio_buf
== '\r') {
2028 /* Signal the main thread and wait until the byte was eaten */
2029 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2032 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2038 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2042 static void win_stdio_thread_wait_func(void *opaque
)
2044 CharDriverState
*chr
= opaque
;
2045 WinStdioCharState
*stdio
= chr
->opaque
;
2047 if (qemu_chr_be_can_write(chr
)) {
2048 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2051 SetEvent(stdio
->hInputDoneEvent
);
2054 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2056 WinStdioCharState
*stdio
= chr
->opaque
;
2059 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2062 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2064 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2068 static void win_stdio_close(CharDriverState
*chr
)
2070 WinStdioCharState
*stdio
= chr
->opaque
;
2072 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2073 CloseHandle(stdio
->hInputReadyEvent
);
2075 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2076 CloseHandle(stdio
->hInputDoneEvent
);
2078 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2079 TerminateThread(stdio
->hInputThread
, 0);
2082 g_free(chr
->opaque
);
2086 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2088 CharDriverState
*chr
;
2089 WinStdioCharState
*stdio
;
2093 chr
= g_malloc0(sizeof(CharDriverState
));
2094 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2096 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2097 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2098 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2102 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2104 chr
->opaque
= stdio
;
2105 chr
->chr_write
= win_stdio_write
;
2106 chr
->chr_close
= win_stdio_close
;
2109 if (qemu_add_wait_object(stdio
->hStdIn
,
2110 win_stdio_wait_func
, chr
)) {
2111 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2116 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2117 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2118 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2121 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2122 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2123 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2124 fprintf(stderr
, "cannot create stdio thread or event\n");
2127 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2128 win_stdio_thread_wait_func
, chr
)) {
2129 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2133 dwMode
|= ENABLE_LINE_INPUT
;
2136 /* set the terminal in raw mode */
2137 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2138 dwMode
|= ENABLE_PROCESSED_INPUT
;
2141 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2143 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2144 qemu_chr_fe_set_echo(chr
, false);
2148 #endif /* !_WIN32 */
2151 /***********************************************************/
2152 /* UDP Net console */
2157 uint8_t buf
[READ_BUF_LEN
];
2163 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2165 NetCharDriver
*s
= chr
->opaque
;
2166 gsize bytes_written
;
2169 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2170 if (status
== G_IO_STATUS_EOF
) {
2172 } else if (status
!= G_IO_STATUS_NORMAL
) {
2176 return bytes_written
;
2179 static int udp_chr_read_poll(void *opaque
)
2181 CharDriverState
*chr
= opaque
;
2182 NetCharDriver
*s
= chr
->opaque
;
2184 s
->max_size
= qemu_chr_be_can_write(chr
);
2186 /* If there were any stray characters in the queue process them
2189 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2190 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2192 s
->max_size
= qemu_chr_be_can_write(chr
);
2197 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2199 CharDriverState
*chr
= opaque
;
2200 NetCharDriver
*s
= chr
->opaque
;
2201 gsize bytes_read
= 0;
2204 if (s
->max_size
== 0) {
2207 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2209 s
->bufcnt
= bytes_read
;
2210 s
->bufptr
= s
->bufcnt
;
2211 if (status
!= G_IO_STATUS_NORMAL
) {
2212 remove_fd_in_watch(chr
);
2217 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2218 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2220 s
->max_size
= qemu_chr_be_can_write(chr
);
2226 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2228 NetCharDriver
*s
= chr
->opaque
;
2230 remove_fd_in_watch(chr
);
2232 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2237 static void udp_chr_close(CharDriverState
*chr
)
2239 NetCharDriver
*s
= chr
->opaque
;
2241 remove_fd_in_watch(chr
);
2243 g_io_channel_unref(s
->chan
);
2247 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2250 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2252 CharDriverState
*chr
= NULL
;
2253 NetCharDriver
*s
= NULL
;
2255 chr
= g_malloc0(sizeof(CharDriverState
));
2256 s
= g_malloc0(sizeof(NetCharDriver
));
2259 s
->chan
= io_channel_from_socket(s
->fd
);
2263 chr
->chr_write
= udp_chr_write
;
2264 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2265 chr
->chr_close
= udp_chr_close
;
2266 /* be isn't opened until we get a connection */
2267 chr
->explicit_be_open
= true;
2271 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2273 Error
*local_err
= NULL
;
2276 fd
= inet_dgram_opts(opts
, &local_err
);
2278 qerror_report_err(local_err
);
2279 error_free(local_err
);
2282 return qemu_chr_open_udp_fd(fd
);
2285 /***********************************************************/
2286 /* TCP Net console */
2290 GIOChannel
*chan
, *listen_chan
;
2301 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2303 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2305 TCPCharDriver
*s
= chr
->opaque
;
2307 return io_channel_send(s
->chan
, buf
, len
);
2309 /* XXX: indicate an error ? */
2314 static int tcp_chr_read_poll(void *opaque
)
2316 CharDriverState
*chr
= opaque
;
2317 TCPCharDriver
*s
= chr
->opaque
;
2320 s
->max_size
= qemu_chr_be_can_write(chr
);
2325 #define IAC_BREAK 243
2326 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2328 uint8_t *buf
, int *size
)
2330 /* Handle any telnet client's basic IAC options to satisfy char by
2331 * char mode with no echo. All IAC options will be removed from
2332 * the buf and the do_telnetopt variable will be used to track the
2333 * state of the width of the IAC information.
2335 * IAC commands come in sets of 3 bytes with the exception of the
2336 * "IAC BREAK" command and the double IAC.
2342 for (i
= 0; i
< *size
; i
++) {
2343 if (s
->do_telnetopt
> 1) {
2344 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2345 /* Double IAC means send an IAC */
2349 s
->do_telnetopt
= 1;
2351 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2352 /* Handle IAC break commands by sending a serial break */
2353 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2358 if (s
->do_telnetopt
>= 4) {
2359 s
->do_telnetopt
= 1;
2362 if ((unsigned char)buf
[i
] == IAC
) {
2363 s
->do_telnetopt
= 2;
2374 static int tcp_get_msgfd(CharDriverState
*chr
)
2376 TCPCharDriver
*s
= chr
->opaque
;
2383 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2385 TCPCharDriver
*s
= chr
->opaque
;
2386 struct cmsghdr
*cmsg
;
2388 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2391 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2392 cmsg
->cmsg_level
!= SOL_SOCKET
||
2393 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2396 fd
= *((int *)CMSG_DATA(cmsg
));
2400 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2403 #ifndef MSG_CMSG_CLOEXEC
2404 qemu_set_cloexec(fd
);
2412 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2414 TCPCharDriver
*s
= chr
->opaque
;
2415 struct msghdr msg
= { NULL
, };
2416 struct iovec iov
[1];
2418 struct cmsghdr cmsg
;
2419 char control
[CMSG_SPACE(sizeof(int))];
2424 iov
[0].iov_base
= buf
;
2425 iov
[0].iov_len
= len
;
2429 msg
.msg_control
= &msg_control
;
2430 msg
.msg_controllen
= sizeof(msg_control
);
2432 #ifdef MSG_CMSG_CLOEXEC
2433 flags
|= MSG_CMSG_CLOEXEC
;
2435 ret
= recvmsg(s
->fd
, &msg
, flags
);
2436 if (ret
> 0 && s
->is_unix
) {
2437 unix_process_msgfd(chr
, &msg
);
2443 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2445 TCPCharDriver
*s
= chr
->opaque
;
2446 return qemu_recv(s
->fd
, buf
, len
, 0);
2450 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2452 TCPCharDriver
*s
= chr
->opaque
;
2453 return g_io_create_watch(s
->chan
, cond
);
2456 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2458 CharDriverState
*chr
= opaque
;
2459 TCPCharDriver
*s
= chr
->opaque
;
2460 uint8_t buf
[READ_BUF_LEN
];
2463 if (!s
->connected
|| s
->max_size
<= 0) {
2467 if (len
> s
->max_size
)
2469 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2471 /* connection closed */
2473 if (s
->listen_chan
) {
2474 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2476 remove_fd_in_watch(chr
);
2477 g_io_channel_unref(s
->chan
);
2481 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2482 } else if (size
> 0) {
2483 if (s
->do_telnetopt
)
2484 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2486 qemu_chr_be_write(chr
, buf
, size
);
2493 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2495 return qemu_chr_open_fd(eventfd
, eventfd
);
2499 static void tcp_chr_connect(void *opaque
)
2501 CharDriverState
*chr
= opaque
;
2502 TCPCharDriver
*s
= chr
->opaque
;
2506 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2509 qemu_chr_be_generic_open(chr
);
2512 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2513 static void tcp_chr_telnet_init(int fd
)
2516 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2517 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2518 send(fd
, (char *)buf
, 3, 0);
2519 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2520 send(fd
, (char *)buf
, 3, 0);
2521 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2522 send(fd
, (char *)buf
, 3, 0);
2523 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2524 send(fd
, (char *)buf
, 3, 0);
2527 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2529 TCPCharDriver
*s
= chr
->opaque
;
2533 qemu_set_nonblock(fd
);
2535 socket_set_nodelay(fd
);
2537 s
->chan
= io_channel_from_socket(fd
);
2538 if (s
->listen_tag
) {
2539 g_source_remove(s
->listen_tag
);
2542 tcp_chr_connect(chr
);
2547 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2549 CharDriverState
*chr
= opaque
;
2550 TCPCharDriver
*s
= chr
->opaque
;
2551 struct sockaddr_in saddr
;
2553 struct sockaddr_un uaddr
;
2555 struct sockaddr
*addr
;
2562 len
= sizeof(uaddr
);
2563 addr
= (struct sockaddr
*)&uaddr
;
2567 len
= sizeof(saddr
);
2568 addr
= (struct sockaddr
*)&saddr
;
2570 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2571 if (fd
< 0 && errno
!= EINTR
) {
2574 } else if (fd
>= 0) {
2575 if (s
->do_telnetopt
)
2576 tcp_chr_telnet_init(fd
);
2580 if (tcp_chr_add_client(chr
, fd
) < 0)
2586 static void tcp_chr_close(CharDriverState
*chr
)
2588 TCPCharDriver
*s
= chr
->opaque
;
2590 remove_fd_in_watch(chr
);
2592 g_io_channel_unref(s
->chan
);
2596 if (s
->listen_fd
>= 0) {
2597 if (s
->listen_tag
) {
2598 g_source_remove(s
->listen_tag
);
2601 if (s
->listen_chan
) {
2602 g_io_channel_unref(s
->listen_chan
);
2604 closesocket(s
->listen_fd
);
2607 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2610 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2611 bool is_listen
, bool is_telnet
,
2612 bool is_waitconnect
,
2615 CharDriverState
*chr
= NULL
;
2616 TCPCharDriver
*s
= NULL
;
2617 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2618 const char *left
= "", *right
= "";
2619 struct sockaddr_storage ss
;
2620 socklen_t ss_len
= sizeof(ss
);
2622 memset(&ss
, 0, ss_len
);
2623 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2624 error_setg_errno(errp
, errno
, "getsockname");
2628 chr
= g_malloc0(sizeof(CharDriverState
));
2629 s
= g_malloc0(sizeof(TCPCharDriver
));
2636 chr
->filename
= g_malloc(256);
2637 switch (ss
.ss_family
) {
2641 snprintf(chr
->filename
, 256, "unix:%s%s",
2642 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2643 is_listen
? ",server" : "");
2651 s
->do_nodelay
= do_nodelay
;
2652 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2653 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2654 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2655 is_telnet
? "telnet" : "tcp",
2656 left
, host
, right
, serv
,
2657 is_listen
? ",server" : "");
2662 chr
->chr_write
= tcp_chr_write
;
2663 chr
->chr_close
= tcp_chr_close
;
2664 chr
->get_msgfd
= tcp_get_msgfd
;
2665 chr
->chr_add_client
= tcp_chr_add_client
;
2666 chr
->chr_add_watch
= tcp_chr_add_watch
;
2667 /* be isn't opened until we get a connection */
2668 chr
->explicit_be_open
= true;
2672 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2673 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2675 s
->do_telnetopt
= 1;
2680 socket_set_nodelay(fd
);
2681 s
->chan
= io_channel_from_socket(s
->fd
);
2682 tcp_chr_connect(chr
);
2685 if (is_listen
&& is_waitconnect
) {
2686 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2688 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2689 qemu_set_nonblock(s
->listen_fd
);
2694 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2696 CharDriverState
*chr
= NULL
;
2697 Error
*local_err
= NULL
;
2700 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2701 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2702 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2703 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2704 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2708 fd
= unix_listen_opts(opts
, &local_err
);
2710 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2714 fd
= inet_listen_opts(opts
, 0, &local_err
);
2716 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2723 if (!is_waitconnect
)
2724 qemu_set_nonblock(fd
);
2726 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2727 is_waitconnect
, &local_err
);
2736 qerror_report_err(local_err
);
2737 error_free(local_err
);
2743 g_free(chr
->opaque
);
2749 /*********************************************************/
2750 /* Ring buffer chardev */
2757 } RingBufCharDriver
;
2759 static size_t ringbuf_count(const CharDriverState
*chr
)
2761 const RingBufCharDriver
*d
= chr
->opaque
;
2763 return d
->prod
- d
->cons
;
2766 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2768 RingBufCharDriver
*d
= chr
->opaque
;
2771 if (!buf
|| (len
< 0)) {
2775 for (i
= 0; i
< len
; i
++ ) {
2776 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2777 if (d
->prod
- d
->cons
> d
->size
) {
2778 d
->cons
= d
->prod
- d
->size
;
2785 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2787 RingBufCharDriver
*d
= chr
->opaque
;
2790 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2791 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2797 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2799 RingBufCharDriver
*d
= chr
->opaque
;
2806 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2809 CharDriverState
*chr
;
2810 RingBufCharDriver
*d
;
2812 chr
= g_malloc0(sizeof(CharDriverState
));
2813 d
= g_malloc(sizeof(*d
));
2815 d
->size
= opts
->has_size
? opts
->size
: 65536;
2817 /* The size must be power of 2 */
2818 if (d
->size
& (d
->size
- 1)) {
2819 error_setg(errp
, "size of ringbuf chardev must be power of two");
2825 d
->cbuf
= g_malloc0(d
->size
);
2828 chr
->chr_write
= ringbuf_chr_write
;
2829 chr
->chr_close
= ringbuf_chr_close
;
2839 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2841 return chr
->chr_write
== ringbuf_chr_write
;
2844 void qmp_ringbuf_write(const char *device
, const char *data
,
2845 bool has_format
, enum DataFormat format
,
2848 CharDriverState
*chr
;
2849 const uint8_t *write_data
;
2853 chr
= qemu_chr_find(device
);
2855 error_setg(errp
, "Device '%s' not found", device
);
2859 if (!chr_is_ringbuf(chr
)) {
2860 error_setg(errp
,"%s is not a ringbuf device", device
);
2864 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2865 write_data
= g_base64_decode(data
, &write_count
);
2867 write_data
= (uint8_t *)data
;
2868 write_count
= strlen(data
);
2871 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2873 if (write_data
!= (uint8_t *)data
) {
2874 g_free((void *)write_data
);
2878 error_setg(errp
, "Failed to write to device %s", device
);
2883 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2884 bool has_format
, enum DataFormat format
,
2887 CharDriverState
*chr
;
2892 chr
= qemu_chr_find(device
);
2894 error_setg(errp
, "Device '%s' not found", device
);
2898 if (!chr_is_ringbuf(chr
)) {
2899 error_setg(errp
,"%s is not a ringbuf device", device
);
2904 error_setg(errp
, "size must be greater than zero");
2908 count
= ringbuf_count(chr
);
2909 size
= size
> count
? count
: size
;
2910 read_data
= g_malloc(size
+ 1);
2912 ringbuf_chr_read(chr
, read_data
, size
);
2914 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2915 data
= g_base64_encode(read_data
, size
);
2919 * FIXME should read only complete, valid UTF-8 characters up
2920 * to @size bytes. Invalid sequences should be replaced by a
2921 * suitable replacement character. Except when (and only
2922 * when) ring buffer lost characters since last read, initial
2923 * continuation characters should be dropped.
2925 read_data
[size
] = 0;
2926 data
= (char *)read_data
;
2932 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2934 char host
[65], port
[33], width
[8], height
[8];
2938 Error
*local_err
= NULL
;
2940 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
2942 qerror_report_err(local_err
);
2943 error_free(local_err
);
2947 if (strstart(filename
, "mon:", &p
)) {
2949 qemu_opt_set(opts
, "mux", "on");
2950 if (strcmp(filename
, "stdio") == 0) {
2951 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
2952 * but pass it to the guest. Handle this only for compat syntax,
2953 * for -chardev syntax we have special option for this.
2954 * This is what -nographic did, redirecting+muxing serial+monitor
2955 * to stdio causing Ctrl+C to be passed to guest. */
2956 qemu_opt_set(opts
, "signal", "off");
2960 if (strcmp(filename
, "null") == 0 ||
2961 strcmp(filename
, "pty") == 0 ||
2962 strcmp(filename
, "msmouse") == 0 ||
2963 strcmp(filename
, "braille") == 0 ||
2964 strcmp(filename
, "stdio") == 0) {
2965 qemu_opt_set(opts
, "backend", filename
);
2968 if (strstart(filename
, "vc", &p
)) {
2969 qemu_opt_set(opts
, "backend", "vc");
2971 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
2973 qemu_opt_set(opts
, "width", width
);
2974 qemu_opt_set(opts
, "height", height
);
2975 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
2977 qemu_opt_set(opts
, "cols", width
);
2978 qemu_opt_set(opts
, "rows", height
);
2985 if (strcmp(filename
, "con:") == 0) {
2986 qemu_opt_set(opts
, "backend", "console");
2989 if (strstart(filename
, "COM", NULL
)) {
2990 qemu_opt_set(opts
, "backend", "serial");
2991 qemu_opt_set(opts
, "path", filename
);
2994 if (strstart(filename
, "file:", &p
)) {
2995 qemu_opt_set(opts
, "backend", "file");
2996 qemu_opt_set(opts
, "path", p
);
2999 if (strstart(filename
, "pipe:", &p
)) {
3000 qemu_opt_set(opts
, "backend", "pipe");
3001 qemu_opt_set(opts
, "path", p
);
3004 if (strstart(filename
, "tcp:", &p
) ||
3005 strstart(filename
, "telnet:", &p
)) {
3006 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3008 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3011 qemu_opt_set(opts
, "backend", "socket");
3012 qemu_opt_set(opts
, "host", host
);
3013 qemu_opt_set(opts
, "port", port
);
3014 if (p
[pos
] == ',') {
3015 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3018 if (strstart(filename
, "telnet:", &p
))
3019 qemu_opt_set(opts
, "telnet", "on");
3022 if (strstart(filename
, "udp:", &p
)) {
3023 qemu_opt_set(opts
, "backend", "udp");
3024 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3026 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3030 qemu_opt_set(opts
, "host", host
);
3031 qemu_opt_set(opts
, "port", port
);
3032 if (p
[pos
] == '@') {
3034 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3036 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3040 qemu_opt_set(opts
, "localaddr", host
);
3041 qemu_opt_set(opts
, "localport", port
);
3045 if (strstart(filename
, "unix:", &p
)) {
3046 qemu_opt_set(opts
, "backend", "socket");
3047 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3051 if (strstart(filename
, "/dev/parport", NULL
) ||
3052 strstart(filename
, "/dev/ppi", NULL
)) {
3053 qemu_opt_set(opts
, "backend", "parport");
3054 qemu_opt_set(opts
, "path", filename
);
3057 if (strstart(filename
, "/dev/", NULL
)) {
3058 qemu_opt_set(opts
, "backend", "tty");
3059 qemu_opt_set(opts
, "path", filename
);
3064 qemu_opts_del(opts
);
3068 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3071 const char *path
= qemu_opt_get(opts
, "path");
3074 error_setg(errp
, "chardev: file: no filename given");
3077 backend
->file
= g_new0(ChardevFile
, 1);
3078 backend
->file
->out
= g_strdup(path
);
3081 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3084 backend
->stdio
= g_new0(ChardevStdio
, 1);
3085 backend
->stdio
->has_signal
= true;
3086 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3089 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3092 const char *device
= qemu_opt_get(opts
, "path");
3094 if (device
== NULL
) {
3095 error_setg(errp
, "chardev: serial/tty: no device path given");
3098 backend
->serial
= g_new0(ChardevHostdev
, 1);
3099 backend
->serial
->device
= g_strdup(device
);
3102 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3105 const char *device
= qemu_opt_get(opts
, "path");
3107 if (device
== NULL
) {
3108 error_setg(errp
, "chardev: parallel: no device path given");
3111 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3112 backend
->parallel
->device
= g_strdup(device
);
3115 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3118 const char *device
= qemu_opt_get(opts
, "path");
3120 if (device
== NULL
) {
3121 error_setg(errp
, "chardev: pipe: no device path given");
3124 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3125 backend
->pipe
->device
= g_strdup(device
);
3128 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3133 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3135 val
= qemu_opt_get_size(opts
, "size", 0);
3137 backend
->ringbuf
->has_size
= true;
3138 backend
->ringbuf
->size
= val
;
3142 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3145 const char *chardev
= qemu_opt_get(opts
, "chardev");
3147 if (chardev
== NULL
) {
3148 error_setg(errp
, "chardev: mux: no chardev given");
3151 backend
->mux
= g_new0(ChardevMux
, 1);
3152 backend
->mux
->chardev
= g_strdup(chardev
);
3155 typedef struct CharDriver
{
3158 CharDriverState
*(*open
)(QemuOpts
*opts
);
3159 /* new, qapi-based */
3160 ChardevBackendKind kind
;
3161 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3164 static GSList
*backends
;
3166 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3170 s
= g_malloc0(sizeof(*s
));
3171 s
->name
= g_strdup(name
);
3174 backends
= g_slist_append(backends
, s
);
3177 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3178 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3182 s
= g_malloc0(sizeof(*s
));
3183 s
->name
= g_strdup(name
);
3187 backends
= g_slist_append(backends
, s
);
3190 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3191 void (*init
)(struct CharDriverState
*s
),
3195 CharDriverState
*chr
;
3198 if (qemu_opts_id(opts
) == NULL
) {
3199 error_setg(errp
, "chardev: no id specified");
3203 if (qemu_opt_get(opts
, "backend") == NULL
) {
3204 error_setg(errp
, "chardev: \"%s\" missing backend",
3205 qemu_opts_id(opts
));
3208 for (i
= backends
; i
; i
= i
->next
) {
3211 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3216 error_setg(errp
, "chardev: backend \"%s\" not found",
3217 qemu_opt_get(opts
, "backend"));
3222 /* using new, qapi init */
3223 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3224 ChardevReturn
*ret
= NULL
;
3225 const char *id
= qemu_opts_id(opts
);
3228 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3229 bid
= g_strdup_printf("%s-base", id
);
3233 backend
->kind
= cd
->kind
;
3235 cd
->parse(opts
, backend
, errp
);
3236 if (error_is_set(errp
)) {
3240 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3241 if (error_is_set(errp
)) {
3246 qapi_free_ChardevBackend(backend
);
3247 qapi_free_ChardevReturn(ret
);
3248 backend
= g_new0(ChardevBackend
, 1);
3249 backend
->mux
= g_new0(ChardevMux
, 1);
3250 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3251 backend
->mux
->chardev
= g_strdup(bid
);
3252 ret
= qmp_chardev_add(id
, backend
, errp
);
3253 if (error_is_set(errp
)) {
3254 chr
= qemu_chr_find(bid
);
3255 qemu_chr_delete(chr
);
3261 chr
= qemu_chr_find(id
);
3265 qapi_free_ChardevBackend(backend
);
3266 qapi_free_ChardevReturn(ret
);
3271 chr
= cd
->open(opts
);
3273 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3274 qemu_opt_get(opts
, "backend"));
3279 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3281 /* if we didn't create the chardev via qmp_chardev_add, we
3282 * need to send the OPENED event here
3284 if (!chr
->explicit_be_open
) {
3285 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3287 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3289 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3290 CharDriverState
*base
= chr
;
3291 int len
= strlen(qemu_opts_id(opts
)) + 6;
3292 base
->label
= g_malloc(len
);
3293 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3294 chr
= qemu_chr_open_mux(base
);
3295 chr
->filename
= base
->filename
;
3296 chr
->avail_connections
= MAX_MUX
;
3297 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3299 chr
->avail_connections
= 1;
3301 chr
->label
= g_strdup(qemu_opts_id(opts
));
3306 qemu_opts_del(opts
);
3310 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3313 CharDriverState
*chr
;
3317 if (strstart(filename
, "chardev:", &p
)) {
3318 return qemu_chr_find(p
);
3321 opts
= qemu_chr_parse_compat(label
, filename
);
3325 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3327 error_report("%s", error_get_pretty(err
));
3330 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3331 qemu_chr_fe_claim_no_fail(chr
);
3332 monitor_init(chr
, MONITOR_USE_READLINE
);
3337 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3339 if (chr
->chr_set_echo
) {
3340 chr
->chr_set_echo(chr
, echo
);
3344 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3346 if (chr
->fe_open
== fe_open
) {
3349 chr
->fe_open
= fe_open
;
3350 if (chr
->chr_set_fe_open
) {
3351 chr
->chr_set_fe_open(chr
, fe_open
);
3355 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3357 if (chr
->chr_fe_event
) {
3358 chr
->chr_fe_event(chr
, event
);
3362 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3363 GIOFunc func
, void *user_data
)
3368 if (s
->chr_add_watch
== NULL
) {
3372 src
= s
->chr_add_watch(s
, cond
);
3373 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3374 tag
= g_source_attach(src
, NULL
);
3375 g_source_unref(src
);
3380 int qemu_chr_fe_claim(CharDriverState
*s
)
3382 if (s
->avail_connections
< 1) {
3385 s
->avail_connections
--;
3389 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3391 if (qemu_chr_fe_claim(s
) != 0) {
3392 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3393 __func__
, s
->label
);
3398 void qemu_chr_fe_release(CharDriverState
*s
)
3400 s
->avail_connections
++;
3403 void qemu_chr_delete(CharDriverState
*chr
)
3405 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3406 if (chr
->chr_close
) {
3407 chr
->chr_close(chr
);
3409 g_free(chr
->filename
);
3412 qemu_opts_del(chr
->opts
);
3417 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3419 ChardevInfoList
*chr_list
= NULL
;
3420 CharDriverState
*chr
;
3422 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3423 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3424 info
->value
= g_malloc0(sizeof(*info
->value
));
3425 info
->value
->label
= g_strdup(chr
->label
);
3426 info
->value
->filename
= g_strdup(chr
->filename
);
3428 info
->next
= chr_list
;
3435 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3437 ChardevBackendInfoList
*backend_list
= NULL
;
3438 CharDriver
*c
= NULL
;
3441 for (i
= backends
; i
; i
= i
->next
) {
3442 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3444 info
->value
= g_malloc0(sizeof(*info
->value
));
3445 info
->value
->name
= g_strdup(c
->name
);
3447 info
->next
= backend_list
;
3448 backend_list
= info
;
3451 return backend_list
;
3454 CharDriverState
*qemu_chr_find(const char *name
)
3456 CharDriverState
*chr
;
3458 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3459 if (strcmp(chr
->label
, name
) != 0)
3466 /* Get a character (serial) device interface. */
3467 CharDriverState
*qemu_char_get_next_serial(void)
3469 static int next_serial
;
3470 CharDriverState
*chr
;
3472 /* FIXME: This function needs to go away: use chardev properties! */
3474 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3475 chr
= serial_hds
[next_serial
++];
3476 qemu_chr_fe_claim_no_fail(chr
);
3482 QemuOptsList qemu_chardev_opts
= {
3484 .implied_opt_name
= "backend",
3485 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3489 .type
= QEMU_OPT_STRING
,
3492 .type
= QEMU_OPT_STRING
,
3495 .type
= QEMU_OPT_STRING
,
3498 .type
= QEMU_OPT_STRING
,
3500 .name
= "localaddr",
3501 .type
= QEMU_OPT_STRING
,
3503 .name
= "localport",
3504 .type
= QEMU_OPT_STRING
,
3507 .type
= QEMU_OPT_NUMBER
,
3510 .type
= QEMU_OPT_BOOL
,
3513 .type
= QEMU_OPT_BOOL
,
3516 .type
= QEMU_OPT_BOOL
,
3519 .type
= QEMU_OPT_BOOL
,
3522 .type
= QEMU_OPT_BOOL
,
3525 .type
= QEMU_OPT_BOOL
,
3528 .type
= QEMU_OPT_NUMBER
,
3531 .type
= QEMU_OPT_NUMBER
,
3534 .type
= QEMU_OPT_NUMBER
,
3537 .type
= QEMU_OPT_NUMBER
,
3540 .type
= QEMU_OPT_BOOL
,
3543 .type
= QEMU_OPT_BOOL
,
3546 .type
= QEMU_OPT_STRING
,
3549 .type
= QEMU_OPT_NUMBER
,
3552 .type
= QEMU_OPT_SIZE
,
3555 .type
= QEMU_OPT_STRING
,
3557 { /* end of list */ }
3563 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3568 error_setg(errp
, "input file not supported");
3572 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3573 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3574 if (out
== INVALID_HANDLE_VALUE
) {
3575 error_setg(errp
, "open %s failed", file
->out
);
3578 return qemu_chr_open_win_file(out
);
3581 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3584 return qemu_chr_open_win_path(serial
->device
);
3587 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3590 error_setg(errp
, "character device backend type 'parallel' not supported");
3596 static int qmp_chardev_open_file_source(char *src
, int flags
,
3601 TFR(fd
= qemu_open(src
, flags
, 0666));
3603 error_setg_file_open(errp
, errno
, src
);
3608 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3610 int flags
, in
= -1, out
= -1;
3612 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3613 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3614 if (error_is_set(errp
)) {
3620 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3621 if (error_is_set(errp
)) {
3627 return qemu_chr_open_fd(in
, out
);
3630 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3633 #ifdef HAVE_CHARDEV_TTY
3636 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3637 if (error_is_set(errp
)) {
3640 qemu_set_nonblock(fd
);
3641 return qemu_chr_open_tty_fd(fd
);
3643 error_setg(errp
, "character device backend type 'serial' not supported");
3648 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3651 #ifdef HAVE_CHARDEV_PARPORT
3654 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3655 if (error_is_set(errp
)) {
3658 return qemu_chr_open_pp_fd(fd
);
3660 error_setg(errp
, "character device backend type 'parallel' not supported");
3667 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3670 SocketAddress
*addr
= sock
->addr
;
3671 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3672 bool is_listen
= sock
->has_server
? sock
->server
: true;
3673 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3674 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3678 fd
= socket_listen(addr
, errp
);
3680 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3682 if (error_is_set(errp
)) {
3685 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3686 is_telnet
, is_waitconnect
, errp
);
3689 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3694 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3695 if (error_is_set(errp
)) {
3698 return qemu_chr_open_udp_fd(fd
);
3701 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3704 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3705 CharDriverState
*base
, *chr
= NULL
;
3707 chr
= qemu_chr_find(id
);
3709 error_setg(errp
, "Chardev '%s' already exists", id
);
3714 switch (backend
->kind
) {
3715 case CHARDEV_BACKEND_KIND_FILE
:
3716 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3718 case CHARDEV_BACKEND_KIND_SERIAL
:
3719 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3721 case CHARDEV_BACKEND_KIND_PARALLEL
:
3722 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3724 case CHARDEV_BACKEND_KIND_PIPE
:
3725 chr
= qemu_chr_open_pipe(backend
->pipe
);
3727 case CHARDEV_BACKEND_KIND_SOCKET
:
3728 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3730 case CHARDEV_BACKEND_KIND_UDP
:
3731 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
3733 #ifdef HAVE_CHARDEV_TTY
3734 case CHARDEV_BACKEND_KIND_PTY
:
3735 chr
= qemu_chr_open_pty(id
, ret
);
3738 case CHARDEV_BACKEND_KIND_NULL
:
3739 chr
= qemu_chr_open_null();
3741 case CHARDEV_BACKEND_KIND_MUX
:
3742 base
= qemu_chr_find(backend
->mux
->chardev
);
3744 error_setg(errp
, "mux: base chardev %s not found",
3745 backend
->mux
->chardev
);
3748 chr
= qemu_chr_open_mux(base
);
3750 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3751 chr
= qemu_chr_open_msmouse();
3753 #ifdef CONFIG_BRLAPI
3754 case CHARDEV_BACKEND_KIND_BRAILLE
:
3755 chr
= chr_baum_init();
3758 case CHARDEV_BACKEND_KIND_STDIO
:
3759 chr
= qemu_chr_open_stdio(backend
->stdio
);
3762 case CHARDEV_BACKEND_KIND_CONSOLE
:
3763 chr
= qemu_chr_open_win_con();
3767 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3768 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3770 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3771 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3774 case CHARDEV_BACKEND_KIND_VC
:
3775 chr
= vc_init(backend
->vc
);
3777 case CHARDEV_BACKEND_KIND_RINGBUF
:
3778 case CHARDEV_BACKEND_KIND_MEMORY
:
3779 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
3782 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3786 if (chr
== NULL
&& !error_is_set(errp
)) {
3787 error_setg(errp
, "Failed to create chardev");
3790 chr
->label
= g_strdup(id
);
3791 chr
->avail_connections
=
3792 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3793 if (!chr
->filename
) {
3794 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
3796 if (!chr
->explicit_be_open
) {
3797 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3799 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3807 void qmp_chardev_remove(const char *id
, Error
**errp
)
3809 CharDriverState
*chr
;
3811 chr
= qemu_chr_find(id
);
3813 error_setg(errp
, "Chardev '%s' not found", id
);
3816 if (chr
->chr_can_read
|| chr
->chr_read
||
3817 chr
->chr_event
|| chr
->handler_opaque
) {
3818 error_setg(errp
, "Chardev '%s' is busy", id
);
3821 qemu_chr_delete(chr
);
3824 static void register_types(void)
3826 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3827 register_char_driver("socket", qemu_chr_open_socket
);
3828 register_char_driver("udp", qemu_chr_open_udp
);
3829 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
3830 qemu_chr_parse_ringbuf
);
3831 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3832 qemu_chr_parse_file_out
);
3833 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3834 qemu_chr_parse_stdio
);
3835 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3836 qemu_chr_parse_serial
);
3837 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3838 qemu_chr_parse_serial
);
3839 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3840 qemu_chr_parse_parallel
);
3841 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3842 qemu_chr_parse_parallel
);
3843 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3844 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3845 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3846 qemu_chr_parse_pipe
);
3847 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
3848 qemu_chr_parse_mux
);
3849 /* Bug-compatibility: */
3850 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3851 qemu_chr_parse_ringbuf
);
3852 /* this must be done after machine init, since we register FEs with muxes
3853 * as part of realize functions like serial_isa_realizefn when -nographic
3856 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
3859 type_init(register_types
);