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 "char/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(__GLIBC__)
58 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
63 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64 #include <dev/ppbus/ppi.h>
65 #include <dev/ppbus/ppbconf.h>
66 #elif defined(__DragonFly__)
67 #include <dev/misc/ppi/ppi.h>
68 #include <bus/ppbus/ppbconf.h>
74 #include <linux/ppdev.h>
75 #include <linux/parport.h>
79 #include <sys/ethernet.h>
80 #include <sys/sockio.h>
81 #include <netinet/arp.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_icmp.h> // must come after ip.h
86 #include <netinet/udp.h>
87 #include <netinet/tcp.h>
95 #include "qemu/sockets.h"
96 #include "ui/qemu-spice.h"
98 #define READ_BUF_LEN 4096
100 /***********************************************************/
101 /* character device */
103 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
104 QTAILQ_HEAD_INITIALIZER(chardevs
);
106 void qemu_chr_be_event(CharDriverState
*s
, int event
)
108 /* Keep track if the char device is open */
110 case CHR_EVENT_OPENED
:
113 case CHR_EVENT_CLOSED
:
120 s
->chr_event(s
->handler_opaque
, event
);
123 static gboolean
qemu_chr_be_generic_open_bh(gpointer opaque
)
125 CharDriverState
*s
= opaque
;
126 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
131 void qemu_chr_be_generic_open(CharDriverState
*s
)
133 if (s
->idle_tag
== 0) {
134 s
->idle_tag
= g_idle_add(qemu_chr_be_generic_open_bh
, s
);
138 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
140 return s
->chr_write(s
, buf
, len
);
143 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
148 while (offset
< len
) {
150 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
151 if (res
== -1 && errno
== EAGAIN
) {
154 } while (res
== -1 && errno
== EAGAIN
);
170 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
174 return s
->chr_ioctl(s
, cmd
, arg
);
177 int qemu_chr_be_can_write(CharDriverState
*s
)
179 if (!s
->chr_can_read
)
181 return s
->chr_can_read(s
->handler_opaque
);
184 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
187 s
->chr_read(s
->handler_opaque
, buf
, len
);
191 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
193 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
196 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
198 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
201 void qemu_chr_accept_input(CharDriverState
*s
)
203 if (s
->chr_accept_input
)
204 s
->chr_accept_input(s
);
208 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
210 char buf
[READ_BUF_LEN
];
213 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
214 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
218 void qemu_chr_add_handlers(CharDriverState
*s
,
219 IOCanReadHandler
*fd_can_read
,
220 IOReadHandler
*fd_read
,
221 IOEventHandler
*fd_event
,
226 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
227 /* chr driver being released. */
228 ++s
->avail_connections
;
233 s
->chr_can_read
= fd_can_read
;
234 s
->chr_read
= fd_read
;
235 s
->chr_event
= fd_event
;
236 s
->handler_opaque
= opaque
;
237 if (s
->chr_update_read_handler
)
238 s
->chr_update_read_handler(s
);
240 if (!s
->explicit_fe_open
) {
241 qemu_chr_fe_set_open(s
, fe_open
);
244 /* We're connecting to an already opened device, so let's make sure we
245 also get the open event */
247 qemu_chr_be_generic_open(s
);
251 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
256 static CharDriverState
*qemu_chr_open_null(void)
258 CharDriverState
*chr
;
260 chr
= g_malloc0(sizeof(CharDriverState
));
261 chr
->chr_write
= null_chr_write
;
265 /* MUX driver for serial I/O splitting */
267 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
268 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
270 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
271 IOReadHandler
*chr_read
[MAX_MUX
];
272 IOEventHandler
*chr_event
[MAX_MUX
];
273 void *ext_opaque
[MAX_MUX
];
274 CharDriverState
*drv
;
279 /* Intermediate input buffer allows to catch escape sequences even if the
280 currently active device is not accepting any input - but only until it
282 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
287 int64_t timestamps_start
;
291 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
293 MuxDriver
*d
= chr
->opaque
;
295 if (!d
->timestamps
) {
296 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
301 for (i
= 0; i
< len
; i
++) {
307 ti
= qemu_get_clock_ms(rt_clock
);
308 if (d
->timestamps_start
== -1)
309 d
->timestamps_start
= ti
;
310 ti
-= d
->timestamps_start
;
312 snprintf(buf1
, sizeof(buf1
),
313 "[%02d:%02d:%02d.%03d] ",
318 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
321 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
322 if (buf
[i
] == '\n') {
330 static const char * const mux_help
[] = {
331 "% h print this help\n\r",
332 "% x exit emulator\n\r",
333 "% s save disk data back to file (if -snapshot)\n\r",
334 "% t toggle console timestamps\n\r"
335 "% b send break (magic sysrq)\n\r",
336 "% c switch between console and monitor\n\r",
341 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
342 static void mux_print_help(CharDriverState
*chr
)
345 char ebuf
[15] = "Escape-Char";
346 char cbuf
[50] = "\n\r";
348 if (term_escape_char
> 0 && term_escape_char
< 26) {
349 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
350 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
352 snprintf(cbuf
, sizeof(cbuf
),
353 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
356 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
357 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
358 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
359 if (mux_help
[i
][j
] == '%')
360 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
362 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
367 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
369 if (d
->chr_event
[mux_nr
])
370 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
373 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
375 if (d
->term_got_escape
) {
376 d
->term_got_escape
= 0;
377 if (ch
== term_escape_char
)
386 const char *term
= "QEMU: Terminated\n\r";
387 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
395 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
398 /* Switch to the next registered device */
399 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
401 if (d
->focus
>= d
->mux_cnt
)
403 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
406 d
->timestamps
= !d
->timestamps
;
407 d
->timestamps_start
= -1;
411 } else if (ch
== term_escape_char
) {
412 d
->term_got_escape
= 1;
420 static void mux_chr_accept_input(CharDriverState
*chr
)
422 MuxDriver
*d
= chr
->opaque
;
425 while (d
->prod
[m
] != d
->cons
[m
] &&
426 d
->chr_can_read
[m
] &&
427 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
428 d
->chr_read
[m
](d
->ext_opaque
[m
],
429 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
433 static int mux_chr_can_read(void *opaque
)
435 CharDriverState
*chr
= opaque
;
436 MuxDriver
*d
= chr
->opaque
;
439 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
441 if (d
->chr_can_read
[m
])
442 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
446 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
448 CharDriverState
*chr
= opaque
;
449 MuxDriver
*d
= chr
->opaque
;
453 mux_chr_accept_input (opaque
);
455 for(i
= 0; i
< size
; i
++)
456 if (mux_proc_byte(chr
, d
, buf
[i
])) {
457 if (d
->prod
[m
] == d
->cons
[m
] &&
458 d
->chr_can_read
[m
] &&
459 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
460 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
462 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
466 static void mux_chr_event(void *opaque
, int event
)
468 CharDriverState
*chr
= opaque
;
469 MuxDriver
*d
= chr
->opaque
;
472 /* Send the event to all registered listeners */
473 for (i
= 0; i
< d
->mux_cnt
; i
++)
474 mux_chr_send_event(d
, i
, event
);
477 static void mux_chr_update_read_handler(CharDriverState
*chr
)
479 MuxDriver
*d
= chr
->opaque
;
481 if (d
->mux_cnt
>= MAX_MUX
) {
482 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
485 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
486 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
487 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
488 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
489 /* Fix up the real driver with mux routines */
490 if (d
->mux_cnt
== 0) {
491 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
494 if (d
->focus
!= -1) {
495 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
497 d
->focus
= d
->mux_cnt
;
499 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
502 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
504 CharDriverState
*chr
;
507 chr
= g_malloc0(sizeof(CharDriverState
));
508 d
= g_malloc0(sizeof(MuxDriver
));
513 chr
->chr_write
= mux_chr_write
;
514 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
515 chr
->chr_accept_input
= mux_chr_accept_input
;
516 /* Frontend guest-open / -close notification is not support with muxes */
517 chr
->chr_guest_open
= NULL
;
518 chr
->chr_guest_close
= NULL
;
520 /* Muxes are always open on creation */
521 qemu_chr_be_generic_open(chr
);
528 int send_all(int fd
, const void *buf
, int len1
)
534 ret
= send(fd
, buf
, len
, 0);
536 errno
= WSAGetLastError();
537 if (errno
!= WSAEWOULDBLOCK
) {
540 } else if (ret
== 0) {
552 int send_all(int fd
, const void *_buf
, int len1
)
555 const uint8_t *buf
= _buf
;
559 ret
= write(fd
, buf
, len
);
561 if (errno
!= EINTR
&& errno
!= EAGAIN
)
563 } else if (ret
== 0) {
573 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
579 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
581 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
598 typedef struct IOWatchPoll
603 IOCanReadHandler
*fd_can_read
;
606 QTAILQ_ENTRY(IOWatchPoll
) node
;
609 static QTAILQ_HEAD(, IOWatchPoll
) io_watch_poll_list
=
610 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list
);
612 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
616 QTAILQ_FOREACH(i
, &io_watch_poll_list
, node
) {
617 if (i
->src
== source
) {
625 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
627 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
629 iwp
->max_size
= iwp
->fd_can_read(iwp
->opaque
);
630 if (iwp
->max_size
== 0) {
634 return g_io_watch_funcs
.prepare(source
, timeout_
);
637 static gboolean
io_watch_poll_check(GSource
*source
)
639 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
641 if (iwp
->max_size
== 0) {
645 return g_io_watch_funcs
.check(source
);
648 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
651 return g_io_watch_funcs
.dispatch(source
, callback
, user_data
);
654 static void io_watch_poll_finalize(GSource
*source
)
656 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
657 QTAILQ_REMOVE(&io_watch_poll_list
, iwp
, node
);
658 g_io_watch_funcs
.finalize(source
);
661 static GSourceFuncs io_watch_poll_funcs
= {
662 .prepare
= io_watch_poll_prepare
,
663 .check
= io_watch_poll_check
,
664 .dispatch
= io_watch_poll_dispatch
,
665 .finalize
= io_watch_poll_finalize
,
668 /* Can only be used for read */
669 static guint
io_add_watch_poll(GIOChannel
*channel
,
670 IOCanReadHandler
*fd_can_read
,
678 src
= g_io_create_watch(channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
679 g_source_set_funcs(src
, &io_watch_poll_funcs
);
680 g_source_set_callback(src
, (GSourceFunc
)fd_read
, user_data
, NULL
);
681 tag
= g_source_attach(src
, NULL
);
684 iwp
= g_malloc0(sizeof(*iwp
));
687 iwp
->fd_can_read
= fd_can_read
;
688 iwp
->opaque
= user_data
;
690 QTAILQ_INSERT_HEAD(&io_watch_poll_list
, iwp
, node
);
696 static GIOChannel
*io_channel_from_fd(int fd
)
704 chan
= g_io_channel_unix_new(fd
);
706 g_io_channel_set_encoding(chan
, NULL
, NULL
);
707 g_io_channel_set_buffered(chan
, FALSE
);
713 static GIOChannel
*io_channel_from_socket(int fd
)
722 chan
= g_io_channel_win32_new_socket(fd
);
724 chan
= g_io_channel_unix_new(fd
);
727 g_io_channel_set_encoding(chan
, NULL
, NULL
);
728 g_io_channel_set_buffered(chan
, FALSE
);
733 static int io_channel_send_all(GIOChannel
*fd
, const void *_buf
, int len1
)
738 const uint8_t *buf
= _buf
;
742 status
= g_io_channel_write_chars(fd
, (const gchar
*)buf
, len
,
743 &bytes_written
, NULL
);
744 if (status
!= G_IO_STATUS_NORMAL
) {
745 if (status
== G_IO_STATUS_AGAIN
) {
752 } else if (status
== G_IO_STATUS_EOF
) {
755 buf
+= bytes_written
;
756 len
-= bytes_written
;
764 typedef struct FDCharDriver
{
765 CharDriverState
*chr
;
766 GIOChannel
*fd_in
, *fd_out
;
769 QTAILQ_ENTRY(FDCharDriver
) node
;
772 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
774 FDCharDriver
*s
= chr
->opaque
;
776 return io_channel_send_all(s
->fd_out
, buf
, len
);
779 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
781 CharDriverState
*chr
= opaque
;
782 FDCharDriver
*s
= chr
->opaque
;
784 uint8_t buf
[READ_BUF_LEN
];
789 if (len
> s
->max_size
) {
796 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
797 len
, &bytes_read
, NULL
);
798 if (status
== G_IO_STATUS_EOF
) {
799 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
802 if (status
== G_IO_STATUS_NORMAL
) {
803 qemu_chr_be_write(chr
, buf
, bytes_read
);
809 static int fd_chr_read_poll(void *opaque
)
811 CharDriverState
*chr
= opaque
;
812 FDCharDriver
*s
= chr
->opaque
;
814 s
->max_size
= qemu_chr_be_can_write(chr
);
818 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
820 FDCharDriver
*s
= chr
->opaque
;
821 return g_io_create_watch(s
->fd_out
, cond
);
824 static void fd_chr_update_read_handler(CharDriverState
*chr
)
826 FDCharDriver
*s
= chr
->opaque
;
829 g_source_remove(s
->fd_in_tag
);
833 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
837 static void fd_chr_close(struct CharDriverState
*chr
)
839 FDCharDriver
*s
= chr
->opaque
;
842 g_source_remove(s
->fd_in_tag
);
847 g_io_channel_unref(s
->fd_in
);
850 g_io_channel_unref(s
->fd_out
);
854 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
857 /* open a character device to a unix fd */
858 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
860 CharDriverState
*chr
;
863 chr
= g_malloc0(sizeof(CharDriverState
));
864 s
= g_malloc0(sizeof(FDCharDriver
));
865 s
->fd_in
= io_channel_from_fd(fd_in
);
866 s
->fd_out
= io_channel_from_fd(fd_out
);
867 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
870 chr
->chr_add_watch
= fd_chr_add_watch
;
871 chr
->chr_write
= fd_chr_write
;
872 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
873 chr
->chr_close
= fd_chr_close
;
875 qemu_chr_be_generic_open(chr
);
880 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
883 char filename_in
[256], filename_out
[256];
884 const char *filename
= opts
->device
;
886 if (filename
== NULL
) {
887 fprintf(stderr
, "chardev: pipe: no filename given\n");
891 snprintf(filename_in
, 256, "%s.in", filename
);
892 snprintf(filename_out
, 256, "%s.out", filename
);
893 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
894 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
895 if (fd_in
< 0 || fd_out
< 0) {
900 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
905 return qemu_chr_open_fd(fd_in
, fd_out
);
908 /* init terminal so that we can grab keys */
909 static struct termios oldtty
;
910 static int old_fd0_flags
;
911 static bool stdio_allow_signal
;
913 static void term_exit(void)
915 tcsetattr (0, TCSANOW
, &oldtty
);
916 fcntl(0, F_SETFL
, old_fd0_flags
);
919 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
925 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
926 |INLCR
|IGNCR
|ICRNL
|IXON
);
927 tty
.c_oflag
|= OPOST
;
928 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
929 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
934 /* if graphical mode, we allow Ctrl-C handling */
935 if (!stdio_allow_signal
)
936 tty
.c_lflag
&= ~ISIG
;
938 tcsetattr (0, TCSANOW
, &tty
);
941 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
947 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
949 CharDriverState
*chr
;
951 if (is_daemonized()) {
952 error_report("cannot use stdio with -daemonize");
955 old_fd0_flags
= fcntl(0, F_GETFL
);
956 tcgetattr (0, &oldtty
);
957 fcntl(0, F_SETFL
, O_NONBLOCK
);
960 chr
= qemu_chr_open_fd(0, 1);
961 chr
->chr_close
= qemu_chr_close_stdio
;
962 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
963 stdio_allow_signal
= display_type
!= DT_NOGRAPHIC
;
964 if (opts
->has_signal
) {
965 stdio_allow_signal
= opts
->signal
;
967 qemu_chr_fe_set_echo(chr
, false);
973 /* Once Solaris has openpty(), this is going to be removed. */
974 static int openpty(int *amaster
, int *aslave
, char *name
,
975 struct termios
*termp
, struct winsize
*winp
)
978 int mfd
= -1, sfd
= -1;
980 *amaster
= *aslave
= -1;
982 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
986 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
989 if ((slave
= ptsname(mfd
)) == NULL
)
992 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
995 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
996 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
1004 ioctl(sfd
, TIOCSWINSZ
, winp
);
1015 static void cfmakeraw (struct termios
*termios_p
)
1017 termios_p
->c_iflag
&=
1018 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
1019 termios_p
->c_oflag
&= ~OPOST
;
1020 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
1021 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
1022 termios_p
->c_cflag
|= CS8
;
1024 termios_p
->c_cc
[VMIN
] = 0;
1025 termios_p
->c_cc
[VTIME
] = 0;
1029 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1030 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1031 || defined(__GLIBC__)
1033 #define HAVE_CHARDEV_TTY 1
1044 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1045 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1047 static gboolean
pty_chr_timer(gpointer opaque
)
1049 struct CharDriverState
*chr
= opaque
;
1050 PtyCharDriver
*s
= chr
->opaque
;
1056 /* If we arrive here without polling being cleared due
1057 * read returning -EIO, then we are (re-)connected */
1058 pty_chr_state(chr
, 1);
1063 pty_chr_update_read_handler(chr
);
1069 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1071 PtyCharDriver
*s
= chr
->opaque
;
1074 g_source_remove(s
->timer_tag
);
1079 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1081 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1085 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1087 PtyCharDriver
*s
= chr
->opaque
;
1089 if (!s
->connected
) {
1090 /* guest sends data, check for (re-)connect */
1091 pty_chr_update_read_handler(chr
);
1094 return io_channel_send_all(s
->fd
, buf
, len
);
1097 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1099 PtyCharDriver
*s
= chr
->opaque
;
1100 return g_io_create_watch(s
->fd
, cond
);
1103 static int pty_chr_read_poll(void *opaque
)
1105 CharDriverState
*chr
= opaque
;
1106 PtyCharDriver
*s
= chr
->opaque
;
1108 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1109 return s
->read_bytes
;
1112 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1114 CharDriverState
*chr
= opaque
;
1115 PtyCharDriver
*s
= chr
->opaque
;
1117 uint8_t buf
[READ_BUF_LEN
];
1121 if (len
> s
->read_bytes
)
1122 len
= s
->read_bytes
;
1125 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1126 if (status
!= G_IO_STATUS_NORMAL
) {
1127 pty_chr_state(chr
, 0);
1130 pty_chr_state(chr
, 1);
1131 qemu_chr_be_write(chr
, buf
, size
);
1136 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1138 PtyCharDriver
*s
= chr
->opaque
;
1141 g_source_remove(s
->fd_tag
);
1144 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1147 * Short timeout here: just need wait long enougth that qemu makes
1148 * it through the poll loop once. When reconnected we want a
1149 * short timeout so we notice it almost instantly. Otherwise
1150 * read() gives us -EIO instantly, making pty_chr_state() reset the
1151 * timeout to the normal (much longer) poll interval before the
1154 pty_chr_rearm_timer(chr
, 10);
1157 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1159 PtyCharDriver
*s
= chr
->opaque
;
1162 g_source_remove(s
->fd_tag
);
1166 /* (re-)connect poll interval for idle guests: once per second.
1167 * We check more frequently in case the guests sends data to
1168 * the virtual device linked to our pty. */
1169 pty_chr_rearm_timer(chr
, 1000);
1172 qemu_chr_be_generic_open(chr
);
1178 static void pty_chr_close(struct CharDriverState
*chr
)
1180 PtyCharDriver
*s
= chr
->opaque
;
1184 g_source_remove(s
->fd_tag
);
1186 fd
= g_io_channel_unix_get_fd(s
->fd
);
1187 g_io_channel_unref(s
->fd
);
1190 g_source_remove(s
->timer_tag
);
1193 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1196 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1199 CharDriverState
*chr
;
1202 int master_fd
, slave_fd
;
1203 #if defined(__OpenBSD__) || defined(__DragonFly__)
1204 char pty_name
[PATH_MAX
];
1205 #define q_ptsname(x) pty_name
1207 char *pty_name
= NULL
;
1208 #define q_ptsname(x) ptsname(x)
1211 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1215 /* Set raw attributes on the pty. */
1216 tcgetattr(slave_fd
, &tty
);
1218 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1221 chr
= g_malloc0(sizeof(CharDriverState
));
1223 chr
->filename
= g_strdup_printf("pty:%s", q_ptsname(master_fd
));
1224 ret
->pty
= g_strdup(q_ptsname(master_fd
));
1225 ret
->has_pty
= true;
1227 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1228 q_ptsname(master_fd
), id
);
1230 s
= g_malloc0(sizeof(PtyCharDriver
));
1232 chr
->chr_write
= pty_chr_write
;
1233 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1234 chr
->chr_close
= pty_chr_close
;
1235 chr
->chr_add_watch
= pty_chr_add_watch
;
1237 s
->fd
= io_channel_from_fd(master_fd
);
1243 static void tty_serial_init(int fd
, int speed
,
1244 int parity
, int data_bits
, int stop_bits
)
1250 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1251 speed
, parity
, data_bits
, stop_bits
);
1253 tcgetattr (fd
, &tty
);
1255 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1256 speed
= speed
* 10 / 11;
1273 /* Non-Posix values follow. They may be unsupported on some systems. */
1275 check_speed(115200);
1277 check_speed(230400);
1280 check_speed(460800);
1283 check_speed(500000);
1286 check_speed(576000);
1289 check_speed(921600);
1292 check_speed(1000000);
1295 check_speed(1152000);
1298 check_speed(1500000);
1301 check_speed(2000000);
1304 check_speed(2500000);
1307 check_speed(3000000);
1310 check_speed(3500000);
1313 check_speed(4000000);
1318 cfsetispeed(&tty
, spd
);
1319 cfsetospeed(&tty
, spd
);
1321 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1322 |INLCR
|IGNCR
|ICRNL
|IXON
);
1323 tty
.c_oflag
|= OPOST
;
1324 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1325 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1346 tty
.c_cflag
|= PARENB
;
1349 tty
.c_cflag
|= PARENB
| PARODD
;
1353 tty
.c_cflag
|= CSTOPB
;
1355 tcsetattr (fd
, TCSANOW
, &tty
);
1358 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1360 FDCharDriver
*s
= chr
->opaque
;
1363 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1365 QEMUSerialSetParams
*ssp
= arg
;
1366 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1367 ssp
->speed
, ssp
->parity
,
1368 ssp
->data_bits
, ssp
->stop_bits
);
1371 case CHR_IOCTL_SERIAL_SET_BREAK
:
1373 int enable
= *(int *)arg
;
1375 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1379 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1382 int *targ
= (int *)arg
;
1383 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1385 if (sarg
& TIOCM_CTS
)
1386 *targ
|= CHR_TIOCM_CTS
;
1387 if (sarg
& TIOCM_CAR
)
1388 *targ
|= CHR_TIOCM_CAR
;
1389 if (sarg
& TIOCM_DSR
)
1390 *targ
|= CHR_TIOCM_DSR
;
1391 if (sarg
& TIOCM_RI
)
1392 *targ
|= CHR_TIOCM_RI
;
1393 if (sarg
& TIOCM_DTR
)
1394 *targ
|= CHR_TIOCM_DTR
;
1395 if (sarg
& TIOCM_RTS
)
1396 *targ
|= CHR_TIOCM_RTS
;
1399 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1401 int sarg
= *(int *)arg
;
1403 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1404 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1405 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1406 if (sarg
& CHR_TIOCM_CTS
)
1408 if (sarg
& CHR_TIOCM_CAR
)
1410 if (sarg
& CHR_TIOCM_DSR
)
1412 if (sarg
& CHR_TIOCM_RI
)
1414 if (sarg
& CHR_TIOCM_DTR
)
1416 if (sarg
& CHR_TIOCM_RTS
)
1418 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1427 static void qemu_chr_close_tty(CharDriverState
*chr
)
1429 FDCharDriver
*s
= chr
->opaque
;
1433 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1443 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1445 CharDriverState
*chr
;
1447 tty_serial_init(fd
, 115200, 'N', 8, 1);
1448 chr
= qemu_chr_open_fd(fd
, fd
);
1449 chr
->chr_ioctl
= tty_serial_ioctl
;
1450 chr
->chr_close
= qemu_chr_close_tty
;
1453 #endif /* __linux__ || __sun__ */
1455 #if defined(__linux__)
1457 #define HAVE_CHARDEV_PARPORT 1
1462 } ParallelCharDriver
;
1464 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1466 if (s
->mode
!= mode
) {
1468 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1475 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1477 ParallelCharDriver
*drv
= chr
->opaque
;
1482 case CHR_IOCTL_PP_READ_DATA
:
1483 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1485 *(uint8_t *)arg
= b
;
1487 case CHR_IOCTL_PP_WRITE_DATA
:
1488 b
= *(uint8_t *)arg
;
1489 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1492 case CHR_IOCTL_PP_READ_CONTROL
:
1493 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1495 /* Linux gives only the lowest bits, and no way to know data
1496 direction! For better compatibility set the fixed upper
1498 *(uint8_t *)arg
= b
| 0xc0;
1500 case CHR_IOCTL_PP_WRITE_CONTROL
:
1501 b
= *(uint8_t *)arg
;
1502 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1505 case CHR_IOCTL_PP_READ_STATUS
:
1506 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1508 *(uint8_t *)arg
= b
;
1510 case CHR_IOCTL_PP_DATA_DIR
:
1511 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1514 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1515 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1516 struct ParallelIOArg
*parg
= arg
;
1517 int n
= read(fd
, parg
->buffer
, parg
->count
);
1518 if (n
!= parg
->count
) {
1523 case CHR_IOCTL_PP_EPP_READ
:
1524 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1525 struct ParallelIOArg
*parg
= arg
;
1526 int n
= read(fd
, parg
->buffer
, parg
->count
);
1527 if (n
!= parg
->count
) {
1532 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1533 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1534 struct ParallelIOArg
*parg
= arg
;
1535 int n
= write(fd
, parg
->buffer
, parg
->count
);
1536 if (n
!= parg
->count
) {
1541 case CHR_IOCTL_PP_EPP_WRITE
:
1542 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1543 struct ParallelIOArg
*parg
= arg
;
1544 int n
= write(fd
, parg
->buffer
, parg
->count
);
1545 if (n
!= parg
->count
) {
1556 static void pp_close(CharDriverState
*chr
)
1558 ParallelCharDriver
*drv
= chr
->opaque
;
1561 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1562 ioctl(fd
, PPRELEASE
);
1565 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1568 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1570 CharDriverState
*chr
;
1571 ParallelCharDriver
*drv
;
1573 if (ioctl(fd
, PPCLAIM
) < 0) {
1578 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1580 drv
->mode
= IEEE1284_MODE_COMPAT
;
1582 chr
= g_malloc0(sizeof(CharDriverState
));
1583 chr
->chr_write
= null_chr_write
;
1584 chr
->chr_ioctl
= pp_ioctl
;
1585 chr
->chr_close
= pp_close
;
1588 qemu_chr_be_generic_open(chr
);
1592 #endif /* __linux__ */
1594 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1596 #define HAVE_CHARDEV_PARPORT 1
1598 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1600 int fd
= (int)(intptr_t)chr
->opaque
;
1604 case CHR_IOCTL_PP_READ_DATA
:
1605 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1607 *(uint8_t *)arg
= b
;
1609 case CHR_IOCTL_PP_WRITE_DATA
:
1610 b
= *(uint8_t *)arg
;
1611 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1614 case CHR_IOCTL_PP_READ_CONTROL
:
1615 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1617 *(uint8_t *)arg
= b
;
1619 case CHR_IOCTL_PP_WRITE_CONTROL
:
1620 b
= *(uint8_t *)arg
;
1621 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1624 case CHR_IOCTL_PP_READ_STATUS
:
1625 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1627 *(uint8_t *)arg
= b
;
1635 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1637 CharDriverState
*chr
;
1639 chr
= g_malloc0(sizeof(CharDriverState
));
1640 chr
->opaque
= (void *)(intptr_t)fd
;
1641 chr
->chr_write
= null_chr_write
;
1642 chr
->chr_ioctl
= pp_ioctl
;
1651 HANDLE hcom
, hrecv
, hsend
;
1652 OVERLAPPED orecv
, osend
;
1659 HANDLE hInputReadyEvent
;
1660 HANDLE hInputDoneEvent
;
1661 HANDLE hInputThread
;
1662 uint8_t win_stdio_buf
;
1663 } WinStdioCharState
;
1665 #define NSENDBUF 2048
1666 #define NRECVBUF 2048
1667 #define MAXCONNECT 1
1668 #define NTIMEOUT 5000
1670 static int win_chr_poll(void *opaque
);
1671 static int win_chr_pipe_poll(void *opaque
);
1673 static void win_chr_close(CharDriverState
*chr
)
1675 WinCharState
*s
= chr
->opaque
;
1678 CloseHandle(s
->hsend
);
1682 CloseHandle(s
->hrecv
);
1686 CloseHandle(s
->hcom
);
1690 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1692 qemu_del_polling_cb(win_chr_poll
, chr
);
1694 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1697 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1699 WinCharState
*s
= chr
->opaque
;
1701 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1706 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1708 fprintf(stderr
, "Failed CreateEvent\n");
1711 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1713 fprintf(stderr
, "Failed CreateEvent\n");
1717 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1718 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1719 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1720 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1725 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1726 fprintf(stderr
, "Failed SetupComm\n");
1730 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1731 size
= sizeof(COMMCONFIG
);
1732 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1733 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1734 CommConfigDialog(filename
, NULL
, &comcfg
);
1736 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1737 fprintf(stderr
, "Failed SetCommState\n");
1741 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1742 fprintf(stderr
, "Failed SetCommMask\n");
1746 cto
.ReadIntervalTimeout
= MAXDWORD
;
1747 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1748 fprintf(stderr
, "Failed SetCommTimeouts\n");
1752 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1753 fprintf(stderr
, "Failed ClearCommError\n");
1756 qemu_add_polling_cb(win_chr_poll
, chr
);
1764 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1766 WinCharState
*s
= chr
->opaque
;
1767 DWORD len
, ret
, size
, err
;
1770 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1771 s
->osend
.hEvent
= s
->hsend
;
1774 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1776 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1778 err
= GetLastError();
1779 if (err
== ERROR_IO_PENDING
) {
1780 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1798 static int win_chr_read_poll(CharDriverState
*chr
)
1800 WinCharState
*s
= chr
->opaque
;
1802 s
->max_size
= qemu_chr_be_can_write(chr
);
1806 static void win_chr_readfile(CharDriverState
*chr
)
1808 WinCharState
*s
= chr
->opaque
;
1810 uint8_t buf
[READ_BUF_LEN
];
1813 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1814 s
->orecv
.hEvent
= s
->hrecv
;
1815 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1817 err
= GetLastError();
1818 if (err
== ERROR_IO_PENDING
) {
1819 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1824 qemu_chr_be_write(chr
, buf
, size
);
1828 static void win_chr_read(CharDriverState
*chr
)
1830 WinCharState
*s
= chr
->opaque
;
1832 if (s
->len
> s
->max_size
)
1833 s
->len
= s
->max_size
;
1837 win_chr_readfile(chr
);
1840 static int win_chr_poll(void *opaque
)
1842 CharDriverState
*chr
= opaque
;
1843 WinCharState
*s
= chr
->opaque
;
1847 ClearCommError(s
->hcom
, &comerr
, &status
);
1848 if (status
.cbInQue
> 0) {
1849 s
->len
= status
.cbInQue
;
1850 win_chr_read_poll(chr
);
1857 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1859 CharDriverState
*chr
;
1862 chr
= g_malloc0(sizeof(CharDriverState
));
1863 s
= g_malloc0(sizeof(WinCharState
));
1865 chr
->chr_write
= win_chr_write
;
1866 chr
->chr_close
= win_chr_close
;
1868 if (win_chr_init(chr
, filename
) < 0) {
1873 qemu_chr_be_generic_open(chr
);
1877 static int win_chr_pipe_poll(void *opaque
)
1879 CharDriverState
*chr
= opaque
;
1880 WinCharState
*s
= chr
->opaque
;
1883 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1886 win_chr_read_poll(chr
);
1893 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1895 WinCharState
*s
= chr
->opaque
;
1903 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1905 fprintf(stderr
, "Failed CreateEvent\n");
1908 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1910 fprintf(stderr
, "Failed CreateEvent\n");
1914 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1915 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1916 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1918 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1919 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1920 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1925 ZeroMemory(&ov
, sizeof(ov
));
1926 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1927 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1929 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1933 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1935 fprintf(stderr
, "Failed GetOverlappedResult\n");
1937 CloseHandle(ov
.hEvent
);
1944 CloseHandle(ov
.hEvent
);
1947 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1956 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1958 const char *filename
= opts
->device
;
1959 CharDriverState
*chr
;
1962 chr
= g_malloc0(sizeof(CharDriverState
));
1963 s
= g_malloc0(sizeof(WinCharState
));
1965 chr
->chr_write
= win_chr_write
;
1966 chr
->chr_close
= win_chr_close
;
1968 if (win_chr_pipe_init(chr
, filename
) < 0) {
1973 qemu_chr_be_generic_open(chr
);
1977 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1979 CharDriverState
*chr
;
1982 chr
= g_malloc0(sizeof(CharDriverState
));
1983 s
= g_malloc0(sizeof(WinCharState
));
1986 chr
->chr_write
= win_chr_write
;
1987 qemu_chr_be_generic_open(chr
);
1991 static CharDriverState
*qemu_chr_open_win_con(void)
1993 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1996 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1998 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2005 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2015 static void win_stdio_wait_func(void *opaque
)
2017 CharDriverState
*chr
= opaque
;
2018 WinStdioCharState
*stdio
= chr
->opaque
;
2019 INPUT_RECORD buf
[4];
2024 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
2028 /* Avoid error storm */
2029 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2033 for (i
= 0; i
< dwSize
; i
++) {
2034 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2036 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2038 if (kev
->uChar
.AsciiChar
!= 0) {
2039 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2040 if (qemu_chr_be_can_write(chr
)) {
2041 uint8_t c
= kev
->uChar
.AsciiChar
;
2042 qemu_chr_be_write(chr
, &c
, 1);
2050 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2052 CharDriverState
*chr
= param
;
2053 WinStdioCharState
*stdio
= chr
->opaque
;
2059 /* Wait for one byte */
2060 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2062 /* Exit in case of error, continue if nothing read */
2070 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2071 if (stdio
->win_stdio_buf
== '\r') {
2075 /* Signal the main thread and wait until the byte was eaten */
2076 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2079 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2085 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2089 static void win_stdio_thread_wait_func(void *opaque
)
2091 CharDriverState
*chr
= opaque
;
2092 WinStdioCharState
*stdio
= chr
->opaque
;
2094 if (qemu_chr_be_can_write(chr
)) {
2095 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2098 SetEvent(stdio
->hInputDoneEvent
);
2101 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2103 WinStdioCharState
*stdio
= chr
->opaque
;
2106 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2109 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2111 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2115 static void win_stdio_close(CharDriverState
*chr
)
2117 WinStdioCharState
*stdio
= chr
->opaque
;
2119 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2120 CloseHandle(stdio
->hInputReadyEvent
);
2122 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2123 CloseHandle(stdio
->hInputDoneEvent
);
2125 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2126 TerminateThread(stdio
->hInputThread
, 0);
2129 g_free(chr
->opaque
);
2133 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2135 CharDriverState
*chr
;
2136 WinStdioCharState
*stdio
;
2140 chr
= g_malloc0(sizeof(CharDriverState
));
2141 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2143 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2144 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2145 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2149 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2151 chr
->opaque
= stdio
;
2152 chr
->chr_write
= win_stdio_write
;
2153 chr
->chr_close
= win_stdio_close
;
2156 if (qemu_add_wait_object(stdio
->hStdIn
,
2157 win_stdio_wait_func
, chr
)) {
2158 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2163 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2164 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2165 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2168 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2169 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2170 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2171 fprintf(stderr
, "cannot create stdio thread or event\n");
2174 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2175 win_stdio_thread_wait_func
, chr
)) {
2176 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2180 dwMode
|= ENABLE_LINE_INPUT
;
2183 /* set the terminal in raw mode */
2184 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2185 dwMode
|= ENABLE_PROCESSED_INPUT
;
2188 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2190 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2191 qemu_chr_fe_set_echo(chr
, false);
2195 #endif /* !_WIN32 */
2198 /***********************************************************/
2199 /* UDP Net console */
2205 uint8_t buf
[READ_BUF_LEN
];
2211 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2213 NetCharDriver
*s
= chr
->opaque
;
2214 gsize bytes_written
;
2217 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2218 if (status
== G_IO_STATUS_EOF
) {
2220 } else if (status
!= G_IO_STATUS_NORMAL
) {
2224 return bytes_written
;
2227 static int udp_chr_read_poll(void *opaque
)
2229 CharDriverState
*chr
= opaque
;
2230 NetCharDriver
*s
= chr
->opaque
;
2232 s
->max_size
= qemu_chr_be_can_write(chr
);
2234 /* If there were any stray characters in the queue process them
2237 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2238 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2240 s
->max_size
= qemu_chr_be_can_write(chr
);
2245 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2247 CharDriverState
*chr
= opaque
;
2248 NetCharDriver
*s
= chr
->opaque
;
2249 gsize bytes_read
= 0;
2252 if (s
->max_size
== 0)
2254 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2256 s
->bufcnt
= bytes_read
;
2257 s
->bufptr
= s
->bufcnt
;
2258 if (status
!= G_IO_STATUS_NORMAL
) {
2263 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2264 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2266 s
->max_size
= qemu_chr_be_can_write(chr
);
2272 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2274 NetCharDriver
*s
= chr
->opaque
;
2277 g_source_remove(s
->tag
);
2282 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2286 static void udp_chr_close(CharDriverState
*chr
)
2288 NetCharDriver
*s
= chr
->opaque
;
2290 g_source_remove(s
->tag
);
2293 g_io_channel_unref(s
->chan
);
2297 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2300 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2302 CharDriverState
*chr
= NULL
;
2303 NetCharDriver
*s
= NULL
;
2305 chr
= g_malloc0(sizeof(CharDriverState
));
2306 s
= g_malloc0(sizeof(NetCharDriver
));
2309 s
->chan
= io_channel_from_socket(s
->fd
);
2313 chr
->chr_write
= udp_chr_write
;
2314 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2315 chr
->chr_close
= udp_chr_close
;
2319 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2321 Error
*local_err
= NULL
;
2324 fd
= inet_dgram_opts(opts
, &local_err
);
2328 return qemu_chr_open_udp_fd(fd
);
2331 /***********************************************************/
2332 /* TCP Net console */
2336 GIOChannel
*chan
, *listen_chan
;
2337 guint tag
, listen_tag
;
2347 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2349 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2351 TCPCharDriver
*s
= chr
->opaque
;
2353 return io_channel_send_all(s
->chan
, buf
, len
);
2355 /* XXX: indicate an error ? */
2360 static int tcp_chr_read_poll(void *opaque
)
2362 CharDriverState
*chr
= opaque
;
2363 TCPCharDriver
*s
= chr
->opaque
;
2366 s
->max_size
= qemu_chr_be_can_write(chr
);
2371 #define IAC_BREAK 243
2372 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2374 uint8_t *buf
, int *size
)
2376 /* Handle any telnet client's basic IAC options to satisfy char by
2377 * char mode with no echo. All IAC options will be removed from
2378 * the buf and the do_telnetopt variable will be used to track the
2379 * state of the width of the IAC information.
2381 * IAC commands come in sets of 3 bytes with the exception of the
2382 * "IAC BREAK" command and the double IAC.
2388 for (i
= 0; i
< *size
; i
++) {
2389 if (s
->do_telnetopt
> 1) {
2390 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2391 /* Double IAC means send an IAC */
2395 s
->do_telnetopt
= 1;
2397 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2398 /* Handle IAC break commands by sending a serial break */
2399 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2404 if (s
->do_telnetopt
>= 4) {
2405 s
->do_telnetopt
= 1;
2408 if ((unsigned char)buf
[i
] == IAC
) {
2409 s
->do_telnetopt
= 2;
2420 static int tcp_get_msgfd(CharDriverState
*chr
)
2422 TCPCharDriver
*s
= chr
->opaque
;
2429 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2431 TCPCharDriver
*s
= chr
->opaque
;
2432 struct cmsghdr
*cmsg
;
2434 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2437 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2438 cmsg
->cmsg_level
!= SOL_SOCKET
||
2439 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2442 fd
= *((int *)CMSG_DATA(cmsg
));
2446 #ifndef MSG_CMSG_CLOEXEC
2447 qemu_set_cloexec(fd
);
2455 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2457 TCPCharDriver
*s
= chr
->opaque
;
2458 struct msghdr msg
= { NULL
, };
2459 struct iovec iov
[1];
2461 struct cmsghdr cmsg
;
2462 char control
[CMSG_SPACE(sizeof(int))];
2467 iov
[0].iov_base
= buf
;
2468 iov
[0].iov_len
= len
;
2472 msg
.msg_control
= &msg_control
;
2473 msg
.msg_controllen
= sizeof(msg_control
);
2475 #ifdef MSG_CMSG_CLOEXEC
2476 flags
|= MSG_CMSG_CLOEXEC
;
2478 ret
= recvmsg(s
->fd
, &msg
, flags
);
2479 if (ret
> 0 && s
->is_unix
) {
2480 unix_process_msgfd(chr
, &msg
);
2486 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2488 TCPCharDriver
*s
= chr
->opaque
;
2489 return qemu_recv(s
->fd
, buf
, len
, 0);
2493 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2495 TCPCharDriver
*s
= chr
->opaque
;
2496 return g_io_create_watch(s
->chan
, cond
);
2499 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2501 CharDriverState
*chr
= opaque
;
2502 TCPCharDriver
*s
= chr
->opaque
;
2503 uint8_t buf
[READ_BUF_LEN
];
2506 if (!s
->connected
|| s
->max_size
<= 0) {
2510 if (len
> s
->max_size
)
2512 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2514 /* connection closed */
2516 if (s
->listen_chan
) {
2517 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2519 g_source_remove(s
->tag
);
2521 g_io_channel_unref(s
->chan
);
2525 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2526 } else if (size
> 0) {
2527 if (s
->do_telnetopt
)
2528 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2530 qemu_chr_be_write(chr
, buf
, size
);
2537 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2539 return qemu_chr_open_fd(eventfd
, eventfd
);
2543 static void tcp_chr_connect(void *opaque
)
2545 CharDriverState
*chr
= opaque
;
2546 TCPCharDriver
*s
= chr
->opaque
;
2550 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2552 qemu_chr_be_generic_open(chr
);
2555 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2556 static void tcp_chr_telnet_init(int fd
)
2559 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2560 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2561 send(fd
, (char *)buf
, 3, 0);
2562 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2563 send(fd
, (char *)buf
, 3, 0);
2564 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2565 send(fd
, (char *)buf
, 3, 0);
2566 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2567 send(fd
, (char *)buf
, 3, 0);
2570 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2572 TCPCharDriver
*s
= chr
->opaque
;
2576 socket_set_nonblock(fd
);
2578 socket_set_nodelay(fd
);
2580 s
->chan
= io_channel_from_socket(fd
);
2581 g_source_remove(s
->listen_tag
);
2583 tcp_chr_connect(chr
);
2588 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2590 CharDriverState
*chr
= opaque
;
2591 TCPCharDriver
*s
= chr
->opaque
;
2592 struct sockaddr_in saddr
;
2594 struct sockaddr_un uaddr
;
2596 struct sockaddr
*addr
;
2603 len
= sizeof(uaddr
);
2604 addr
= (struct sockaddr
*)&uaddr
;
2608 len
= sizeof(saddr
);
2609 addr
= (struct sockaddr
*)&saddr
;
2611 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2612 if (fd
< 0 && errno
!= EINTR
) {
2614 } else if (fd
>= 0) {
2615 if (s
->do_telnetopt
)
2616 tcp_chr_telnet_init(fd
);
2620 if (tcp_chr_add_client(chr
, fd
) < 0)
2626 static void tcp_chr_close(CharDriverState
*chr
)
2628 TCPCharDriver
*s
= chr
->opaque
;
2631 g_source_remove(s
->tag
);
2634 g_io_channel_unref(s
->chan
);
2638 if (s
->listen_fd
>= 0) {
2639 if (s
->listen_tag
) {
2640 g_source_remove(s
->listen_tag
);
2642 if (s
->listen_chan
) {
2643 g_io_channel_unref(s
->listen_chan
);
2645 closesocket(s
->listen_fd
);
2648 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2651 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2652 bool is_listen
, bool is_telnet
,
2653 bool is_waitconnect
,
2656 CharDriverState
*chr
= NULL
;
2657 TCPCharDriver
*s
= NULL
;
2658 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2659 const char *left
= "", *right
= "";
2660 struct sockaddr_storage ss
;
2661 socklen_t ss_len
= sizeof(ss
);
2663 memset(&ss
, 0, ss_len
);
2664 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2665 error_setg(errp
, "getsockname: %s", strerror(errno
));
2669 chr
= g_malloc0(sizeof(CharDriverState
));
2670 s
= g_malloc0(sizeof(TCPCharDriver
));
2677 chr
->filename
= g_malloc(256);
2678 switch (ss
.ss_family
) {
2682 snprintf(chr
->filename
, 256, "unix:%s%s",
2683 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2684 is_listen
? ",server" : "");
2692 s
->do_nodelay
= do_nodelay
;
2693 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2694 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2695 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2696 is_telnet
? "telnet" : "tcp",
2697 left
, host
, right
, serv
,
2698 is_listen
? ",server" : "");
2703 chr
->chr_write
= tcp_chr_write
;
2704 chr
->chr_close
= tcp_chr_close
;
2705 chr
->get_msgfd
= tcp_get_msgfd
;
2706 chr
->chr_add_client
= tcp_chr_add_client
;
2707 chr
->chr_add_watch
= tcp_chr_add_watch
;
2711 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2712 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2714 s
->do_telnetopt
= 1;
2719 socket_set_nodelay(fd
);
2720 s
->chan
= io_channel_from_socket(s
->fd
);
2721 tcp_chr_connect(chr
);
2724 if (is_listen
&& is_waitconnect
) {
2725 printf("QEMU waiting for connection on: %s\n",
2727 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2728 socket_set_nonblock(s
->listen_fd
);
2733 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2735 CharDriverState
*chr
= NULL
;
2736 Error
*local_err
= NULL
;
2744 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2745 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2746 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2747 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2748 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2754 fd
= unix_listen_opts(opts
, &local_err
);
2756 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2760 fd
= inet_listen_opts(opts
, 0, &local_err
);
2762 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2769 if (!is_waitconnect
)
2770 socket_set_nonblock(fd
);
2772 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2773 is_waitconnect
, &local_err
);
2774 if (error_is_set(&local_err
)) {
2782 qerror_report_err(local_err
);
2783 error_free(local_err
);
2789 g_free(chr
->opaque
);
2795 /***********************************************************/
2796 /* Memory chardev */
2799 size_t outbuf_capacity
;
2803 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2805 MemoryDriver
*d
= chr
->opaque
;
2807 /* TODO: the QString implementation has the same code, we should
2808 * introduce a generic way to do this in cutils.c */
2809 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2811 d
->outbuf_capacity
+= len
;
2812 d
->outbuf_capacity
*= 2;
2813 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2816 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2817 d
->outbuf_size
+= len
;
2822 void qemu_chr_init_mem(CharDriverState
*chr
)
2826 d
= g_malloc(sizeof(*d
));
2828 d
->outbuf_capacity
= 4096;
2829 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2831 memset(chr
, 0, sizeof(*chr
));
2833 chr
->chr_write
= mem_chr_write
;
2836 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2838 MemoryDriver
*d
= chr
->opaque
;
2839 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2842 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2843 void qemu_chr_close_mem(CharDriverState
*chr
)
2845 MemoryDriver
*d
= chr
->opaque
;
2848 g_free(chr
->opaque
);
2850 chr
->chr_write
= NULL
;
2853 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2855 const MemoryDriver
*d
= chr
->opaque
;
2856 return d
->outbuf_size
;
2859 /*********************************************************/
2860 /* Ring buffer chardev */
2867 } RingBufCharDriver
;
2869 static size_t ringbuf_count(const CharDriverState
*chr
)
2871 const RingBufCharDriver
*d
= chr
->opaque
;
2873 return d
->prod
- d
->cons
;
2876 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2878 RingBufCharDriver
*d
= chr
->opaque
;
2881 if (!buf
|| (len
< 0)) {
2885 for (i
= 0; i
< len
; i
++ ) {
2886 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2887 if (d
->prod
- d
->cons
> d
->size
) {
2888 d
->cons
= d
->prod
- d
->size
;
2895 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2897 RingBufCharDriver
*d
= chr
->opaque
;
2900 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2901 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2907 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2909 RingBufCharDriver
*d
= chr
->opaque
;
2916 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2919 CharDriverState
*chr
;
2920 RingBufCharDriver
*d
;
2922 chr
= g_malloc0(sizeof(CharDriverState
));
2923 d
= g_malloc(sizeof(*d
));
2925 d
->size
= opts
->has_size
? opts
->size
: 65536;
2927 /* The size must be power of 2 */
2928 if (d
->size
& (d
->size
- 1)) {
2929 error_setg(errp
, "size of ringbuf chardev must be power of two");
2935 d
->cbuf
= g_malloc0(d
->size
);
2938 chr
->chr_write
= ringbuf_chr_write
;
2939 chr
->chr_close
= ringbuf_chr_close
;
2949 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2951 return chr
->chr_write
== ringbuf_chr_write
;
2954 void qmp_ringbuf_write(const char *device
, const char *data
,
2955 bool has_format
, enum DataFormat format
,
2958 CharDriverState
*chr
;
2959 const uint8_t *write_data
;
2963 chr
= qemu_chr_find(device
);
2965 error_setg(errp
, "Device '%s' not found", device
);
2969 if (!chr_is_ringbuf(chr
)) {
2970 error_setg(errp
,"%s is not a ringbuf device", device
);
2974 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2975 write_data
= g_base64_decode(data
, &write_count
);
2977 write_data
= (uint8_t *)data
;
2978 write_count
= strlen(data
);
2981 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2983 if (write_data
!= (uint8_t *)data
) {
2984 g_free((void *)write_data
);
2988 error_setg(errp
, "Failed to write to device %s", device
);
2993 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2994 bool has_format
, enum DataFormat format
,
2997 CharDriverState
*chr
;
3002 chr
= qemu_chr_find(device
);
3004 error_setg(errp
, "Device '%s' not found", device
);
3008 if (!chr_is_ringbuf(chr
)) {
3009 error_setg(errp
,"%s is not a ringbuf device", device
);
3014 error_setg(errp
, "size must be greater than zero");
3018 count
= ringbuf_count(chr
);
3019 size
= size
> count
? count
: size
;
3020 read_data
= g_malloc(size
+ 1);
3022 ringbuf_chr_read(chr
, read_data
, size
);
3024 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3025 data
= g_base64_encode(read_data
, size
);
3029 * FIXME should read only complete, valid UTF-8 characters up
3030 * to @size bytes. Invalid sequences should be replaced by a
3031 * suitable replacement character. Except when (and only
3032 * when) ring buffer lost characters since last read, initial
3033 * continuation characters should be dropped.
3035 read_data
[size
] = 0;
3036 data
= (char *)read_data
;
3042 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3044 char host
[65], port
[33], width
[8], height
[8];
3048 Error
*local_err
= NULL
;
3050 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3051 if (error_is_set(&local_err
)) {
3052 qerror_report_err(local_err
);
3053 error_free(local_err
);
3057 if (strstart(filename
, "mon:", &p
)) {
3059 qemu_opt_set(opts
, "mux", "on");
3062 if (strcmp(filename
, "null") == 0 ||
3063 strcmp(filename
, "pty") == 0 ||
3064 strcmp(filename
, "msmouse") == 0 ||
3065 strcmp(filename
, "braille") == 0 ||
3066 strcmp(filename
, "stdio") == 0) {
3067 qemu_opt_set(opts
, "backend", filename
);
3070 if (strstart(filename
, "vc", &p
)) {
3071 qemu_opt_set(opts
, "backend", "vc");
3073 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3075 qemu_opt_set(opts
, "width", width
);
3076 qemu_opt_set(opts
, "height", height
);
3077 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3079 qemu_opt_set(opts
, "cols", width
);
3080 qemu_opt_set(opts
, "rows", height
);
3087 if (strcmp(filename
, "con:") == 0) {
3088 qemu_opt_set(opts
, "backend", "console");
3091 if (strstart(filename
, "COM", NULL
)) {
3092 qemu_opt_set(opts
, "backend", "serial");
3093 qemu_opt_set(opts
, "path", filename
);
3096 if (strstart(filename
, "file:", &p
)) {
3097 qemu_opt_set(opts
, "backend", "file");
3098 qemu_opt_set(opts
, "path", p
);
3101 if (strstart(filename
, "pipe:", &p
)) {
3102 qemu_opt_set(opts
, "backend", "pipe");
3103 qemu_opt_set(opts
, "path", p
);
3106 if (strstart(filename
, "tcp:", &p
) ||
3107 strstart(filename
, "telnet:", &p
)) {
3108 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3110 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3113 qemu_opt_set(opts
, "backend", "socket");
3114 qemu_opt_set(opts
, "host", host
);
3115 qemu_opt_set(opts
, "port", port
);
3116 if (p
[pos
] == ',') {
3117 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3120 if (strstart(filename
, "telnet:", &p
))
3121 qemu_opt_set(opts
, "telnet", "on");
3124 if (strstart(filename
, "udp:", &p
)) {
3125 qemu_opt_set(opts
, "backend", "udp");
3126 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3128 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3132 qemu_opt_set(opts
, "host", host
);
3133 qemu_opt_set(opts
, "port", port
);
3134 if (p
[pos
] == '@') {
3136 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3138 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3142 qemu_opt_set(opts
, "localaddr", host
);
3143 qemu_opt_set(opts
, "localport", port
);
3147 if (strstart(filename
, "unix:", &p
)) {
3148 qemu_opt_set(opts
, "backend", "socket");
3149 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3153 if (strstart(filename
, "/dev/parport", NULL
) ||
3154 strstart(filename
, "/dev/ppi", NULL
)) {
3155 qemu_opt_set(opts
, "backend", "parport");
3156 qemu_opt_set(opts
, "path", filename
);
3159 if (strstart(filename
, "/dev/", NULL
)) {
3160 qemu_opt_set(opts
, "backend", "tty");
3161 qemu_opt_set(opts
, "path", filename
);
3166 qemu_opts_del(opts
);
3170 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3173 const char *path
= qemu_opt_get(opts
, "path");
3176 error_setg(errp
, "chardev: file: no filename given");
3179 backend
->file
= g_new0(ChardevFile
, 1);
3180 backend
->file
->out
= g_strdup(path
);
3183 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3186 backend
->stdio
= g_new0(ChardevStdio
, 1);
3187 backend
->stdio
->has_signal
= true;
3188 backend
->stdio
->signal
=
3189 qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
);
3192 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3195 const char *device
= qemu_opt_get(opts
, "path");
3197 if (device
== NULL
) {
3198 error_setg(errp
, "chardev: serial/tty: no device path given");
3201 backend
->serial
= g_new0(ChardevHostdev
, 1);
3202 backend
->serial
->device
= g_strdup(device
);
3205 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3208 const char *device
= qemu_opt_get(opts
, "path");
3210 if (device
== NULL
) {
3211 error_setg(errp
, "chardev: parallel: no device path given");
3214 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3215 backend
->parallel
->device
= g_strdup(device
);
3218 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3221 const char *device
= qemu_opt_get(opts
, "path");
3223 if (device
== NULL
) {
3224 error_setg(errp
, "chardev: pipe: no device path given");
3227 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3228 backend
->pipe
->device
= g_strdup(device
);
3231 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3236 backend
->memory
= g_new0(ChardevRingbuf
, 1);
3238 val
= qemu_opt_get_number(opts
, "size", 0);
3240 backend
->memory
->has_size
= true;
3241 backend
->memory
->size
= val
;
3245 typedef struct CharDriver
{
3248 CharDriverState
*(*open
)(QemuOpts
*opts
);
3249 /* new, qapi-based */
3251 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3254 static GSList
*backends
;
3256 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3260 s
= g_malloc0(sizeof(*s
));
3261 s
->name
= g_strdup(name
);
3264 backends
= g_slist_append(backends
, s
);
3267 void register_char_driver_qapi(const char *name
, int kind
,
3268 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3272 s
= g_malloc0(sizeof(*s
));
3273 s
->name
= g_strdup(name
);
3277 backends
= g_slist_append(backends
, s
);
3280 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3281 void (*init
)(struct CharDriverState
*s
),
3285 CharDriverState
*chr
;
3288 if (qemu_opts_id(opts
) == NULL
) {
3289 error_setg(errp
, "chardev: no id specified");
3293 if (qemu_opt_get(opts
, "backend") == NULL
) {
3294 error_setg(errp
, "chardev: \"%s\" missing backend",
3295 qemu_opts_id(opts
));
3298 for (i
= backends
; i
; i
= i
->next
) {
3301 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3306 error_setg(errp
, "chardev: backend \"%s\" not found",
3307 qemu_opt_get(opts
, "backend"));
3312 /* using new, qapi init */
3313 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3314 ChardevReturn
*ret
= NULL
;
3315 const char *id
= qemu_opts_id(opts
);
3316 const char *bid
= NULL
;
3318 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3319 bid
= g_strdup_printf("%s-base", id
);
3323 backend
->kind
= cd
->kind
;
3325 cd
->parse(opts
, backend
, errp
);
3326 if (error_is_set(errp
)) {
3330 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3331 if (error_is_set(errp
)) {
3336 qapi_free_ChardevBackend(backend
);
3337 qapi_free_ChardevReturn(ret
);
3338 backend
= g_new0(ChardevBackend
, 1);
3339 backend
->mux
= g_new0(ChardevMux
, 1);
3340 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3341 backend
->mux
->chardev
= g_strdup(bid
);
3342 ret
= qmp_chardev_add(id
, backend
, errp
);
3343 if (error_is_set(errp
)) {
3348 chr
= qemu_chr_find(id
);
3351 qapi_free_ChardevBackend(backend
);
3352 qapi_free_ChardevReturn(ret
);
3356 chr
= cd
->open(opts
);
3358 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3359 qemu_opt_get(opts
, "backend"));
3364 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3366 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3368 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3369 CharDriverState
*base
= chr
;
3370 int len
= strlen(qemu_opts_id(opts
)) + 6;
3371 base
->label
= g_malloc(len
);
3372 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3373 chr
= qemu_chr_open_mux(base
);
3374 chr
->filename
= base
->filename
;
3375 chr
->avail_connections
= MAX_MUX
;
3376 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3378 chr
->avail_connections
= 1;
3380 chr
->label
= g_strdup(qemu_opts_id(opts
));
3385 qemu_opts_del(opts
);
3389 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3392 CharDriverState
*chr
;
3396 if (strstart(filename
, "chardev:", &p
)) {
3397 return qemu_chr_find(p
);
3400 opts
= qemu_chr_parse_compat(label
, filename
);
3404 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3405 if (error_is_set(&err
)) {
3406 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3409 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3410 monitor_init(chr
, MONITOR_USE_READLINE
);
3415 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3417 if (chr
->chr_set_echo
) {
3418 chr
->chr_set_echo(chr
, echo
);
3422 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3424 if (chr
->fe_open
== fe_open
) {
3427 chr
->fe_open
= fe_open
;
3428 if (fe_open
&& chr
->chr_guest_open
) {
3429 chr
->chr_guest_open(chr
);
3431 if (!fe_open
&& chr
->chr_guest_close
) {
3432 chr
->chr_guest_close(chr
);
3436 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3437 GIOFunc func
, void *user_data
)
3442 if (s
->chr_add_watch
== NULL
) {
3446 src
= s
->chr_add_watch(s
, cond
);
3447 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3448 tag
= g_source_attach(src
, NULL
);
3449 g_source_unref(src
);
3454 void qemu_chr_delete(CharDriverState
*chr
)
3456 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3457 if (chr
->chr_close
) {
3458 chr
->chr_close(chr
);
3460 g_free(chr
->filename
);
3463 qemu_opts_del(chr
->opts
);
3468 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3470 ChardevInfoList
*chr_list
= NULL
;
3471 CharDriverState
*chr
;
3473 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3474 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3475 info
->value
= g_malloc0(sizeof(*info
->value
));
3476 info
->value
->label
= g_strdup(chr
->label
);
3477 info
->value
->filename
= g_strdup(chr
->filename
);
3479 info
->next
= chr_list
;
3486 CharDriverState
*qemu_chr_find(const char *name
)
3488 CharDriverState
*chr
;
3490 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3491 if (strcmp(chr
->label
, name
) != 0)
3498 /* Get a character (serial) device interface. */
3499 CharDriverState
*qemu_char_get_next_serial(void)
3501 static int next_serial
;
3503 /* FIXME: This function needs to go away: use chardev properties! */
3504 return serial_hds
[next_serial
++];
3507 QemuOptsList qemu_chardev_opts
= {
3509 .implied_opt_name
= "backend",
3510 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3514 .type
= QEMU_OPT_STRING
,
3517 .type
= QEMU_OPT_STRING
,
3520 .type
= QEMU_OPT_STRING
,
3523 .type
= QEMU_OPT_STRING
,
3525 .name
= "localaddr",
3526 .type
= QEMU_OPT_STRING
,
3528 .name
= "localport",
3529 .type
= QEMU_OPT_STRING
,
3532 .type
= QEMU_OPT_NUMBER
,
3535 .type
= QEMU_OPT_BOOL
,
3538 .type
= QEMU_OPT_BOOL
,
3541 .type
= QEMU_OPT_BOOL
,
3544 .type
= QEMU_OPT_BOOL
,
3547 .type
= QEMU_OPT_BOOL
,
3550 .type
= QEMU_OPT_BOOL
,
3553 .type
= QEMU_OPT_NUMBER
,
3556 .type
= QEMU_OPT_NUMBER
,
3559 .type
= QEMU_OPT_NUMBER
,
3562 .type
= QEMU_OPT_NUMBER
,
3565 .type
= QEMU_OPT_BOOL
,
3568 .type
= QEMU_OPT_BOOL
,
3571 .type
= QEMU_OPT_STRING
,
3574 .type
= QEMU_OPT_NUMBER
,
3577 .type
= QEMU_OPT_SIZE
,
3579 { /* end of list */ }
3585 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3590 error_setg(errp
, "input file not supported");
3594 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3595 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3596 if (out
== INVALID_HANDLE_VALUE
) {
3597 error_setg(errp
, "open %s failed", file
->out
);
3600 return qemu_chr_open_win_file(out
);
3603 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3606 return qemu_chr_open_win_path(serial
->device
);
3609 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3612 error_setg(errp
, "character device backend type 'parallel' not supported");
3618 static int qmp_chardev_open_file_source(char *src
, int flags
,
3623 TFR(fd
= qemu_open(src
, flags
, 0666));
3625 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3630 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3632 int flags
, in
= -1, out
= -1;
3634 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3635 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3636 if (error_is_set(errp
)) {
3642 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3643 if (error_is_set(errp
)) {
3649 return qemu_chr_open_fd(in
, out
);
3652 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3655 #ifdef HAVE_CHARDEV_TTY
3658 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3659 if (error_is_set(errp
)) {
3662 socket_set_nonblock(fd
);
3663 return qemu_chr_open_tty_fd(fd
);
3665 error_setg(errp
, "character device backend type 'serial' not supported");
3670 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3673 #ifdef HAVE_CHARDEV_PARPORT
3676 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3677 if (error_is_set(errp
)) {
3680 return qemu_chr_open_pp_fd(fd
);
3682 error_setg(errp
, "character device backend type 'parallel' not supported");
3689 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3692 SocketAddress
*addr
= sock
->addr
;
3693 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3694 bool is_listen
= sock
->has_server
? sock
->server
: true;
3695 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3696 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3700 fd
= socket_listen(addr
, errp
);
3702 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3704 if (error_is_set(errp
)) {
3707 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3708 is_telnet
, is_waitconnect
, errp
);
3711 static CharDriverState
*qmp_chardev_open_dgram(ChardevDgram
*dgram
,
3716 fd
= socket_dgram(dgram
->remote
, dgram
->local
, errp
);
3717 if (error_is_set(errp
)) {
3720 return qemu_chr_open_udp_fd(fd
);
3723 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3726 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3727 CharDriverState
*base
, *chr
= NULL
;
3729 chr
= qemu_chr_find(id
);
3731 error_setg(errp
, "Chardev '%s' already exists", id
);
3736 switch (backend
->kind
) {
3737 case CHARDEV_BACKEND_KIND_FILE
:
3738 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3740 case CHARDEV_BACKEND_KIND_SERIAL
:
3741 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3743 case CHARDEV_BACKEND_KIND_PARALLEL
:
3744 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3746 case CHARDEV_BACKEND_KIND_PIPE
:
3747 chr
= qemu_chr_open_pipe(backend
->pipe
);
3749 case CHARDEV_BACKEND_KIND_SOCKET
:
3750 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3752 case CHARDEV_BACKEND_KIND_DGRAM
:
3753 chr
= qmp_chardev_open_dgram(backend
->dgram
, errp
);
3755 #ifdef HAVE_CHARDEV_TTY
3756 case CHARDEV_BACKEND_KIND_PTY
:
3757 chr
= qemu_chr_open_pty(id
, ret
);
3760 case CHARDEV_BACKEND_KIND_NULL
:
3761 chr
= qemu_chr_open_null();
3763 case CHARDEV_BACKEND_KIND_MUX
:
3764 base
= qemu_chr_find(backend
->mux
->chardev
);
3766 error_setg(errp
, "mux: base chardev %s not found",
3767 backend
->mux
->chardev
);
3770 chr
= qemu_chr_open_mux(base
);
3772 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3773 chr
= qemu_chr_open_msmouse();
3775 #ifdef CONFIG_BRLAPI
3776 case CHARDEV_BACKEND_KIND_BRAILLE
:
3777 chr
= chr_baum_init();
3780 case CHARDEV_BACKEND_KIND_STDIO
:
3781 chr
= qemu_chr_open_stdio(backend
->stdio
);
3784 case CHARDEV_BACKEND_KIND_CONSOLE
:
3785 chr
= qemu_chr_open_win_con();
3789 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3790 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3792 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3793 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3796 case CHARDEV_BACKEND_KIND_VC
:
3797 chr
= vc_init(backend
->vc
);
3799 case CHARDEV_BACKEND_KIND_MEMORY
:
3800 chr
= qemu_chr_open_ringbuf(backend
->memory
, errp
);
3803 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3807 if (chr
== NULL
&& !error_is_set(errp
)) {
3808 error_setg(errp
, "Failed to create chardev");
3811 chr
->label
= g_strdup(id
);
3812 chr
->avail_connections
=
3813 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3814 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3822 void qmp_chardev_remove(const char *id
, Error
**errp
)
3824 CharDriverState
*chr
;
3826 chr
= qemu_chr_find(id
);
3828 error_setg(errp
, "Chardev '%s' not found", id
);
3831 if (chr
->chr_can_read
|| chr
->chr_read
||
3832 chr
->chr_event
|| chr
->handler_opaque
) {
3833 error_setg(errp
, "Chardev '%s' is busy", id
);
3836 qemu_chr_delete(chr
);
3839 static void register_types(void)
3841 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3842 register_char_driver("socket", qemu_chr_open_socket
);
3843 register_char_driver("udp", qemu_chr_open_udp
);
3844 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3845 qemu_chr_parse_ringbuf
);
3846 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3847 qemu_chr_parse_file_out
);
3848 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3849 qemu_chr_parse_stdio
);
3850 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3851 qemu_chr_parse_serial
);
3852 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3853 qemu_chr_parse_serial
);
3854 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3855 qemu_chr_parse_parallel
);
3856 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3857 qemu_chr_parse_parallel
);
3858 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3859 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3860 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3861 qemu_chr_parse_pipe
);
3864 type_init(register_types
);