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_generic_open_bh(gpointer opaque
)
125 CharDriverState
*s
= opaque
;
126 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
131 void qemu_chr_generic_open(CharDriverState
*s
)
133 if (s
->idle_tag
== 0) {
134 s
->idle_tag
= g_idle_add(qemu_chr_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_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
147 return s
->chr_ioctl(s
, cmd
, arg
);
150 int qemu_chr_be_can_write(CharDriverState
*s
)
152 if (!s
->chr_can_read
)
154 return s
->chr_can_read(s
->handler_opaque
);
157 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
160 s
->chr_read(s
->handler_opaque
, buf
, len
);
164 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
166 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
169 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
171 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
174 void qemu_chr_accept_input(CharDriverState
*s
)
176 if (s
->chr_accept_input
)
177 s
->chr_accept_input(s
);
181 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
183 char buf
[READ_BUF_LEN
];
186 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
187 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
191 void qemu_chr_add_handlers(CharDriverState
*s
,
192 IOCanReadHandler
*fd_can_read
,
193 IOReadHandler
*fd_read
,
194 IOEventHandler
*fd_event
,
197 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
198 /* chr driver being released. */
199 ++s
->avail_connections
;
201 s
->chr_can_read
= fd_can_read
;
202 s
->chr_read
= fd_read
;
203 s
->chr_event
= fd_event
;
204 s
->handler_opaque
= opaque
;
205 if (s
->chr_update_read_handler
)
206 s
->chr_update_read_handler(s
);
208 /* We're connecting to an already opened device, so let's make sure we
209 also get the open event */
211 qemu_chr_generic_open(s
);
215 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
220 static CharDriverState
*qemu_chr_open_null(void)
222 CharDriverState
*chr
;
224 chr
= g_malloc0(sizeof(CharDriverState
));
225 chr
->chr_write
= null_chr_write
;
229 /* MUX driver for serial I/O splitting */
231 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
232 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
234 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
235 IOReadHandler
*chr_read
[MAX_MUX
];
236 IOEventHandler
*chr_event
[MAX_MUX
];
237 void *ext_opaque
[MAX_MUX
];
238 CharDriverState
*drv
;
243 /* Intermediate input buffer allows to catch escape sequences even if the
244 currently active device is not accepting any input - but only until it
246 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
251 int64_t timestamps_start
;
255 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
257 MuxDriver
*d
= chr
->opaque
;
259 if (!d
->timestamps
) {
260 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
265 for (i
= 0; i
< len
; i
++) {
271 ti
= qemu_get_clock_ms(rt_clock
);
272 if (d
->timestamps_start
== -1)
273 d
->timestamps_start
= ti
;
274 ti
-= d
->timestamps_start
;
276 snprintf(buf1
, sizeof(buf1
),
277 "[%02d:%02d:%02d.%03d] ",
282 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
285 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
286 if (buf
[i
] == '\n') {
294 static const char * const mux_help
[] = {
295 "% h print this help\n\r",
296 "% x exit emulator\n\r",
297 "% s save disk data back to file (if -snapshot)\n\r",
298 "% t toggle console timestamps\n\r"
299 "% b send break (magic sysrq)\n\r",
300 "% c switch between console and monitor\n\r",
305 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
306 static void mux_print_help(CharDriverState
*chr
)
309 char ebuf
[15] = "Escape-Char";
310 char cbuf
[50] = "\n\r";
312 if (term_escape_char
> 0 && term_escape_char
< 26) {
313 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
314 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
316 snprintf(cbuf
, sizeof(cbuf
),
317 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
320 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
321 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
322 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
323 if (mux_help
[i
][j
] == '%')
324 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
326 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
331 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
333 if (d
->chr_event
[mux_nr
])
334 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
337 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
339 if (d
->term_got_escape
) {
340 d
->term_got_escape
= 0;
341 if (ch
== term_escape_char
)
350 const char *term
= "QEMU: Terminated\n\r";
351 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
359 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
362 /* Switch to the next registered device */
363 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
365 if (d
->focus
>= d
->mux_cnt
)
367 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
370 d
->timestamps
= !d
->timestamps
;
371 d
->timestamps_start
= -1;
375 } else if (ch
== term_escape_char
) {
376 d
->term_got_escape
= 1;
384 static void mux_chr_accept_input(CharDriverState
*chr
)
386 MuxDriver
*d
= chr
->opaque
;
389 while (d
->prod
[m
] != d
->cons
[m
] &&
390 d
->chr_can_read
[m
] &&
391 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
392 d
->chr_read
[m
](d
->ext_opaque
[m
],
393 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
397 static int mux_chr_can_read(void *opaque
)
399 CharDriverState
*chr
= opaque
;
400 MuxDriver
*d
= chr
->opaque
;
403 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
405 if (d
->chr_can_read
[m
])
406 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
410 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
412 CharDriverState
*chr
= opaque
;
413 MuxDriver
*d
= chr
->opaque
;
417 mux_chr_accept_input (opaque
);
419 for(i
= 0; i
< size
; i
++)
420 if (mux_proc_byte(chr
, d
, buf
[i
])) {
421 if (d
->prod
[m
] == d
->cons
[m
] &&
422 d
->chr_can_read
[m
] &&
423 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
424 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
426 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
430 static void mux_chr_event(void *opaque
, int event
)
432 CharDriverState
*chr
= opaque
;
433 MuxDriver
*d
= chr
->opaque
;
436 /* Send the event to all registered listeners */
437 for (i
= 0; i
< d
->mux_cnt
; i
++)
438 mux_chr_send_event(d
, i
, event
);
441 static void mux_chr_update_read_handler(CharDriverState
*chr
)
443 MuxDriver
*d
= chr
->opaque
;
445 if (d
->mux_cnt
>= MAX_MUX
) {
446 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
449 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
450 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
451 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
452 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
453 /* Fix up the real driver with mux routines */
454 if (d
->mux_cnt
== 0) {
455 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
458 if (d
->focus
!= -1) {
459 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
461 d
->focus
= d
->mux_cnt
;
463 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
466 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
468 CharDriverState
*chr
;
471 chr
= g_malloc0(sizeof(CharDriverState
));
472 d
= g_malloc0(sizeof(MuxDriver
));
477 chr
->chr_write
= mux_chr_write
;
478 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
479 chr
->chr_accept_input
= mux_chr_accept_input
;
480 /* Frontend guest-open / -close notification is not support with muxes */
481 chr
->chr_guest_open
= NULL
;
482 chr
->chr_guest_close
= NULL
;
484 /* Muxes are always open on creation */
485 qemu_chr_generic_open(chr
);
492 int send_all(int fd
, const void *buf
, int len1
)
498 ret
= send(fd
, buf
, len
, 0);
500 errno
= WSAGetLastError();
501 if (errno
!= WSAEWOULDBLOCK
) {
504 } else if (ret
== 0) {
516 int send_all(int fd
, const void *_buf
, int len1
)
519 const uint8_t *buf
= _buf
;
523 ret
= write(fd
, buf
, len
);
525 if (errno
!= EINTR
&& errno
!= EAGAIN
)
527 } else if (ret
== 0) {
537 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
543 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
545 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
562 typedef struct IOWatchPoll
567 IOCanReadHandler
*fd_can_read
;
570 QTAILQ_ENTRY(IOWatchPoll
) node
;
573 static QTAILQ_HEAD(, IOWatchPoll
) io_watch_poll_list
=
574 QTAILQ_HEAD_INITIALIZER(io_watch_poll_list
);
576 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
580 QTAILQ_FOREACH(i
, &io_watch_poll_list
, node
) {
581 if (i
->src
== source
) {
589 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
591 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
593 iwp
->max_size
= iwp
->fd_can_read(iwp
->opaque
);
594 if (iwp
->max_size
== 0) {
598 return g_io_watch_funcs
.prepare(source
, timeout_
);
601 static gboolean
io_watch_poll_check(GSource
*source
)
603 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
605 if (iwp
->max_size
== 0) {
609 return g_io_watch_funcs
.check(source
);
612 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
615 return g_io_watch_funcs
.dispatch(source
, callback
, user_data
);
618 static void io_watch_poll_finalize(GSource
*source
)
620 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
621 QTAILQ_REMOVE(&io_watch_poll_list
, iwp
, node
);
622 g_io_watch_funcs
.finalize(source
);
625 static GSourceFuncs io_watch_poll_funcs
= {
626 .prepare
= io_watch_poll_prepare
,
627 .check
= io_watch_poll_check
,
628 .dispatch
= io_watch_poll_dispatch
,
629 .finalize
= io_watch_poll_finalize
,
632 /* Can only be used for read */
633 static guint
io_add_watch_poll(GIOChannel
*channel
,
634 IOCanReadHandler
*fd_can_read
,
642 src
= g_io_create_watch(channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
643 g_source_set_funcs(src
, &io_watch_poll_funcs
);
644 g_source_set_callback(src
, (GSourceFunc
)fd_read
, user_data
, NULL
);
645 tag
= g_source_attach(src
, NULL
);
648 iwp
= g_malloc0(sizeof(*iwp
));
651 iwp
->fd_can_read
= fd_can_read
;
652 iwp
->opaque
= user_data
;
654 QTAILQ_INSERT_HEAD(&io_watch_poll_list
, iwp
, node
);
660 static GIOChannel
*io_channel_from_fd(int fd
)
668 chan
= g_io_channel_unix_new(fd
);
670 g_io_channel_set_encoding(chan
, NULL
, NULL
);
671 g_io_channel_set_buffered(chan
, FALSE
);
677 static GIOChannel
*io_channel_from_socket(int fd
)
686 chan
= g_io_channel_win32_new_socket(fd
);
688 chan
= g_io_channel_unix_new(fd
);
691 g_io_channel_set_encoding(chan
, NULL
, NULL
);
692 g_io_channel_set_buffered(chan
, FALSE
);
697 static int io_channel_send_all(GIOChannel
*fd
, const void *_buf
, int len1
)
702 const uint8_t *buf
= _buf
;
706 status
= g_io_channel_write_chars(fd
, (const gchar
*)buf
, len
,
707 &bytes_written
, NULL
);
708 if (status
!= G_IO_STATUS_NORMAL
) {
709 if (status
== G_IO_STATUS_AGAIN
) {
716 } else if (status
== G_IO_STATUS_EOF
) {
719 buf
+= bytes_written
;
720 len
-= bytes_written
;
728 typedef struct FDCharDriver
{
729 CharDriverState
*chr
;
730 GIOChannel
*fd_in
, *fd_out
;
733 QTAILQ_ENTRY(FDCharDriver
) node
;
736 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
738 FDCharDriver
*s
= chr
->opaque
;
740 return io_channel_send_all(s
->fd_out
, buf
, len
);
743 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
745 CharDriverState
*chr
= opaque
;
746 FDCharDriver
*s
= chr
->opaque
;
748 uint8_t buf
[READ_BUF_LEN
];
753 if (len
> s
->max_size
) {
760 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
761 len
, &bytes_read
, NULL
);
762 if (status
== G_IO_STATUS_EOF
) {
763 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
766 if (status
== G_IO_STATUS_NORMAL
) {
767 qemu_chr_be_write(chr
, buf
, bytes_read
);
773 static int fd_chr_read_poll(void *opaque
)
775 CharDriverState
*chr
= opaque
;
776 FDCharDriver
*s
= chr
->opaque
;
778 s
->max_size
= qemu_chr_be_can_write(chr
);
782 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
784 FDCharDriver
*s
= chr
->opaque
;
785 return g_io_create_watch(s
->fd_out
, cond
);
788 static void fd_chr_update_read_handler(CharDriverState
*chr
)
790 FDCharDriver
*s
= chr
->opaque
;
793 g_source_remove(s
->fd_in_tag
);
797 s
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
, fd_chr_read
, chr
);
801 static void fd_chr_close(struct CharDriverState
*chr
)
803 FDCharDriver
*s
= chr
->opaque
;
806 g_source_remove(s
->fd_in_tag
);
811 g_io_channel_unref(s
->fd_in
);
814 g_io_channel_unref(s
->fd_out
);
818 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
821 /* open a character device to a unix fd */
822 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
824 CharDriverState
*chr
;
827 chr
= g_malloc0(sizeof(CharDriverState
));
828 s
= g_malloc0(sizeof(FDCharDriver
));
829 s
->fd_in
= io_channel_from_fd(fd_in
);
830 s
->fd_out
= io_channel_from_fd(fd_out
);
831 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
834 chr
->chr_add_watch
= fd_chr_add_watch
;
835 chr
->chr_write
= fd_chr_write
;
836 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
837 chr
->chr_close
= fd_chr_close
;
839 qemu_chr_generic_open(chr
);
844 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
847 char filename_in
[256], filename_out
[256];
848 const char *filename
= opts
->device
;
850 if (filename
== NULL
) {
851 fprintf(stderr
, "chardev: pipe: no filename given\n");
855 snprintf(filename_in
, 256, "%s.in", filename
);
856 snprintf(filename_out
, 256, "%s.out", filename
);
857 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
858 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
859 if (fd_in
< 0 || fd_out
< 0) {
864 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
869 return qemu_chr_open_fd(fd_in
, fd_out
);
872 /* init terminal so that we can grab keys */
873 static struct termios oldtty
;
874 static int old_fd0_flags
;
875 static bool stdio_allow_signal
;
877 static void term_exit(void)
879 tcsetattr (0, TCSANOW
, &oldtty
);
880 fcntl(0, F_SETFL
, old_fd0_flags
);
883 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
889 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
890 |INLCR
|IGNCR
|ICRNL
|IXON
);
891 tty
.c_oflag
|= OPOST
;
892 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
893 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
898 /* if graphical mode, we allow Ctrl-C handling */
899 if (!stdio_allow_signal
)
900 tty
.c_lflag
&= ~ISIG
;
902 tcsetattr (0, TCSANOW
, &tty
);
905 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
911 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
913 CharDriverState
*chr
;
915 if (is_daemonized()) {
916 error_report("cannot use stdio with -daemonize");
919 old_fd0_flags
= fcntl(0, F_GETFL
);
920 tcgetattr (0, &oldtty
);
921 fcntl(0, F_SETFL
, O_NONBLOCK
);
924 chr
= qemu_chr_open_fd(0, 1);
925 chr
->chr_close
= qemu_chr_close_stdio
;
926 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
927 stdio_allow_signal
= display_type
!= DT_NOGRAPHIC
;
928 if (opts
->has_signal
) {
929 stdio_allow_signal
= opts
->signal
;
931 qemu_chr_fe_set_echo(chr
, false);
937 /* Once Solaris has openpty(), this is going to be removed. */
938 static int openpty(int *amaster
, int *aslave
, char *name
,
939 struct termios
*termp
, struct winsize
*winp
)
942 int mfd
= -1, sfd
= -1;
944 *amaster
= *aslave
= -1;
946 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
950 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
953 if ((slave
= ptsname(mfd
)) == NULL
)
956 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
959 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
960 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
968 ioctl(sfd
, TIOCSWINSZ
, winp
);
979 static void cfmakeraw (struct termios
*termios_p
)
981 termios_p
->c_iflag
&=
982 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
983 termios_p
->c_oflag
&= ~OPOST
;
984 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
985 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
986 termios_p
->c_cflag
|= CS8
;
988 termios_p
->c_cc
[VMIN
] = 0;
989 termios_p
->c_cc
[VTIME
] = 0;
993 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
994 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
995 || defined(__GLIBC__)
997 #define HAVE_CHARDEV_TTY 1
1008 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1009 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1011 static gboolean
pty_chr_timer(gpointer opaque
)
1013 struct CharDriverState
*chr
= opaque
;
1014 PtyCharDriver
*s
= chr
->opaque
;
1020 /* If we arrive here without polling being cleared due
1021 * read returning -EIO, then we are (re-)connected */
1022 pty_chr_state(chr
, 1);
1027 pty_chr_update_read_handler(chr
);
1033 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1035 PtyCharDriver
*s
= chr
->opaque
;
1038 g_source_remove(s
->timer_tag
);
1043 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1045 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1049 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1051 PtyCharDriver
*s
= chr
->opaque
;
1053 if (!s
->connected
) {
1054 /* guest sends data, check for (re-)connect */
1055 pty_chr_update_read_handler(chr
);
1058 return io_channel_send_all(s
->fd
, buf
, len
);
1061 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1063 PtyCharDriver
*s
= chr
->opaque
;
1064 return g_io_create_watch(s
->fd
, cond
);
1067 static int pty_chr_read_poll(void *opaque
)
1069 CharDriverState
*chr
= opaque
;
1070 PtyCharDriver
*s
= chr
->opaque
;
1072 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1073 return s
->read_bytes
;
1076 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1078 CharDriverState
*chr
= opaque
;
1079 PtyCharDriver
*s
= chr
->opaque
;
1081 uint8_t buf
[READ_BUF_LEN
];
1085 if (len
> s
->read_bytes
)
1086 len
= s
->read_bytes
;
1089 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1090 if (status
!= G_IO_STATUS_NORMAL
) {
1091 pty_chr_state(chr
, 0);
1094 pty_chr_state(chr
, 1);
1095 qemu_chr_be_write(chr
, buf
, size
);
1100 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1102 PtyCharDriver
*s
= chr
->opaque
;
1105 g_source_remove(s
->fd_tag
);
1108 s
->fd_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
, pty_chr_read
, chr
);
1111 * Short timeout here: just need wait long enougth that qemu makes
1112 * it through the poll loop once. When reconnected we want a
1113 * short timeout so we notice it almost instantly. Otherwise
1114 * read() gives us -EIO instantly, making pty_chr_state() reset the
1115 * timeout to the normal (much longer) poll interval before the
1118 pty_chr_rearm_timer(chr
, 10);
1121 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1123 PtyCharDriver
*s
= chr
->opaque
;
1126 g_source_remove(s
->fd_tag
);
1130 /* (re-)connect poll interval for idle guests: once per second.
1131 * We check more frequently in case the guests sends data to
1132 * the virtual device linked to our pty. */
1133 pty_chr_rearm_timer(chr
, 1000);
1136 qemu_chr_generic_open(chr
);
1142 static void pty_chr_close(struct CharDriverState
*chr
)
1144 PtyCharDriver
*s
= chr
->opaque
;
1148 g_source_remove(s
->fd_tag
);
1150 fd
= g_io_channel_unix_get_fd(s
->fd
);
1151 g_io_channel_unref(s
->fd
);
1154 g_source_remove(s
->timer_tag
);
1157 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1160 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1163 CharDriverState
*chr
;
1166 int master_fd
, slave_fd
;
1167 #if defined(__OpenBSD__) || defined(__DragonFly__)
1168 char pty_name
[PATH_MAX
];
1169 #define q_ptsname(x) pty_name
1171 char *pty_name
= NULL
;
1172 #define q_ptsname(x) ptsname(x)
1175 if (openpty(&master_fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
1179 /* Set raw attributes on the pty. */
1180 tcgetattr(slave_fd
, &tty
);
1182 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1185 chr
= g_malloc0(sizeof(CharDriverState
));
1187 chr
->filename
= g_strdup_printf("pty:%s", q_ptsname(master_fd
));
1188 ret
->pty
= g_strdup(q_ptsname(master_fd
));
1189 ret
->has_pty
= true;
1191 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1192 q_ptsname(master_fd
), id
);
1194 s
= g_malloc0(sizeof(PtyCharDriver
));
1196 chr
->chr_write
= pty_chr_write
;
1197 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1198 chr
->chr_close
= pty_chr_close
;
1199 chr
->chr_add_watch
= pty_chr_add_watch
;
1201 s
->fd
= io_channel_from_fd(master_fd
);
1207 static void tty_serial_init(int fd
, int speed
,
1208 int parity
, int data_bits
, int stop_bits
)
1214 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1215 speed
, parity
, data_bits
, stop_bits
);
1217 tcgetattr (fd
, &tty
);
1219 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1220 speed
= speed
* 10 / 11;
1237 /* Non-Posix values follow. They may be unsupported on some systems. */
1239 check_speed(115200);
1241 check_speed(230400);
1244 check_speed(460800);
1247 check_speed(500000);
1250 check_speed(576000);
1253 check_speed(921600);
1256 check_speed(1000000);
1259 check_speed(1152000);
1262 check_speed(1500000);
1265 check_speed(2000000);
1268 check_speed(2500000);
1271 check_speed(3000000);
1274 check_speed(3500000);
1277 check_speed(4000000);
1282 cfsetispeed(&tty
, spd
);
1283 cfsetospeed(&tty
, spd
);
1285 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1286 |INLCR
|IGNCR
|ICRNL
|IXON
);
1287 tty
.c_oflag
|= OPOST
;
1288 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1289 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1310 tty
.c_cflag
|= PARENB
;
1313 tty
.c_cflag
|= PARENB
| PARODD
;
1317 tty
.c_cflag
|= CSTOPB
;
1319 tcsetattr (fd
, TCSANOW
, &tty
);
1322 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1324 FDCharDriver
*s
= chr
->opaque
;
1327 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1329 QEMUSerialSetParams
*ssp
= arg
;
1330 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1331 ssp
->speed
, ssp
->parity
,
1332 ssp
->data_bits
, ssp
->stop_bits
);
1335 case CHR_IOCTL_SERIAL_SET_BREAK
:
1337 int enable
= *(int *)arg
;
1339 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1343 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1346 int *targ
= (int *)arg
;
1347 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1349 if (sarg
& TIOCM_CTS
)
1350 *targ
|= CHR_TIOCM_CTS
;
1351 if (sarg
& TIOCM_CAR
)
1352 *targ
|= CHR_TIOCM_CAR
;
1353 if (sarg
& TIOCM_DSR
)
1354 *targ
|= CHR_TIOCM_DSR
;
1355 if (sarg
& TIOCM_RI
)
1356 *targ
|= CHR_TIOCM_RI
;
1357 if (sarg
& TIOCM_DTR
)
1358 *targ
|= CHR_TIOCM_DTR
;
1359 if (sarg
& TIOCM_RTS
)
1360 *targ
|= CHR_TIOCM_RTS
;
1363 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1365 int sarg
= *(int *)arg
;
1367 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1368 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1369 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1370 if (sarg
& CHR_TIOCM_CTS
)
1372 if (sarg
& CHR_TIOCM_CAR
)
1374 if (sarg
& CHR_TIOCM_DSR
)
1376 if (sarg
& CHR_TIOCM_RI
)
1378 if (sarg
& CHR_TIOCM_DTR
)
1380 if (sarg
& CHR_TIOCM_RTS
)
1382 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1391 static void qemu_chr_close_tty(CharDriverState
*chr
)
1393 FDCharDriver
*s
= chr
->opaque
;
1397 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1407 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1409 CharDriverState
*chr
;
1411 tty_serial_init(fd
, 115200, 'N', 8, 1);
1412 chr
= qemu_chr_open_fd(fd
, fd
);
1413 chr
->chr_ioctl
= tty_serial_ioctl
;
1414 chr
->chr_close
= qemu_chr_close_tty
;
1417 #endif /* __linux__ || __sun__ */
1419 #if defined(__linux__)
1421 #define HAVE_CHARDEV_PARPORT 1
1426 } ParallelCharDriver
;
1428 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1430 if (s
->mode
!= mode
) {
1432 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1439 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1441 ParallelCharDriver
*drv
= chr
->opaque
;
1446 case CHR_IOCTL_PP_READ_DATA
:
1447 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1449 *(uint8_t *)arg
= b
;
1451 case CHR_IOCTL_PP_WRITE_DATA
:
1452 b
= *(uint8_t *)arg
;
1453 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1456 case CHR_IOCTL_PP_READ_CONTROL
:
1457 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1459 /* Linux gives only the lowest bits, and no way to know data
1460 direction! For better compatibility set the fixed upper
1462 *(uint8_t *)arg
= b
| 0xc0;
1464 case CHR_IOCTL_PP_WRITE_CONTROL
:
1465 b
= *(uint8_t *)arg
;
1466 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1469 case CHR_IOCTL_PP_READ_STATUS
:
1470 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1472 *(uint8_t *)arg
= b
;
1474 case CHR_IOCTL_PP_DATA_DIR
:
1475 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1478 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1479 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1480 struct ParallelIOArg
*parg
= arg
;
1481 int n
= read(fd
, parg
->buffer
, parg
->count
);
1482 if (n
!= parg
->count
) {
1487 case CHR_IOCTL_PP_EPP_READ
:
1488 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1489 struct ParallelIOArg
*parg
= arg
;
1490 int n
= read(fd
, parg
->buffer
, parg
->count
);
1491 if (n
!= parg
->count
) {
1496 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1497 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1498 struct ParallelIOArg
*parg
= arg
;
1499 int n
= write(fd
, parg
->buffer
, parg
->count
);
1500 if (n
!= parg
->count
) {
1505 case CHR_IOCTL_PP_EPP_WRITE
:
1506 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1507 struct ParallelIOArg
*parg
= arg
;
1508 int n
= write(fd
, parg
->buffer
, parg
->count
);
1509 if (n
!= parg
->count
) {
1520 static void pp_close(CharDriverState
*chr
)
1522 ParallelCharDriver
*drv
= chr
->opaque
;
1525 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1526 ioctl(fd
, PPRELEASE
);
1529 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1532 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1534 CharDriverState
*chr
;
1535 ParallelCharDriver
*drv
;
1537 if (ioctl(fd
, PPCLAIM
) < 0) {
1542 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1544 drv
->mode
= IEEE1284_MODE_COMPAT
;
1546 chr
= g_malloc0(sizeof(CharDriverState
));
1547 chr
->chr_write
= null_chr_write
;
1548 chr
->chr_ioctl
= pp_ioctl
;
1549 chr
->chr_close
= pp_close
;
1552 qemu_chr_generic_open(chr
);
1556 #endif /* __linux__ */
1558 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1560 #define HAVE_CHARDEV_PARPORT 1
1562 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1564 int fd
= (int)(intptr_t)chr
->opaque
;
1568 case CHR_IOCTL_PP_READ_DATA
:
1569 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1571 *(uint8_t *)arg
= b
;
1573 case CHR_IOCTL_PP_WRITE_DATA
:
1574 b
= *(uint8_t *)arg
;
1575 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1578 case CHR_IOCTL_PP_READ_CONTROL
:
1579 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1581 *(uint8_t *)arg
= b
;
1583 case CHR_IOCTL_PP_WRITE_CONTROL
:
1584 b
= *(uint8_t *)arg
;
1585 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1588 case CHR_IOCTL_PP_READ_STATUS
:
1589 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1591 *(uint8_t *)arg
= b
;
1599 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1601 CharDriverState
*chr
;
1603 chr
= g_malloc0(sizeof(CharDriverState
));
1604 chr
->opaque
= (void *)(intptr_t)fd
;
1605 chr
->chr_write
= null_chr_write
;
1606 chr
->chr_ioctl
= pp_ioctl
;
1615 HANDLE hcom
, hrecv
, hsend
;
1616 OVERLAPPED orecv
, osend
;
1623 HANDLE hInputReadyEvent
;
1624 HANDLE hInputDoneEvent
;
1625 HANDLE hInputThread
;
1626 uint8_t win_stdio_buf
;
1627 } WinStdioCharState
;
1629 #define NSENDBUF 2048
1630 #define NRECVBUF 2048
1631 #define MAXCONNECT 1
1632 #define NTIMEOUT 5000
1634 static int win_chr_poll(void *opaque
);
1635 static int win_chr_pipe_poll(void *opaque
);
1637 static void win_chr_close(CharDriverState
*chr
)
1639 WinCharState
*s
= chr
->opaque
;
1642 CloseHandle(s
->hsend
);
1646 CloseHandle(s
->hrecv
);
1650 CloseHandle(s
->hcom
);
1654 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1656 qemu_del_polling_cb(win_chr_poll
, chr
);
1658 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1661 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1663 WinCharState
*s
= chr
->opaque
;
1665 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1670 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1672 fprintf(stderr
, "Failed CreateEvent\n");
1675 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1677 fprintf(stderr
, "Failed CreateEvent\n");
1681 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1682 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1683 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1684 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1689 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1690 fprintf(stderr
, "Failed SetupComm\n");
1694 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1695 size
= sizeof(COMMCONFIG
);
1696 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1697 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1698 CommConfigDialog(filename
, NULL
, &comcfg
);
1700 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1701 fprintf(stderr
, "Failed SetCommState\n");
1705 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1706 fprintf(stderr
, "Failed SetCommMask\n");
1710 cto
.ReadIntervalTimeout
= MAXDWORD
;
1711 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1712 fprintf(stderr
, "Failed SetCommTimeouts\n");
1716 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1717 fprintf(stderr
, "Failed ClearCommError\n");
1720 qemu_add_polling_cb(win_chr_poll
, chr
);
1728 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1730 WinCharState
*s
= chr
->opaque
;
1731 DWORD len
, ret
, size
, err
;
1734 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1735 s
->osend
.hEvent
= s
->hsend
;
1738 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1740 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1742 err
= GetLastError();
1743 if (err
== ERROR_IO_PENDING
) {
1744 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1762 static int win_chr_read_poll(CharDriverState
*chr
)
1764 WinCharState
*s
= chr
->opaque
;
1766 s
->max_size
= qemu_chr_be_can_write(chr
);
1770 static void win_chr_readfile(CharDriverState
*chr
)
1772 WinCharState
*s
= chr
->opaque
;
1774 uint8_t buf
[READ_BUF_LEN
];
1777 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1778 s
->orecv
.hEvent
= s
->hrecv
;
1779 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1781 err
= GetLastError();
1782 if (err
== ERROR_IO_PENDING
) {
1783 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1788 qemu_chr_be_write(chr
, buf
, size
);
1792 static void win_chr_read(CharDriverState
*chr
)
1794 WinCharState
*s
= chr
->opaque
;
1796 if (s
->len
> s
->max_size
)
1797 s
->len
= s
->max_size
;
1801 win_chr_readfile(chr
);
1804 static int win_chr_poll(void *opaque
)
1806 CharDriverState
*chr
= opaque
;
1807 WinCharState
*s
= chr
->opaque
;
1811 ClearCommError(s
->hcom
, &comerr
, &status
);
1812 if (status
.cbInQue
> 0) {
1813 s
->len
= status
.cbInQue
;
1814 win_chr_read_poll(chr
);
1821 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1823 CharDriverState
*chr
;
1826 chr
= g_malloc0(sizeof(CharDriverState
));
1827 s
= g_malloc0(sizeof(WinCharState
));
1829 chr
->chr_write
= win_chr_write
;
1830 chr
->chr_close
= win_chr_close
;
1832 if (win_chr_init(chr
, filename
) < 0) {
1837 qemu_chr_generic_open(chr
);
1841 static int win_chr_pipe_poll(void *opaque
)
1843 CharDriverState
*chr
= opaque
;
1844 WinCharState
*s
= chr
->opaque
;
1847 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1850 win_chr_read_poll(chr
);
1857 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1859 WinCharState
*s
= chr
->opaque
;
1867 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1869 fprintf(stderr
, "Failed CreateEvent\n");
1872 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1874 fprintf(stderr
, "Failed CreateEvent\n");
1878 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1879 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1880 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1882 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1883 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1884 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1889 ZeroMemory(&ov
, sizeof(ov
));
1890 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1891 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1893 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1897 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1899 fprintf(stderr
, "Failed GetOverlappedResult\n");
1901 CloseHandle(ov
.hEvent
);
1908 CloseHandle(ov
.hEvent
);
1911 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1920 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1922 const char *filename
= opts
->device
;
1923 CharDriverState
*chr
;
1926 chr
= g_malloc0(sizeof(CharDriverState
));
1927 s
= g_malloc0(sizeof(WinCharState
));
1929 chr
->chr_write
= win_chr_write
;
1930 chr
->chr_close
= win_chr_close
;
1932 if (win_chr_pipe_init(chr
, filename
) < 0) {
1937 qemu_chr_generic_open(chr
);
1941 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1943 CharDriverState
*chr
;
1946 chr
= g_malloc0(sizeof(CharDriverState
));
1947 s
= g_malloc0(sizeof(WinCharState
));
1950 chr
->chr_write
= win_chr_write
;
1951 qemu_chr_generic_open(chr
);
1955 static CharDriverState
*qemu_chr_open_win_con(void)
1957 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1960 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1962 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
1969 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
1979 static void win_stdio_wait_func(void *opaque
)
1981 CharDriverState
*chr
= opaque
;
1982 WinStdioCharState
*stdio
= chr
->opaque
;
1983 INPUT_RECORD buf
[4];
1988 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, sizeof(buf
) / sizeof(*buf
),
1992 /* Avoid error storm */
1993 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
1997 for (i
= 0; i
< dwSize
; i
++) {
1998 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2000 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2002 if (kev
->uChar
.AsciiChar
!= 0) {
2003 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2004 if (qemu_chr_be_can_write(chr
)) {
2005 uint8_t c
= kev
->uChar
.AsciiChar
;
2006 qemu_chr_be_write(chr
, &c
, 1);
2014 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2016 CharDriverState
*chr
= param
;
2017 WinStdioCharState
*stdio
= chr
->opaque
;
2023 /* Wait for one byte */
2024 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2026 /* Exit in case of error, continue if nothing read */
2034 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2035 if (stdio
->win_stdio_buf
== '\r') {
2039 /* Signal the main thread and wait until the byte was eaten */
2040 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2043 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2049 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2053 static void win_stdio_thread_wait_func(void *opaque
)
2055 CharDriverState
*chr
= opaque
;
2056 WinStdioCharState
*stdio
= chr
->opaque
;
2058 if (qemu_chr_be_can_write(chr
)) {
2059 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2062 SetEvent(stdio
->hInputDoneEvent
);
2065 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2067 WinStdioCharState
*stdio
= chr
->opaque
;
2070 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2073 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2075 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2079 static void win_stdio_close(CharDriverState
*chr
)
2081 WinStdioCharState
*stdio
= chr
->opaque
;
2083 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2084 CloseHandle(stdio
->hInputReadyEvent
);
2086 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2087 CloseHandle(stdio
->hInputDoneEvent
);
2089 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2090 TerminateThread(stdio
->hInputThread
, 0);
2093 g_free(chr
->opaque
);
2097 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2099 CharDriverState
*chr
;
2100 WinStdioCharState
*stdio
;
2104 chr
= g_malloc0(sizeof(CharDriverState
));
2105 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2107 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2108 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2109 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2113 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2115 chr
->opaque
= stdio
;
2116 chr
->chr_write
= win_stdio_write
;
2117 chr
->chr_close
= win_stdio_close
;
2120 if (qemu_add_wait_object(stdio
->hStdIn
,
2121 win_stdio_wait_func
, chr
)) {
2122 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2127 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2128 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2129 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2132 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2133 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2134 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2135 fprintf(stderr
, "cannot create stdio thread or event\n");
2138 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2139 win_stdio_thread_wait_func
, chr
)) {
2140 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2144 dwMode
|= ENABLE_LINE_INPUT
;
2147 /* set the terminal in raw mode */
2148 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2149 dwMode
|= ENABLE_PROCESSED_INPUT
;
2152 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2154 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2155 qemu_chr_fe_set_echo(chr
, false);
2159 #endif /* !_WIN32 */
2162 /***********************************************************/
2163 /* UDP Net console */
2169 uint8_t buf
[READ_BUF_LEN
];
2175 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2177 NetCharDriver
*s
= chr
->opaque
;
2178 gsize bytes_written
;
2181 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2182 if (status
== G_IO_STATUS_EOF
) {
2184 } else if (status
!= G_IO_STATUS_NORMAL
) {
2188 return bytes_written
;
2191 static int udp_chr_read_poll(void *opaque
)
2193 CharDriverState
*chr
= opaque
;
2194 NetCharDriver
*s
= chr
->opaque
;
2196 s
->max_size
= qemu_chr_be_can_write(chr
);
2198 /* If there were any stray characters in the queue process them
2201 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2202 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2204 s
->max_size
= qemu_chr_be_can_write(chr
);
2209 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2211 CharDriverState
*chr
= opaque
;
2212 NetCharDriver
*s
= chr
->opaque
;
2213 gsize bytes_read
= 0;
2216 if (s
->max_size
== 0)
2218 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2220 s
->bufcnt
= bytes_read
;
2221 s
->bufptr
= s
->bufcnt
;
2222 if (status
!= G_IO_STATUS_NORMAL
) {
2227 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2228 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2230 s
->max_size
= qemu_chr_be_can_write(chr
);
2236 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2238 NetCharDriver
*s
= chr
->opaque
;
2241 g_source_remove(s
->tag
);
2246 s
->tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
, udp_chr_read
, chr
);
2250 static void udp_chr_close(CharDriverState
*chr
)
2252 NetCharDriver
*s
= chr
->opaque
;
2254 g_source_remove(s
->tag
);
2257 g_io_channel_unref(s
->chan
);
2261 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2264 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2266 CharDriverState
*chr
= NULL
;
2267 NetCharDriver
*s
= NULL
;
2269 chr
= g_malloc0(sizeof(CharDriverState
));
2270 s
= g_malloc0(sizeof(NetCharDriver
));
2273 s
->chan
= io_channel_from_socket(s
->fd
);
2277 chr
->chr_write
= udp_chr_write
;
2278 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2279 chr
->chr_close
= udp_chr_close
;
2283 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2285 Error
*local_err
= NULL
;
2288 fd
= inet_dgram_opts(opts
, &local_err
);
2292 return qemu_chr_open_udp_fd(fd
);
2295 /***********************************************************/
2296 /* TCP Net console */
2300 GIOChannel
*chan
, *listen_chan
;
2301 guint tag
, listen_tag
;
2311 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2313 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2315 TCPCharDriver
*s
= chr
->opaque
;
2317 return io_channel_send_all(s
->chan
, buf
, len
);
2319 /* XXX: indicate an error ? */
2324 static int tcp_chr_read_poll(void *opaque
)
2326 CharDriverState
*chr
= opaque
;
2327 TCPCharDriver
*s
= chr
->opaque
;
2330 s
->max_size
= qemu_chr_be_can_write(chr
);
2335 #define IAC_BREAK 243
2336 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2338 uint8_t *buf
, int *size
)
2340 /* Handle any telnet client's basic IAC options to satisfy char by
2341 * char mode with no echo. All IAC options will be removed from
2342 * the buf and the do_telnetopt variable will be used to track the
2343 * state of the width of the IAC information.
2345 * IAC commands come in sets of 3 bytes with the exception of the
2346 * "IAC BREAK" command and the double IAC.
2352 for (i
= 0; i
< *size
; i
++) {
2353 if (s
->do_telnetopt
> 1) {
2354 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2355 /* Double IAC means send an IAC */
2359 s
->do_telnetopt
= 1;
2361 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2362 /* Handle IAC break commands by sending a serial break */
2363 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2368 if (s
->do_telnetopt
>= 4) {
2369 s
->do_telnetopt
= 1;
2372 if ((unsigned char)buf
[i
] == IAC
) {
2373 s
->do_telnetopt
= 2;
2384 static int tcp_get_msgfd(CharDriverState
*chr
)
2386 TCPCharDriver
*s
= chr
->opaque
;
2393 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2395 TCPCharDriver
*s
= chr
->opaque
;
2396 struct cmsghdr
*cmsg
;
2398 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2401 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2402 cmsg
->cmsg_level
!= SOL_SOCKET
||
2403 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2406 fd
= *((int *)CMSG_DATA(cmsg
));
2410 #ifndef MSG_CMSG_CLOEXEC
2411 qemu_set_cloexec(fd
);
2419 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2421 TCPCharDriver
*s
= chr
->opaque
;
2422 struct msghdr msg
= { NULL
, };
2423 struct iovec iov
[1];
2425 struct cmsghdr cmsg
;
2426 char control
[CMSG_SPACE(sizeof(int))];
2431 iov
[0].iov_base
= buf
;
2432 iov
[0].iov_len
= len
;
2436 msg
.msg_control
= &msg_control
;
2437 msg
.msg_controllen
= sizeof(msg_control
);
2439 #ifdef MSG_CMSG_CLOEXEC
2440 flags
|= MSG_CMSG_CLOEXEC
;
2442 ret
= recvmsg(s
->fd
, &msg
, flags
);
2443 if (ret
> 0 && s
->is_unix
) {
2444 unix_process_msgfd(chr
, &msg
);
2450 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2452 TCPCharDriver
*s
= chr
->opaque
;
2453 return qemu_recv(s
->fd
, buf
, len
, 0);
2457 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2459 TCPCharDriver
*s
= chr
->opaque
;
2460 return g_io_create_watch(s
->chan
, cond
);
2463 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2465 CharDriverState
*chr
= opaque
;
2466 TCPCharDriver
*s
= chr
->opaque
;
2467 uint8_t buf
[READ_BUF_LEN
];
2470 if (!s
->connected
|| s
->max_size
<= 0) {
2474 if (len
> s
->max_size
)
2476 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2478 /* connection closed */
2480 if (s
->listen_chan
) {
2481 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2483 g_source_remove(s
->tag
);
2485 g_io_channel_unref(s
->chan
);
2489 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2490 } else if (size
> 0) {
2491 if (s
->do_telnetopt
)
2492 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2494 qemu_chr_be_write(chr
, buf
, size
);
2501 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2503 return qemu_chr_open_fd(eventfd
, eventfd
);
2507 static void tcp_chr_connect(void *opaque
)
2509 CharDriverState
*chr
= opaque
;
2510 TCPCharDriver
*s
= chr
->opaque
;
2514 s
->tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
, tcp_chr_read
, chr
);
2516 qemu_chr_generic_open(chr
);
2519 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2520 static void tcp_chr_telnet_init(int fd
)
2523 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2524 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2525 send(fd
, (char *)buf
, 3, 0);
2526 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2527 send(fd
, (char *)buf
, 3, 0);
2528 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2529 send(fd
, (char *)buf
, 3, 0);
2530 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2531 send(fd
, (char *)buf
, 3, 0);
2534 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2536 TCPCharDriver
*s
= chr
->opaque
;
2540 socket_set_nonblock(fd
);
2542 socket_set_nodelay(fd
);
2544 s
->chan
= io_channel_from_socket(fd
);
2545 g_source_remove(s
->listen_tag
);
2547 tcp_chr_connect(chr
);
2552 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2554 CharDriverState
*chr
= opaque
;
2555 TCPCharDriver
*s
= chr
->opaque
;
2556 struct sockaddr_in saddr
;
2558 struct sockaddr_un uaddr
;
2560 struct sockaddr
*addr
;
2567 len
= sizeof(uaddr
);
2568 addr
= (struct sockaddr
*)&uaddr
;
2572 len
= sizeof(saddr
);
2573 addr
= (struct sockaddr
*)&saddr
;
2575 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2576 if (fd
< 0 && errno
!= EINTR
) {
2578 } else if (fd
>= 0) {
2579 if (s
->do_telnetopt
)
2580 tcp_chr_telnet_init(fd
);
2584 if (tcp_chr_add_client(chr
, fd
) < 0)
2590 static void tcp_chr_close(CharDriverState
*chr
)
2592 TCPCharDriver
*s
= chr
->opaque
;
2595 g_source_remove(s
->tag
);
2598 g_io_channel_unref(s
->chan
);
2602 if (s
->listen_fd
>= 0) {
2603 if (s
->listen_tag
) {
2604 g_source_remove(s
->listen_tag
);
2606 if (s
->listen_chan
) {
2607 g_io_channel_unref(s
->listen_chan
);
2609 closesocket(s
->listen_fd
);
2612 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2615 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2616 bool is_listen
, bool is_telnet
,
2617 bool is_waitconnect
,
2620 CharDriverState
*chr
= NULL
;
2621 TCPCharDriver
*s
= NULL
;
2622 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2623 const char *left
= "", *right
= "";
2624 struct sockaddr_storage ss
;
2625 socklen_t ss_len
= sizeof(ss
);
2627 memset(&ss
, 0, ss_len
);
2628 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2629 error_setg(errp
, "getsockname: %s", strerror(errno
));
2633 chr
= g_malloc0(sizeof(CharDriverState
));
2634 s
= g_malloc0(sizeof(TCPCharDriver
));
2641 chr
->filename
= g_malloc(256);
2642 switch (ss
.ss_family
) {
2646 snprintf(chr
->filename
, 256, "unix:%s%s",
2647 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2648 is_listen
? ",server" : "");
2656 s
->do_nodelay
= do_nodelay
;
2657 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2658 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2659 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2660 is_telnet
? "telnet" : "tcp",
2661 left
, host
, right
, serv
,
2662 is_listen
? ",server" : "");
2667 chr
->chr_write
= tcp_chr_write
;
2668 chr
->chr_close
= tcp_chr_close
;
2669 chr
->get_msgfd
= tcp_get_msgfd
;
2670 chr
->chr_add_client
= tcp_chr_add_client
;
2671 chr
->chr_add_watch
= tcp_chr_add_watch
;
2675 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2676 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2678 s
->do_telnetopt
= 1;
2683 socket_set_nodelay(fd
);
2684 s
->chan
= io_channel_from_socket(s
->fd
);
2685 tcp_chr_connect(chr
);
2688 if (is_listen
&& is_waitconnect
) {
2689 printf("QEMU waiting for connection on: %s\n",
2691 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2692 socket_set_nonblock(s
->listen_fd
);
2697 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2699 CharDriverState
*chr
= NULL
;
2700 Error
*local_err
= NULL
;
2708 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2709 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2710 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2711 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2712 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2718 fd
= unix_listen_opts(opts
, &local_err
);
2720 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2724 fd
= inet_listen_opts(opts
, 0, &local_err
);
2726 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2733 if (!is_waitconnect
)
2734 socket_set_nonblock(fd
);
2736 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2737 is_waitconnect
, &local_err
);
2738 if (error_is_set(&local_err
)) {
2746 qerror_report_err(local_err
);
2747 error_free(local_err
);
2753 g_free(chr
->opaque
);
2759 /***********************************************************/
2760 /* Memory chardev */
2763 size_t outbuf_capacity
;
2767 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2769 MemoryDriver
*d
= chr
->opaque
;
2771 /* TODO: the QString implementation has the same code, we should
2772 * introduce a generic way to do this in cutils.c */
2773 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2775 d
->outbuf_capacity
+= len
;
2776 d
->outbuf_capacity
*= 2;
2777 d
->outbuf
= g_realloc(d
->outbuf
, d
->outbuf_capacity
);
2780 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2781 d
->outbuf_size
+= len
;
2786 void qemu_chr_init_mem(CharDriverState
*chr
)
2790 d
= g_malloc(sizeof(*d
));
2792 d
->outbuf_capacity
= 4096;
2793 d
->outbuf
= g_malloc0(d
->outbuf_capacity
);
2795 memset(chr
, 0, sizeof(*chr
));
2797 chr
->chr_write
= mem_chr_write
;
2800 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2802 MemoryDriver
*d
= chr
->opaque
;
2803 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2806 /* NOTE: this driver can not be closed with qemu_chr_delete()! */
2807 void qemu_chr_close_mem(CharDriverState
*chr
)
2809 MemoryDriver
*d
= chr
->opaque
;
2812 g_free(chr
->opaque
);
2814 chr
->chr_write
= NULL
;
2817 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2819 const MemoryDriver
*d
= chr
->opaque
;
2820 return d
->outbuf_size
;
2823 /*********************************************************/
2824 /* Ring buffer chardev */
2831 } RingBufCharDriver
;
2833 static size_t ringbuf_count(const CharDriverState
*chr
)
2835 const RingBufCharDriver
*d
= chr
->opaque
;
2837 return d
->prod
- d
->cons
;
2840 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2842 RingBufCharDriver
*d
= chr
->opaque
;
2845 if (!buf
|| (len
< 0)) {
2849 for (i
= 0; i
< len
; i
++ ) {
2850 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
2851 if (d
->prod
- d
->cons
> d
->size
) {
2852 d
->cons
= d
->prod
- d
->size
;
2859 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
2861 RingBufCharDriver
*d
= chr
->opaque
;
2864 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
2865 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
2871 static void ringbuf_chr_close(struct CharDriverState
*chr
)
2873 RingBufCharDriver
*d
= chr
->opaque
;
2880 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
2883 CharDriverState
*chr
;
2884 RingBufCharDriver
*d
;
2886 chr
= g_malloc0(sizeof(CharDriverState
));
2887 d
= g_malloc(sizeof(*d
));
2889 d
->size
= opts
->has_size
? opts
->size
: 65536;
2891 /* The size must be power of 2 */
2892 if (d
->size
& (d
->size
- 1)) {
2893 error_setg(errp
, "size of ringbuf chardev must be power of two");
2899 d
->cbuf
= g_malloc0(d
->size
);
2902 chr
->chr_write
= ringbuf_chr_write
;
2903 chr
->chr_close
= ringbuf_chr_close
;
2913 static bool chr_is_ringbuf(const CharDriverState
*chr
)
2915 return chr
->chr_write
== ringbuf_chr_write
;
2918 void qmp_ringbuf_write(const char *device
, const char *data
,
2919 bool has_format
, enum DataFormat format
,
2922 CharDriverState
*chr
;
2923 const uint8_t *write_data
;
2927 chr
= qemu_chr_find(device
);
2929 error_setg(errp
, "Device '%s' not found", device
);
2933 if (!chr_is_ringbuf(chr
)) {
2934 error_setg(errp
,"%s is not a ringbuf device", device
);
2938 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2939 write_data
= g_base64_decode(data
, &write_count
);
2941 write_data
= (uint8_t *)data
;
2942 write_count
= strlen(data
);
2945 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
2947 if (write_data
!= (uint8_t *)data
) {
2948 g_free((void *)write_data
);
2952 error_setg(errp
, "Failed to write to device %s", device
);
2957 char *qmp_ringbuf_read(const char *device
, int64_t size
,
2958 bool has_format
, enum DataFormat format
,
2961 CharDriverState
*chr
;
2966 chr
= qemu_chr_find(device
);
2968 error_setg(errp
, "Device '%s' not found", device
);
2972 if (!chr_is_ringbuf(chr
)) {
2973 error_setg(errp
,"%s is not a ringbuf device", device
);
2978 error_setg(errp
, "size must be greater than zero");
2982 count
= ringbuf_count(chr
);
2983 size
= size
> count
? count
: size
;
2984 read_data
= g_malloc(size
+ 1);
2986 ringbuf_chr_read(chr
, read_data
, size
);
2988 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
2989 data
= g_base64_encode(read_data
, size
);
2993 * FIXME should read only complete, valid UTF-8 characters up
2994 * to @size bytes. Invalid sequences should be replaced by a
2995 * suitable replacement character. Except when (and only
2996 * when) ring buffer lost characters since last read, initial
2997 * continuation characters should be dropped.
2999 read_data
[size
] = 0;
3000 data
= (char *)read_data
;
3006 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3008 char host
[65], port
[33], width
[8], height
[8];
3012 Error
*local_err
= NULL
;
3014 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3015 if (error_is_set(&local_err
)) {
3016 qerror_report_err(local_err
);
3017 error_free(local_err
);
3021 if (strstart(filename
, "mon:", &p
)) {
3023 qemu_opt_set(opts
, "mux", "on");
3026 if (strcmp(filename
, "null") == 0 ||
3027 strcmp(filename
, "pty") == 0 ||
3028 strcmp(filename
, "msmouse") == 0 ||
3029 strcmp(filename
, "braille") == 0 ||
3030 strcmp(filename
, "stdio") == 0) {
3031 qemu_opt_set(opts
, "backend", filename
);
3034 if (strstart(filename
, "vc", &p
)) {
3035 qemu_opt_set(opts
, "backend", "vc");
3037 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
3039 qemu_opt_set(opts
, "width", width
);
3040 qemu_opt_set(opts
, "height", height
);
3041 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
3043 qemu_opt_set(opts
, "cols", width
);
3044 qemu_opt_set(opts
, "rows", height
);
3051 if (strcmp(filename
, "con:") == 0) {
3052 qemu_opt_set(opts
, "backend", "console");
3055 if (strstart(filename
, "COM", NULL
)) {
3056 qemu_opt_set(opts
, "backend", "serial");
3057 qemu_opt_set(opts
, "path", filename
);
3060 if (strstart(filename
, "file:", &p
)) {
3061 qemu_opt_set(opts
, "backend", "file");
3062 qemu_opt_set(opts
, "path", p
);
3065 if (strstart(filename
, "pipe:", &p
)) {
3066 qemu_opt_set(opts
, "backend", "pipe");
3067 qemu_opt_set(opts
, "path", p
);
3070 if (strstart(filename
, "tcp:", &p
) ||
3071 strstart(filename
, "telnet:", &p
)) {
3072 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3074 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3077 qemu_opt_set(opts
, "backend", "socket");
3078 qemu_opt_set(opts
, "host", host
);
3079 qemu_opt_set(opts
, "port", port
);
3080 if (p
[pos
] == ',') {
3081 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3084 if (strstart(filename
, "telnet:", &p
))
3085 qemu_opt_set(opts
, "telnet", "on");
3088 if (strstart(filename
, "udp:", &p
)) {
3089 qemu_opt_set(opts
, "backend", "udp");
3090 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3092 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3096 qemu_opt_set(opts
, "host", host
);
3097 qemu_opt_set(opts
, "port", port
);
3098 if (p
[pos
] == '@') {
3100 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3102 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3106 qemu_opt_set(opts
, "localaddr", host
);
3107 qemu_opt_set(opts
, "localport", port
);
3111 if (strstart(filename
, "unix:", &p
)) {
3112 qemu_opt_set(opts
, "backend", "socket");
3113 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3117 if (strstart(filename
, "/dev/parport", NULL
) ||
3118 strstart(filename
, "/dev/ppi", NULL
)) {
3119 qemu_opt_set(opts
, "backend", "parport");
3120 qemu_opt_set(opts
, "path", filename
);
3123 if (strstart(filename
, "/dev/", NULL
)) {
3124 qemu_opt_set(opts
, "backend", "tty");
3125 qemu_opt_set(opts
, "path", filename
);
3130 qemu_opts_del(opts
);
3134 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3137 const char *path
= qemu_opt_get(opts
, "path");
3140 error_setg(errp
, "chardev: file: no filename given");
3143 backend
->file
= g_new0(ChardevFile
, 1);
3144 backend
->file
->out
= g_strdup(path
);
3147 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3150 backend
->stdio
= g_new0(ChardevStdio
, 1);
3151 backend
->stdio
->has_signal
= true;
3152 backend
->stdio
->signal
=
3153 qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
);
3156 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3159 const char *device
= qemu_opt_get(opts
, "path");
3161 if (device
== NULL
) {
3162 error_setg(errp
, "chardev: serial/tty: no device path given");
3165 backend
->serial
= g_new0(ChardevHostdev
, 1);
3166 backend
->serial
->device
= g_strdup(device
);
3169 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3172 const char *device
= qemu_opt_get(opts
, "path");
3174 if (device
== NULL
) {
3175 error_setg(errp
, "chardev: parallel: no device path given");
3178 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3179 backend
->parallel
->device
= g_strdup(device
);
3182 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3185 const char *device
= qemu_opt_get(opts
, "path");
3187 if (device
== NULL
) {
3188 error_setg(errp
, "chardev: pipe: no device path given");
3191 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3192 backend
->pipe
->device
= g_strdup(device
);
3195 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3200 backend
->memory
= g_new0(ChardevRingbuf
, 1);
3202 val
= qemu_opt_get_number(opts
, "size", 0);
3204 backend
->memory
->has_size
= true;
3205 backend
->memory
->size
= val
;
3209 typedef struct CharDriver
{
3212 CharDriverState
*(*open
)(QemuOpts
*opts
);
3213 /* new, qapi-based */
3215 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3218 static GSList
*backends
;
3220 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3224 s
= g_malloc0(sizeof(*s
));
3225 s
->name
= g_strdup(name
);
3228 backends
= g_slist_append(backends
, s
);
3231 void register_char_driver_qapi(const char *name
, int kind
,
3232 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3236 s
= g_malloc0(sizeof(*s
));
3237 s
->name
= g_strdup(name
);
3241 backends
= g_slist_append(backends
, s
);
3244 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3245 void (*init
)(struct CharDriverState
*s
),
3249 CharDriverState
*chr
;
3252 if (qemu_opts_id(opts
) == NULL
) {
3253 error_setg(errp
, "chardev: no id specified");
3257 if (qemu_opt_get(opts
, "backend") == NULL
) {
3258 error_setg(errp
, "chardev: \"%s\" missing backend",
3259 qemu_opts_id(opts
));
3262 for (i
= backends
; i
; i
= i
->next
) {
3265 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3270 error_setg(errp
, "chardev: backend \"%s\" not found",
3271 qemu_opt_get(opts
, "backend"));
3276 /* using new, qapi init */
3277 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3278 ChardevReturn
*ret
= NULL
;
3279 const char *id
= qemu_opts_id(opts
);
3280 const char *bid
= NULL
;
3282 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3283 bid
= g_strdup_printf("%s-base", id
);
3287 backend
->kind
= cd
->kind
;
3289 cd
->parse(opts
, backend
, errp
);
3290 if (error_is_set(errp
)) {
3294 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3295 if (error_is_set(errp
)) {
3300 qapi_free_ChardevBackend(backend
);
3301 qapi_free_ChardevReturn(ret
);
3302 backend
= g_new0(ChardevBackend
, 1);
3303 backend
->mux
= g_new0(ChardevMux
, 1);
3304 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3305 backend
->mux
->chardev
= g_strdup(bid
);
3306 ret
= qmp_chardev_add(id
, backend
, errp
);
3307 if (error_is_set(errp
)) {
3312 chr
= qemu_chr_find(id
);
3315 qapi_free_ChardevBackend(backend
);
3316 qapi_free_ChardevReturn(ret
);
3320 chr
= cd
->open(opts
);
3322 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3323 qemu_opt_get(opts
, "backend"));
3328 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3330 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3332 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3333 CharDriverState
*base
= chr
;
3334 int len
= strlen(qemu_opts_id(opts
)) + 6;
3335 base
->label
= g_malloc(len
);
3336 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3337 chr
= qemu_chr_open_mux(base
);
3338 chr
->filename
= base
->filename
;
3339 chr
->avail_connections
= MAX_MUX
;
3340 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3342 chr
->avail_connections
= 1;
3344 chr
->label
= g_strdup(qemu_opts_id(opts
));
3349 qemu_opts_del(opts
);
3353 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3356 CharDriverState
*chr
;
3360 if (strstart(filename
, "chardev:", &p
)) {
3361 return qemu_chr_find(p
);
3364 opts
= qemu_chr_parse_compat(label
, filename
);
3368 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3369 if (error_is_set(&err
)) {
3370 fprintf(stderr
, "%s\n", error_get_pretty(err
));
3373 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3374 monitor_init(chr
, MONITOR_USE_READLINE
);
3379 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3381 if (chr
->chr_set_echo
) {
3382 chr
->chr_set_echo(chr
, echo
);
3386 void qemu_chr_fe_open(struct CharDriverState
*chr
)
3388 if (chr
->chr_guest_open
) {
3389 chr
->chr_guest_open(chr
);
3393 void qemu_chr_fe_close(struct CharDriverState
*chr
)
3395 if (chr
->chr_guest_close
) {
3396 chr
->chr_guest_close(chr
);
3400 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3401 GIOFunc func
, void *user_data
)
3406 if (s
->chr_add_watch
== NULL
) {
3410 src
= s
->chr_add_watch(s
, cond
);
3411 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3412 tag
= g_source_attach(src
, NULL
);
3413 g_source_unref(src
);
3418 void qemu_chr_delete(CharDriverState
*chr
)
3420 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3421 if (chr
->chr_close
) {
3422 chr
->chr_close(chr
);
3424 g_free(chr
->filename
);
3427 qemu_opts_del(chr
->opts
);
3432 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3434 ChardevInfoList
*chr_list
= NULL
;
3435 CharDriverState
*chr
;
3437 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3438 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3439 info
->value
= g_malloc0(sizeof(*info
->value
));
3440 info
->value
->label
= g_strdup(chr
->label
);
3441 info
->value
->filename
= g_strdup(chr
->filename
);
3443 info
->next
= chr_list
;
3450 CharDriverState
*qemu_chr_find(const char *name
)
3452 CharDriverState
*chr
;
3454 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3455 if (strcmp(chr
->label
, name
) != 0)
3462 /* Get a character (serial) device interface. */
3463 CharDriverState
*qemu_char_get_next_serial(void)
3465 static int next_serial
;
3467 /* FIXME: This function needs to go away: use chardev properties! */
3468 return serial_hds
[next_serial
++];
3471 QemuOptsList qemu_chardev_opts
= {
3473 .implied_opt_name
= "backend",
3474 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3478 .type
= QEMU_OPT_STRING
,
3481 .type
= QEMU_OPT_STRING
,
3484 .type
= QEMU_OPT_STRING
,
3487 .type
= QEMU_OPT_STRING
,
3489 .name
= "localaddr",
3490 .type
= QEMU_OPT_STRING
,
3492 .name
= "localport",
3493 .type
= QEMU_OPT_STRING
,
3496 .type
= QEMU_OPT_NUMBER
,
3499 .type
= QEMU_OPT_BOOL
,
3502 .type
= QEMU_OPT_BOOL
,
3505 .type
= QEMU_OPT_BOOL
,
3508 .type
= QEMU_OPT_BOOL
,
3511 .type
= QEMU_OPT_BOOL
,
3514 .type
= QEMU_OPT_BOOL
,
3517 .type
= QEMU_OPT_NUMBER
,
3520 .type
= QEMU_OPT_NUMBER
,
3523 .type
= QEMU_OPT_NUMBER
,
3526 .type
= QEMU_OPT_NUMBER
,
3529 .type
= QEMU_OPT_BOOL
,
3532 .type
= QEMU_OPT_BOOL
,
3535 .type
= QEMU_OPT_STRING
,
3538 .type
= QEMU_OPT_NUMBER
,
3541 .type
= QEMU_OPT_SIZE
,
3543 { /* end of list */ }
3549 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3554 error_setg(errp
, "input file not supported");
3558 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3559 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3560 if (out
== INVALID_HANDLE_VALUE
) {
3561 error_setg(errp
, "open %s failed", file
->out
);
3564 return qemu_chr_open_win_file(out
);
3567 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3570 return qemu_chr_open_win_path(serial
->device
);
3573 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3576 error_setg(errp
, "character device backend type 'parallel' not supported");
3582 static int qmp_chardev_open_file_source(char *src
, int flags
,
3587 TFR(fd
= qemu_open(src
, flags
, 0666));
3589 error_setg(errp
, "open %s: %s", src
, strerror(errno
));
3594 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3596 int flags
, in
= -1, out
= -1;
3598 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3599 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3600 if (error_is_set(errp
)) {
3606 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3607 if (error_is_set(errp
)) {
3613 return qemu_chr_open_fd(in
, out
);
3616 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3619 #ifdef HAVE_CHARDEV_TTY
3622 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3623 if (error_is_set(errp
)) {
3626 socket_set_nonblock(fd
);
3627 return qemu_chr_open_tty_fd(fd
);
3629 error_setg(errp
, "character device backend type 'serial' not supported");
3634 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3637 #ifdef HAVE_CHARDEV_PARPORT
3640 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3641 if (error_is_set(errp
)) {
3644 return qemu_chr_open_pp_fd(fd
);
3646 error_setg(errp
, "character device backend type 'parallel' not supported");
3653 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3656 SocketAddress
*addr
= sock
->addr
;
3657 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3658 bool is_listen
= sock
->has_server
? sock
->server
: true;
3659 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3660 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3664 fd
= socket_listen(addr
, errp
);
3666 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3668 if (error_is_set(errp
)) {
3671 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3672 is_telnet
, is_waitconnect
, errp
);
3675 static CharDriverState
*qmp_chardev_open_dgram(ChardevDgram
*dgram
,
3680 fd
= socket_dgram(dgram
->remote
, dgram
->local
, errp
);
3681 if (error_is_set(errp
)) {
3684 return qemu_chr_open_udp_fd(fd
);
3687 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3690 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3691 CharDriverState
*base
, *chr
= NULL
;
3693 chr
= qemu_chr_find(id
);
3695 error_setg(errp
, "Chardev '%s' already exists", id
);
3700 switch (backend
->kind
) {
3701 case CHARDEV_BACKEND_KIND_FILE
:
3702 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3704 case CHARDEV_BACKEND_KIND_SERIAL
:
3705 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3707 case CHARDEV_BACKEND_KIND_PARALLEL
:
3708 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3710 case CHARDEV_BACKEND_KIND_PIPE
:
3711 chr
= qemu_chr_open_pipe(backend
->pipe
);
3713 case CHARDEV_BACKEND_KIND_SOCKET
:
3714 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3716 case CHARDEV_BACKEND_KIND_DGRAM
:
3717 chr
= qmp_chardev_open_dgram(backend
->dgram
, errp
);
3719 #ifdef HAVE_CHARDEV_TTY
3720 case CHARDEV_BACKEND_KIND_PTY
:
3721 chr
= qemu_chr_open_pty(id
, ret
);
3724 case CHARDEV_BACKEND_KIND_NULL
:
3725 chr
= qemu_chr_open_null();
3727 case CHARDEV_BACKEND_KIND_MUX
:
3728 base
= qemu_chr_find(backend
->mux
->chardev
);
3730 error_setg(errp
, "mux: base chardev %s not found",
3731 backend
->mux
->chardev
);
3734 chr
= qemu_chr_open_mux(base
);
3736 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3737 chr
= qemu_chr_open_msmouse();
3739 #ifdef CONFIG_BRLAPI
3740 case CHARDEV_BACKEND_KIND_BRAILLE
:
3741 chr
= chr_baum_init();
3744 case CHARDEV_BACKEND_KIND_STDIO
:
3745 chr
= qemu_chr_open_stdio(backend
->stdio
);
3748 case CHARDEV_BACKEND_KIND_CONSOLE
:
3749 chr
= qemu_chr_open_win_con();
3753 case CHARDEV_BACKEND_KIND_SPICEVMC
:
3754 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
3756 case CHARDEV_BACKEND_KIND_SPICEPORT
:
3757 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
3760 case CHARDEV_BACKEND_KIND_VC
:
3761 chr
= vc_init(backend
->vc
);
3763 case CHARDEV_BACKEND_KIND_MEMORY
:
3764 chr
= qemu_chr_open_ringbuf(backend
->memory
, errp
);
3767 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
3771 if (chr
== NULL
&& !error_is_set(errp
)) {
3772 error_setg(errp
, "Failed to create chardev");
3775 chr
->label
= g_strdup(id
);
3776 chr
->avail_connections
=
3777 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
3778 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3786 void qmp_chardev_remove(const char *id
, Error
**errp
)
3788 CharDriverState
*chr
;
3790 chr
= qemu_chr_find(id
);
3792 error_setg(errp
, "Chardev '%s' not found", id
);
3795 if (chr
->chr_can_read
|| chr
->chr_read
||
3796 chr
->chr_event
|| chr
->handler_opaque
) {
3797 error_setg(errp
, "Chardev '%s' is busy", id
);
3800 qemu_chr_delete(chr
);
3803 static void register_types(void)
3805 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
3806 register_char_driver("socket", qemu_chr_open_socket
);
3807 register_char_driver("udp", qemu_chr_open_udp
);
3808 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
3809 qemu_chr_parse_ringbuf
);
3810 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
3811 qemu_chr_parse_file_out
);
3812 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
3813 qemu_chr_parse_stdio
);
3814 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
3815 qemu_chr_parse_serial
);
3816 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
3817 qemu_chr_parse_serial
);
3818 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
3819 qemu_chr_parse_parallel
);
3820 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
3821 qemu_chr_parse_parallel
);
3822 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
3823 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
3824 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
3825 qemu_chr_parse_pipe
);
3828 type_init(register_types
);