4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "sysemu/sysemu.h"
27 #include "qemu/timer.h"
28 #include "sysemu/char.h"
30 #include "qmp-commands.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #include <arpa/inet.h>
52 #include <sys/select.h>
55 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56 #include <dev/ppbus/ppi.h>
57 #include <dev/ppbus/ppbconf.h>
58 #elif defined(__DragonFly__)
59 #include <dev/misc/ppi/ppi.h>
60 #include <bus/ppbus/ppbconf.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
82 #include "qemu/sockets.h"
83 #include "ui/qemu-spice.h"
85 #define READ_BUF_LEN 4096
86 #define READ_RETRIES 10
88 /***********************************************************/
89 /* character device */
91 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
92 QTAILQ_HEAD_INITIALIZER(chardevs
);
94 void qemu_chr_be_event(CharDriverState
*s
, int event
)
96 /* Keep track if the char device is open */
98 case CHR_EVENT_OPENED
:
101 case CHR_EVENT_CLOSED
:
108 s
->chr_event(s
->handler_opaque
, event
);
111 void qemu_chr_be_generic_open(CharDriverState
*s
)
113 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
116 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
118 return s
->chr_write(s
, buf
, len
);
121 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
126 while (offset
< len
) {
128 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
129 if (res
== -1 && errno
== EAGAIN
) {
132 } while (res
== -1 && errno
== EAGAIN
);
148 int qemu_chr_fe_read_all(CharDriverState
*s
, uint8_t *buf
, int len
)
150 int offset
= 0, counter
= 10;
153 if (!s
->chr_sync_read
) {
157 while (offset
< len
) {
159 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
160 if (res
== -1 && errno
== EAGAIN
) {
163 } while (res
== -1 && errno
== EAGAIN
);
183 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
187 return s
->chr_ioctl(s
, cmd
, arg
);
190 int qemu_chr_be_can_write(CharDriverState
*s
)
192 if (!s
->chr_can_read
)
194 return s
->chr_can_read(s
->handler_opaque
);
197 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
200 s
->chr_read(s
->handler_opaque
, buf
, len
);
204 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
207 return (qemu_chr_fe_get_msgfds(s
, &fd
, 1) == 1) ? fd
: -1;
210 int qemu_chr_fe_get_msgfds(CharDriverState
*s
, int *fds
, int len
)
212 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
215 int qemu_chr_fe_set_msgfds(CharDriverState
*s
, int *fds
, int num
)
217 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
220 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
222 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
225 void qemu_chr_accept_input(CharDriverState
*s
)
227 if (s
->chr_accept_input
)
228 s
->chr_accept_input(s
);
232 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
234 char buf
[READ_BUF_LEN
];
237 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
238 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
242 static void remove_fd_in_watch(CharDriverState
*chr
);
244 void qemu_chr_add_handlers(CharDriverState
*s
,
245 IOCanReadHandler
*fd_can_read
,
246 IOReadHandler
*fd_read
,
247 IOEventHandler
*fd_event
,
252 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
254 remove_fd_in_watch(s
);
258 s
->chr_can_read
= fd_can_read
;
259 s
->chr_read
= fd_read
;
260 s
->chr_event
= fd_event
;
261 s
->handler_opaque
= opaque
;
262 if (fe_open
&& s
->chr_update_read_handler
)
263 s
->chr_update_read_handler(s
);
265 if (!s
->explicit_fe_open
) {
266 qemu_chr_fe_set_open(s
, fe_open
);
269 /* We're connecting to an already opened device, so let's make sure we
270 also get the open event */
271 if (fe_open
&& s
->be_open
) {
272 qemu_chr_be_generic_open(s
);
276 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
281 static CharDriverState
*qemu_chr_open_null(void)
283 CharDriverState
*chr
;
285 chr
= g_malloc0(sizeof(CharDriverState
));
286 chr
->chr_write
= null_chr_write
;
287 chr
->explicit_be_open
= true;
291 /* MUX driver for serial I/O splitting */
293 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
294 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
296 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
297 IOReadHandler
*chr_read
[MAX_MUX
];
298 IOEventHandler
*chr_event
[MAX_MUX
];
299 void *ext_opaque
[MAX_MUX
];
300 CharDriverState
*drv
;
305 /* Intermediate input buffer allows to catch escape sequences even if the
306 currently active device is not accepting any input - but only until it
308 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
313 int64_t timestamps_start
;
317 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
319 MuxDriver
*d
= chr
->opaque
;
321 if (!d
->timestamps
) {
322 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
327 for (i
= 0; i
< len
; i
++) {
333 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
334 if (d
->timestamps_start
== -1)
335 d
->timestamps_start
= ti
;
336 ti
-= d
->timestamps_start
;
338 snprintf(buf1
, sizeof(buf1
),
339 "[%02d:%02d:%02d.%03d] ",
344 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
347 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
348 if (buf
[i
] == '\n') {
356 static const char * const mux_help
[] = {
357 "% h print this help\n\r",
358 "% x exit emulator\n\r",
359 "% s save disk data back to file (if -snapshot)\n\r",
360 "% t toggle console timestamps\n\r"
361 "% b send break (magic sysrq)\n\r",
362 "% c switch between console and monitor\n\r",
367 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
368 static void mux_print_help(CharDriverState
*chr
)
371 char ebuf
[15] = "Escape-Char";
372 char cbuf
[50] = "\n\r";
374 if (term_escape_char
> 0 && term_escape_char
< 26) {
375 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
376 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
378 snprintf(cbuf
, sizeof(cbuf
),
379 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
382 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
383 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
384 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
385 if (mux_help
[i
][j
] == '%')
386 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
388 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
393 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
395 if (d
->chr_event
[mux_nr
])
396 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
399 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
401 if (d
->term_got_escape
) {
402 d
->term_got_escape
= 0;
403 if (ch
== term_escape_char
)
412 const char *term
= "QEMU: Terminated\n\r";
413 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
421 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
424 /* Switch to the next registered device */
425 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
427 if (d
->focus
>= d
->mux_cnt
)
429 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
432 d
->timestamps
= !d
->timestamps
;
433 d
->timestamps_start
= -1;
437 } else if (ch
== term_escape_char
) {
438 d
->term_got_escape
= 1;
446 static void mux_chr_accept_input(CharDriverState
*chr
)
448 MuxDriver
*d
= chr
->opaque
;
451 while (d
->prod
[m
] != d
->cons
[m
] &&
452 d
->chr_can_read
[m
] &&
453 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
454 d
->chr_read
[m
](d
->ext_opaque
[m
],
455 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
459 static int mux_chr_can_read(void *opaque
)
461 CharDriverState
*chr
= opaque
;
462 MuxDriver
*d
= chr
->opaque
;
465 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
467 if (d
->chr_can_read
[m
])
468 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
472 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
474 CharDriverState
*chr
= opaque
;
475 MuxDriver
*d
= chr
->opaque
;
479 mux_chr_accept_input (opaque
);
481 for(i
= 0; i
< size
; i
++)
482 if (mux_proc_byte(chr
, d
, buf
[i
])) {
483 if (d
->prod
[m
] == d
->cons
[m
] &&
484 d
->chr_can_read
[m
] &&
485 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
486 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
488 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
492 static void mux_chr_event(void *opaque
, int event
)
494 CharDriverState
*chr
= opaque
;
495 MuxDriver
*d
= chr
->opaque
;
498 /* Send the event to all registered listeners */
499 for (i
= 0; i
< d
->mux_cnt
; i
++)
500 mux_chr_send_event(d
, i
, event
);
503 static void mux_chr_update_read_handler(CharDriverState
*chr
)
505 MuxDriver
*d
= chr
->opaque
;
507 if (d
->mux_cnt
>= MAX_MUX
) {
508 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
511 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
512 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
513 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
514 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
515 /* Fix up the real driver with mux routines */
516 if (d
->mux_cnt
== 0) {
517 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
520 if (d
->focus
!= -1) {
521 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
523 d
->focus
= d
->mux_cnt
;
525 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
528 static bool muxes_realized
;
531 * Called after processing of default and command-line-specified
532 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
533 * to a mux chardev. This is done here to ensure that
534 * output/prompts/banners are only displayed for the FE that has
535 * focus when initial command-line processing/machine init is
538 * After this point, any new FE attached to any new or existing
539 * mux will receive CHR_EVENT_OPENED notifications for the BE
542 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
544 CharDriverState
*chr
;
546 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
548 MuxDriver
*d
= chr
->opaque
;
551 /* send OPENED to all already-attached FEs */
552 for (i
= 0; i
< d
->mux_cnt
; i
++) {
553 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
555 /* mark mux as OPENED so any new FEs will immediately receive
558 qemu_chr_be_generic_open(chr
);
561 muxes_realized
= true;
564 static Notifier muxes_realize_notify
= {
565 .notify
= muxes_realize_done
,
568 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
570 CharDriverState
*chr
;
573 chr
= g_malloc0(sizeof(CharDriverState
));
574 d
= g_malloc0(sizeof(MuxDriver
));
579 chr
->chr_write
= mux_chr_write
;
580 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
581 chr
->chr_accept_input
= mux_chr_accept_input
;
582 /* Frontend guest-open / -close notification is not support with muxes */
583 chr
->chr_set_fe_open
= NULL
;
584 /* only default to opened state if we've realized the initial
587 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
595 int send_all(int fd
, const void *buf
, int len1
)
601 ret
= send(fd
, buf
, len
, 0);
603 errno
= WSAGetLastError();
604 if (errno
!= WSAEWOULDBLOCK
) {
607 } else if (ret
== 0) {
619 int send_all(int fd
, const void *_buf
, int len1
)
622 const uint8_t *buf
= _buf
;
626 ret
= write(fd
, buf
, len
);
628 if (errno
!= EINTR
&& errno
!= EAGAIN
)
630 } else if (ret
== 0) {
640 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
646 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
648 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
665 typedef struct IOWatchPoll
672 IOCanReadHandler
*fd_can_read
;
677 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
679 return container_of(source
, IOWatchPoll
, parent
);
682 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
684 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
685 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
686 bool was_active
= iwp
->src
!= NULL
;
687 if (was_active
== now_active
) {
692 iwp
->src
= g_io_create_watch(iwp
->channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
693 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
694 g_source_attach(iwp
->src
, NULL
);
696 g_source_destroy(iwp
->src
);
697 g_source_unref(iwp
->src
);
703 static gboolean
io_watch_poll_check(GSource
*source
)
708 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
714 static void io_watch_poll_finalize(GSource
*source
)
716 /* Due to a glib bug, removing the last reference to a source
717 * inside a finalize callback causes recursive locking (and a
718 * deadlock). This is not a problem inside other callbacks,
719 * including dispatch callbacks, so we call io_remove_watch_poll
720 * to remove this source. At this point, iwp->src must
721 * be NULL, or we would leak it.
723 * This would be solved much more elegantly by child sources,
724 * but we support older glib versions that do not have them.
726 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
727 assert(iwp
->src
== NULL
);
730 static GSourceFuncs io_watch_poll_funcs
= {
731 .prepare
= io_watch_poll_prepare
,
732 .check
= io_watch_poll_check
,
733 .dispatch
= io_watch_poll_dispatch
,
734 .finalize
= io_watch_poll_finalize
,
737 /* Can only be used for read */
738 static guint
io_add_watch_poll(GIOChannel
*channel
,
739 IOCanReadHandler
*fd_can_read
,
746 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
747 iwp
->fd_can_read
= fd_can_read
;
748 iwp
->opaque
= user_data
;
749 iwp
->channel
= channel
;
750 iwp
->fd_read
= (GSourceFunc
) fd_read
;
753 tag
= g_source_attach(&iwp
->parent
, NULL
);
754 g_source_unref(&iwp
->parent
);
758 static void io_remove_watch_poll(guint tag
)
763 g_return_if_fail (tag
> 0);
765 source
= g_main_context_find_source_by_id(NULL
, tag
);
766 g_return_if_fail (source
!= NULL
);
768 iwp
= io_watch_poll_from_source(source
);
770 g_source_destroy(iwp
->src
);
771 g_source_unref(iwp
->src
);
774 g_source_destroy(&iwp
->parent
);
777 static void remove_fd_in_watch(CharDriverState
*chr
)
779 if (chr
->fd_in_tag
) {
780 io_remove_watch_poll(chr
->fd_in_tag
);
786 static GIOChannel
*io_channel_from_fd(int fd
)
794 chan
= g_io_channel_unix_new(fd
);
796 g_io_channel_set_encoding(chan
, NULL
, NULL
);
797 g_io_channel_set_buffered(chan
, FALSE
);
803 static GIOChannel
*io_channel_from_socket(int fd
)
812 chan
= g_io_channel_win32_new_socket(fd
);
814 chan
= g_io_channel_unix_new(fd
);
817 g_io_channel_set_encoding(chan
, NULL
, NULL
);
818 g_io_channel_set_buffered(chan
, FALSE
);
823 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
826 GIOStatus status
= G_IO_STATUS_NORMAL
;
828 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
829 gsize bytes_written
= 0;
831 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
832 &bytes_written
, NULL
);
833 offset
+= bytes_written
;
840 case G_IO_STATUS_NORMAL
:
843 case G_IO_STATUS_AGAIN
:
855 typedef struct FDCharDriver
{
856 CharDriverState
*chr
;
857 GIOChannel
*fd_in
, *fd_out
;
859 QTAILQ_ENTRY(FDCharDriver
) node
;
862 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
864 FDCharDriver
*s
= chr
->opaque
;
866 return io_channel_send(s
->fd_out
, buf
, len
);
869 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
871 CharDriverState
*chr
= opaque
;
872 FDCharDriver
*s
= chr
->opaque
;
874 uint8_t buf
[READ_BUF_LEN
];
879 if (len
> s
->max_size
) {
886 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
887 len
, &bytes_read
, NULL
);
888 if (status
== G_IO_STATUS_EOF
) {
889 remove_fd_in_watch(chr
);
890 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
893 if (status
== G_IO_STATUS_NORMAL
) {
894 qemu_chr_be_write(chr
, buf
, bytes_read
);
900 static int fd_chr_read_poll(void *opaque
)
902 CharDriverState
*chr
= opaque
;
903 FDCharDriver
*s
= chr
->opaque
;
905 s
->max_size
= qemu_chr_be_can_write(chr
);
909 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
911 FDCharDriver
*s
= chr
->opaque
;
912 return g_io_create_watch(s
->fd_out
, cond
);
915 static void fd_chr_update_read_handler(CharDriverState
*chr
)
917 FDCharDriver
*s
= chr
->opaque
;
919 remove_fd_in_watch(chr
);
921 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
926 static void fd_chr_close(struct CharDriverState
*chr
)
928 FDCharDriver
*s
= chr
->opaque
;
930 remove_fd_in_watch(chr
);
932 g_io_channel_unref(s
->fd_in
);
935 g_io_channel_unref(s
->fd_out
);
939 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
942 /* open a character device to a unix fd */
943 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
945 CharDriverState
*chr
;
948 chr
= g_malloc0(sizeof(CharDriverState
));
949 s
= g_malloc0(sizeof(FDCharDriver
));
950 s
->fd_in
= io_channel_from_fd(fd_in
);
951 s
->fd_out
= io_channel_from_fd(fd_out
);
952 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
955 chr
->chr_add_watch
= fd_chr_add_watch
;
956 chr
->chr_write
= fd_chr_write
;
957 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
958 chr
->chr_close
= fd_chr_close
;
963 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
966 char filename_in
[256], filename_out
[256];
967 const char *filename
= opts
->device
;
969 if (filename
== NULL
) {
970 fprintf(stderr
, "chardev: pipe: no filename given\n");
974 snprintf(filename_in
, 256, "%s.in", filename
);
975 snprintf(filename_out
, 256, "%s.out", filename
);
976 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
977 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
978 if (fd_in
< 0 || fd_out
< 0) {
983 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
988 return qemu_chr_open_fd(fd_in
, fd_out
);
991 /* init terminal so that we can grab keys */
992 static struct termios oldtty
;
993 static int old_fd0_flags
;
994 static bool stdio_allow_signal
;
996 static void term_exit(void)
998 tcsetattr (0, TCSANOW
, &oldtty
);
999 fcntl(0, F_SETFL
, old_fd0_flags
);
1002 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1008 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1009 |INLCR
|IGNCR
|ICRNL
|IXON
);
1010 tty
.c_oflag
|= OPOST
;
1011 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1012 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1015 tty
.c_cc
[VTIME
] = 0;
1017 if (!stdio_allow_signal
)
1018 tty
.c_lflag
&= ~ISIG
;
1020 tcsetattr (0, TCSANOW
, &tty
);
1023 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1029 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
1031 CharDriverState
*chr
;
1033 if (is_daemonized()) {
1034 error_report("cannot use stdio with -daemonize");
1037 old_fd0_flags
= fcntl(0, F_GETFL
);
1038 tcgetattr (0, &oldtty
);
1039 fcntl(0, F_SETFL
, O_NONBLOCK
);
1042 chr
= qemu_chr_open_fd(0, 1);
1043 chr
->chr_close
= qemu_chr_close_stdio
;
1044 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1045 if (opts
->has_signal
) {
1046 stdio_allow_signal
= opts
->signal
;
1048 qemu_chr_fe_set_echo(chr
, false);
1053 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1054 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1055 || defined(__GLIBC__)
1057 #define HAVE_CHARDEV_TTY 1
1066 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1067 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1069 static gboolean
pty_chr_timer(gpointer opaque
)
1071 struct CharDriverState
*chr
= opaque
;
1072 PtyCharDriver
*s
= chr
->opaque
;
1075 if (!s
->connected
) {
1077 pty_chr_update_read_handler(chr
);
1082 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1084 PtyCharDriver
*s
= chr
->opaque
;
1087 g_source_remove(s
->timer_tag
);
1092 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1094 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1098 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1100 PtyCharDriver
*s
= chr
->opaque
;
1102 if (!s
->connected
) {
1103 /* guest sends data, check for (re-)connect */
1104 pty_chr_update_read_handler(chr
);
1107 return io_channel_send(s
->fd
, buf
, len
);
1110 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1112 PtyCharDriver
*s
= chr
->opaque
;
1113 return g_io_create_watch(s
->fd
, cond
);
1116 static int pty_chr_read_poll(void *opaque
)
1118 CharDriverState
*chr
= opaque
;
1119 PtyCharDriver
*s
= chr
->opaque
;
1121 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1122 return s
->read_bytes
;
1125 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1127 CharDriverState
*chr
= opaque
;
1128 PtyCharDriver
*s
= chr
->opaque
;
1130 uint8_t buf
[READ_BUF_LEN
];
1134 if (len
> s
->read_bytes
)
1135 len
= s
->read_bytes
;
1139 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1140 if (status
!= G_IO_STATUS_NORMAL
) {
1141 pty_chr_state(chr
, 0);
1144 pty_chr_state(chr
, 1);
1145 qemu_chr_be_write(chr
, buf
, size
);
1150 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1152 PtyCharDriver
*s
= chr
->opaque
;
1155 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1156 pfd
.events
= G_IO_OUT
;
1159 if (pfd
.revents
& G_IO_HUP
) {
1160 pty_chr_state(chr
, 0);
1162 pty_chr_state(chr
, 1);
1166 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1168 PtyCharDriver
*s
= chr
->opaque
;
1171 remove_fd_in_watch(chr
);
1173 /* (re-)connect poll interval for idle guests: once per second.
1174 * We check more frequently in case the guests sends data to
1175 * the virtual device linked to our pty. */
1176 pty_chr_rearm_timer(chr
, 1000);
1179 g_source_remove(s
->timer_tag
);
1182 if (!s
->connected
) {
1184 qemu_chr_be_generic_open(chr
);
1186 if (!chr
->fd_in_tag
) {
1187 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1193 static void pty_chr_close(struct CharDriverState
*chr
)
1195 PtyCharDriver
*s
= chr
->opaque
;
1198 remove_fd_in_watch(chr
);
1199 fd
= g_io_channel_unix_get_fd(s
->fd
);
1200 g_io_channel_unref(s
->fd
);
1203 g_source_remove(s
->timer_tag
);
1207 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1210 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1213 CharDriverState
*chr
;
1215 int master_fd
, slave_fd
;
1216 char pty_name
[PATH_MAX
];
1218 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1219 if (master_fd
< 0) {
1225 chr
= g_malloc0(sizeof(CharDriverState
));
1227 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1228 ret
->pty
= g_strdup(pty_name
);
1229 ret
->has_pty
= true;
1231 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1234 s
= g_malloc0(sizeof(PtyCharDriver
));
1236 chr
->chr_write
= pty_chr_write
;
1237 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1238 chr
->chr_close
= pty_chr_close
;
1239 chr
->chr_add_watch
= pty_chr_add_watch
;
1240 chr
->explicit_be_open
= true;
1242 s
->fd
= io_channel_from_fd(master_fd
);
1248 static void tty_serial_init(int fd
, int speed
,
1249 int parity
, int data_bits
, int stop_bits
)
1255 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1256 speed
, parity
, data_bits
, stop_bits
);
1258 tcgetattr (fd
, &tty
);
1260 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1261 speed
= speed
* 10 / 11;
1278 /* Non-Posix values follow. They may be unsupported on some systems. */
1280 check_speed(115200);
1282 check_speed(230400);
1285 check_speed(460800);
1288 check_speed(500000);
1291 check_speed(576000);
1294 check_speed(921600);
1297 check_speed(1000000);
1300 check_speed(1152000);
1303 check_speed(1500000);
1306 check_speed(2000000);
1309 check_speed(2500000);
1312 check_speed(3000000);
1315 check_speed(3500000);
1318 check_speed(4000000);
1323 cfsetispeed(&tty
, spd
);
1324 cfsetospeed(&tty
, spd
);
1326 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1327 |INLCR
|IGNCR
|ICRNL
|IXON
);
1328 tty
.c_oflag
|= OPOST
;
1329 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1330 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1351 tty
.c_cflag
|= PARENB
;
1354 tty
.c_cflag
|= PARENB
| PARODD
;
1358 tty
.c_cflag
|= CSTOPB
;
1360 tcsetattr (fd
, TCSANOW
, &tty
);
1363 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1365 FDCharDriver
*s
= chr
->opaque
;
1368 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1370 QEMUSerialSetParams
*ssp
= arg
;
1371 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1372 ssp
->speed
, ssp
->parity
,
1373 ssp
->data_bits
, ssp
->stop_bits
);
1376 case CHR_IOCTL_SERIAL_SET_BREAK
:
1378 int enable
= *(int *)arg
;
1380 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1384 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1387 int *targ
= (int *)arg
;
1388 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1390 if (sarg
& TIOCM_CTS
)
1391 *targ
|= CHR_TIOCM_CTS
;
1392 if (sarg
& TIOCM_CAR
)
1393 *targ
|= CHR_TIOCM_CAR
;
1394 if (sarg
& TIOCM_DSR
)
1395 *targ
|= CHR_TIOCM_DSR
;
1396 if (sarg
& TIOCM_RI
)
1397 *targ
|= CHR_TIOCM_RI
;
1398 if (sarg
& TIOCM_DTR
)
1399 *targ
|= CHR_TIOCM_DTR
;
1400 if (sarg
& TIOCM_RTS
)
1401 *targ
|= CHR_TIOCM_RTS
;
1404 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1406 int sarg
= *(int *)arg
;
1408 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1409 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1410 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1411 if (sarg
& CHR_TIOCM_CTS
)
1413 if (sarg
& CHR_TIOCM_CAR
)
1415 if (sarg
& CHR_TIOCM_DSR
)
1417 if (sarg
& CHR_TIOCM_RI
)
1419 if (sarg
& CHR_TIOCM_DTR
)
1421 if (sarg
& CHR_TIOCM_RTS
)
1423 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1432 static void qemu_chr_close_tty(CharDriverState
*chr
)
1434 FDCharDriver
*s
= chr
->opaque
;
1438 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1448 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1450 CharDriverState
*chr
;
1452 tty_serial_init(fd
, 115200, 'N', 8, 1);
1453 chr
= qemu_chr_open_fd(fd
, fd
);
1454 chr
->chr_ioctl
= tty_serial_ioctl
;
1455 chr
->chr_close
= qemu_chr_close_tty
;
1458 #endif /* __linux__ || __sun__ */
1460 #if defined(__linux__)
1462 #define HAVE_CHARDEV_PARPORT 1
1467 } ParallelCharDriver
;
1469 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1471 if (s
->mode
!= mode
) {
1473 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1480 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1482 ParallelCharDriver
*drv
= chr
->opaque
;
1487 case CHR_IOCTL_PP_READ_DATA
:
1488 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1490 *(uint8_t *)arg
= b
;
1492 case CHR_IOCTL_PP_WRITE_DATA
:
1493 b
= *(uint8_t *)arg
;
1494 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1497 case CHR_IOCTL_PP_READ_CONTROL
:
1498 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1500 /* Linux gives only the lowest bits, and no way to know data
1501 direction! For better compatibility set the fixed upper
1503 *(uint8_t *)arg
= b
| 0xc0;
1505 case CHR_IOCTL_PP_WRITE_CONTROL
:
1506 b
= *(uint8_t *)arg
;
1507 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1510 case CHR_IOCTL_PP_READ_STATUS
:
1511 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1513 *(uint8_t *)arg
= b
;
1515 case CHR_IOCTL_PP_DATA_DIR
:
1516 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1519 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1520 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1521 struct ParallelIOArg
*parg
= arg
;
1522 int n
= read(fd
, parg
->buffer
, parg
->count
);
1523 if (n
!= parg
->count
) {
1528 case CHR_IOCTL_PP_EPP_READ
:
1529 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1530 struct ParallelIOArg
*parg
= arg
;
1531 int n
= read(fd
, parg
->buffer
, parg
->count
);
1532 if (n
!= parg
->count
) {
1537 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1538 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1539 struct ParallelIOArg
*parg
= arg
;
1540 int n
= write(fd
, parg
->buffer
, parg
->count
);
1541 if (n
!= parg
->count
) {
1546 case CHR_IOCTL_PP_EPP_WRITE
:
1547 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1548 struct ParallelIOArg
*parg
= arg
;
1549 int n
= write(fd
, parg
->buffer
, parg
->count
);
1550 if (n
!= parg
->count
) {
1561 static void pp_close(CharDriverState
*chr
)
1563 ParallelCharDriver
*drv
= chr
->opaque
;
1566 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1567 ioctl(fd
, PPRELEASE
);
1570 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1573 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1575 CharDriverState
*chr
;
1576 ParallelCharDriver
*drv
;
1578 if (ioctl(fd
, PPCLAIM
) < 0) {
1583 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1585 drv
->mode
= IEEE1284_MODE_COMPAT
;
1587 chr
= g_malloc0(sizeof(CharDriverState
));
1588 chr
->chr_write
= null_chr_write
;
1589 chr
->chr_ioctl
= pp_ioctl
;
1590 chr
->chr_close
= pp_close
;
1595 #endif /* __linux__ */
1597 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1599 #define HAVE_CHARDEV_PARPORT 1
1601 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1603 int fd
= (int)(intptr_t)chr
->opaque
;
1607 case CHR_IOCTL_PP_READ_DATA
:
1608 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1610 *(uint8_t *)arg
= b
;
1612 case CHR_IOCTL_PP_WRITE_DATA
:
1613 b
= *(uint8_t *)arg
;
1614 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1617 case CHR_IOCTL_PP_READ_CONTROL
:
1618 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1620 *(uint8_t *)arg
= b
;
1622 case CHR_IOCTL_PP_WRITE_CONTROL
:
1623 b
= *(uint8_t *)arg
;
1624 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1627 case CHR_IOCTL_PP_READ_STATUS
:
1628 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1630 *(uint8_t *)arg
= b
;
1638 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1640 CharDriverState
*chr
;
1642 chr
= g_malloc0(sizeof(CharDriverState
));
1643 chr
->opaque
= (void *)(intptr_t)fd
;
1644 chr
->chr_write
= null_chr_write
;
1645 chr
->chr_ioctl
= pp_ioctl
;
1646 chr
->explicit_be_open
= true;
1655 HANDLE hcom
, hrecv
, hsend
;
1656 OVERLAPPED orecv
, osend
;
1663 HANDLE hInputReadyEvent
;
1664 HANDLE hInputDoneEvent
;
1665 HANDLE hInputThread
;
1666 uint8_t win_stdio_buf
;
1667 } WinStdioCharState
;
1669 #define NSENDBUF 2048
1670 #define NRECVBUF 2048
1671 #define MAXCONNECT 1
1672 #define NTIMEOUT 5000
1674 static int win_chr_poll(void *opaque
);
1675 static int win_chr_pipe_poll(void *opaque
);
1677 static void win_chr_close(CharDriverState
*chr
)
1679 WinCharState
*s
= chr
->opaque
;
1682 CloseHandle(s
->hsend
);
1686 CloseHandle(s
->hrecv
);
1690 CloseHandle(s
->hcom
);
1694 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1696 qemu_del_polling_cb(win_chr_poll
, chr
);
1698 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1701 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1703 WinCharState
*s
= chr
->opaque
;
1705 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1710 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1712 fprintf(stderr
, "Failed CreateEvent\n");
1715 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1717 fprintf(stderr
, "Failed CreateEvent\n");
1721 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1722 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1723 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1724 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1729 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1730 fprintf(stderr
, "Failed SetupComm\n");
1734 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1735 size
= sizeof(COMMCONFIG
);
1736 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1737 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1738 CommConfigDialog(filename
, NULL
, &comcfg
);
1740 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1741 fprintf(stderr
, "Failed SetCommState\n");
1745 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1746 fprintf(stderr
, "Failed SetCommMask\n");
1750 cto
.ReadIntervalTimeout
= MAXDWORD
;
1751 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1752 fprintf(stderr
, "Failed SetCommTimeouts\n");
1756 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1757 fprintf(stderr
, "Failed ClearCommError\n");
1760 qemu_add_polling_cb(win_chr_poll
, chr
);
1768 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1770 WinCharState
*s
= chr
->opaque
;
1771 DWORD len
, ret
, size
, err
;
1774 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1775 s
->osend
.hEvent
= s
->hsend
;
1778 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1780 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1782 err
= GetLastError();
1783 if (err
== ERROR_IO_PENDING
) {
1784 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1802 static int win_chr_read_poll(CharDriverState
*chr
)
1804 WinCharState
*s
= chr
->opaque
;
1806 s
->max_size
= qemu_chr_be_can_write(chr
);
1810 static void win_chr_readfile(CharDriverState
*chr
)
1812 WinCharState
*s
= chr
->opaque
;
1814 uint8_t buf
[READ_BUF_LEN
];
1817 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1818 s
->orecv
.hEvent
= s
->hrecv
;
1819 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1821 err
= GetLastError();
1822 if (err
== ERROR_IO_PENDING
) {
1823 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1828 qemu_chr_be_write(chr
, buf
, size
);
1832 static void win_chr_read(CharDriverState
*chr
)
1834 WinCharState
*s
= chr
->opaque
;
1836 if (s
->len
> s
->max_size
)
1837 s
->len
= s
->max_size
;
1841 win_chr_readfile(chr
);
1844 static int win_chr_poll(void *opaque
)
1846 CharDriverState
*chr
= opaque
;
1847 WinCharState
*s
= chr
->opaque
;
1851 ClearCommError(s
->hcom
, &comerr
, &status
);
1852 if (status
.cbInQue
> 0) {
1853 s
->len
= status
.cbInQue
;
1854 win_chr_read_poll(chr
);
1861 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1863 CharDriverState
*chr
;
1866 chr
= g_malloc0(sizeof(CharDriverState
));
1867 s
= g_malloc0(sizeof(WinCharState
));
1869 chr
->chr_write
= win_chr_write
;
1870 chr
->chr_close
= win_chr_close
;
1872 if (win_chr_init(chr
, filename
) < 0) {
1880 static int win_chr_pipe_poll(void *opaque
)
1882 CharDriverState
*chr
= opaque
;
1883 WinCharState
*s
= chr
->opaque
;
1886 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1889 win_chr_read_poll(chr
);
1896 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1898 WinCharState
*s
= chr
->opaque
;
1906 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1908 fprintf(stderr
, "Failed CreateEvent\n");
1911 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1913 fprintf(stderr
, "Failed CreateEvent\n");
1917 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1918 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1919 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1921 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1922 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1923 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1928 ZeroMemory(&ov
, sizeof(ov
));
1929 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1930 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1932 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1936 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1938 fprintf(stderr
, "Failed GetOverlappedResult\n");
1940 CloseHandle(ov
.hEvent
);
1947 CloseHandle(ov
.hEvent
);
1950 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1959 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1961 const char *filename
= opts
->device
;
1962 CharDriverState
*chr
;
1965 chr
= g_malloc0(sizeof(CharDriverState
));
1966 s
= g_malloc0(sizeof(WinCharState
));
1968 chr
->chr_write
= win_chr_write
;
1969 chr
->chr_close
= win_chr_close
;
1971 if (win_chr_pipe_init(chr
, filename
) < 0) {
1979 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1981 CharDriverState
*chr
;
1984 chr
= g_malloc0(sizeof(CharDriverState
));
1985 s
= g_malloc0(sizeof(WinCharState
));
1988 chr
->chr_write
= win_chr_write
;
1992 static CharDriverState
*qemu_chr_open_win_con(void)
1994 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1997 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1999 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2006 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2016 static void win_stdio_wait_func(void *opaque
)
2018 CharDriverState
*chr
= opaque
;
2019 WinStdioCharState
*stdio
= chr
->opaque
;
2020 INPUT_RECORD buf
[4];
2025 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
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 */
2204 uint8_t buf
[READ_BUF_LEN
];
2210 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2212 NetCharDriver
*s
= chr
->opaque
;
2213 gsize bytes_written
;
2216 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2217 if (status
== G_IO_STATUS_EOF
) {
2219 } else if (status
!= G_IO_STATUS_NORMAL
) {
2223 return bytes_written
;
2226 static int udp_chr_read_poll(void *opaque
)
2228 CharDriverState
*chr
= opaque
;
2229 NetCharDriver
*s
= chr
->opaque
;
2231 s
->max_size
= qemu_chr_be_can_write(chr
);
2233 /* If there were any stray characters in the queue process them
2236 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2237 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2239 s
->max_size
= qemu_chr_be_can_write(chr
);
2244 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2246 CharDriverState
*chr
= opaque
;
2247 NetCharDriver
*s
= chr
->opaque
;
2248 gsize bytes_read
= 0;
2251 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
) {
2259 remove_fd_in_watch(chr
);
2264 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2265 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2267 s
->max_size
= qemu_chr_be_can_write(chr
);
2273 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2275 NetCharDriver
*s
= chr
->opaque
;
2277 remove_fd_in_watch(chr
);
2279 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2284 static void udp_chr_close(CharDriverState
*chr
)
2286 NetCharDriver
*s
= chr
->opaque
;
2288 remove_fd_in_watch(chr
);
2290 g_io_channel_unref(s
->chan
);
2294 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2297 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2299 CharDriverState
*chr
= NULL
;
2300 NetCharDriver
*s
= NULL
;
2302 chr
= g_malloc0(sizeof(CharDriverState
));
2303 s
= g_malloc0(sizeof(NetCharDriver
));
2306 s
->chan
= io_channel_from_socket(s
->fd
);
2310 chr
->chr_write
= udp_chr_write
;
2311 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2312 chr
->chr_close
= udp_chr_close
;
2313 /* be isn't opened until we get a connection */
2314 chr
->explicit_be_open
= true;
2318 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2320 Error
*local_err
= NULL
;
2323 fd
= inet_dgram_opts(opts
, &local_err
);
2325 qerror_report_err(local_err
);
2326 error_free(local_err
);
2329 return qemu_chr_open_udp_fd(fd
);
2332 /***********************************************************/
2333 /* TCP Net console */
2337 GIOChannel
*chan
, *listen_chan
;
2346 int read_msgfds_num
;
2348 int write_msgfds_num
;
2351 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2354 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2356 TCPCharDriver
*s
= chr
->opaque
;
2361 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2362 char control
[CMSG_SPACE(fd_size
)];
2363 struct cmsghdr
*cmsg
;
2365 memset(&msgh
, 0, sizeof(msgh
));
2366 memset(control
, 0, sizeof(control
));
2368 /* set the payload */
2369 iov
.iov_base
= (uint8_t *) buf
;
2372 msgh
.msg_iov
= &iov
;
2373 msgh
.msg_iovlen
= 1;
2375 msgh
.msg_control
= control
;
2376 msgh
.msg_controllen
= sizeof(control
);
2378 cmsg
= CMSG_FIRSTHDR(&msgh
);
2380 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2381 cmsg
->cmsg_level
= SOL_SOCKET
;
2382 cmsg
->cmsg_type
= SCM_RIGHTS
;
2383 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2386 r
= sendmsg(s
->fd
, &msgh
, 0);
2387 } while (r
< 0 && errno
== EINTR
);
2389 /* free the written msgfds, no matter what */
2390 if (s
->write_msgfds_num
) {
2391 g_free(s
->write_msgfds
);
2392 s
->write_msgfds
= 0;
2393 s
->write_msgfds_num
= 0;
2400 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2402 TCPCharDriver
*s
= chr
->opaque
;
2405 if (s
->is_unix
&& s
->write_msgfds_num
) {
2406 return unix_send_msgfds(chr
, buf
, len
);
2410 return io_channel_send(s
->chan
, buf
, len
);
2413 /* XXX: indicate an error ? */
2418 static int tcp_chr_read_poll(void *opaque
)
2420 CharDriverState
*chr
= opaque
;
2421 TCPCharDriver
*s
= chr
->opaque
;
2424 s
->max_size
= qemu_chr_be_can_write(chr
);
2429 #define IAC_BREAK 243
2430 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2432 uint8_t *buf
, int *size
)
2434 /* Handle any telnet client's basic IAC options to satisfy char by
2435 * char mode with no echo. All IAC options will be removed from
2436 * the buf and the do_telnetopt variable will be used to track the
2437 * state of the width of the IAC information.
2439 * IAC commands come in sets of 3 bytes with the exception of the
2440 * "IAC BREAK" command and the double IAC.
2446 for (i
= 0; i
< *size
; i
++) {
2447 if (s
->do_telnetopt
> 1) {
2448 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2449 /* Double IAC means send an IAC */
2453 s
->do_telnetopt
= 1;
2455 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2456 /* Handle IAC break commands by sending a serial break */
2457 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2462 if (s
->do_telnetopt
>= 4) {
2463 s
->do_telnetopt
= 1;
2466 if ((unsigned char)buf
[i
] == IAC
) {
2467 s
->do_telnetopt
= 2;
2478 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2480 TCPCharDriver
*s
= chr
->opaque
;
2481 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2486 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2488 /* Close unused fds */
2489 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
2490 close(s
->read_msgfds
[i
]);
2493 g_free(s
->read_msgfds
);
2495 s
->read_msgfds_num
= 0;
2501 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2503 TCPCharDriver
*s
= chr
->opaque
;
2505 /* clear old pending fd array */
2506 if (s
->write_msgfds
) {
2507 g_free(s
->write_msgfds
);
2511 s
->write_msgfds
= g_malloc(num
* sizeof(int));
2512 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2515 s
->write_msgfds_num
= num
;
2521 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2523 TCPCharDriver
*s
= chr
->opaque
;
2524 struct cmsghdr
*cmsg
;
2526 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2529 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2530 cmsg
->cmsg_level
!= SOL_SOCKET
||
2531 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2535 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2541 /* close and clean read_msgfds */
2542 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2543 close(s
->read_msgfds
[i
]);
2546 if (s
->read_msgfds_num
) {
2547 g_free(s
->read_msgfds
);
2550 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2551 s
->read_msgfds
= g_malloc(fd_size
);
2552 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2554 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2555 int fd
= s
->read_msgfds
[i
];
2560 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2563 #ifndef MSG_CMSG_CLOEXEC
2564 qemu_set_cloexec(fd
);
2570 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2572 TCPCharDriver
*s
= chr
->opaque
;
2573 struct msghdr msg
= { NULL
, };
2574 struct iovec iov
[1];
2576 struct cmsghdr cmsg
;
2577 char control
[CMSG_SPACE(sizeof(int))];
2582 iov
[0].iov_base
= buf
;
2583 iov
[0].iov_len
= len
;
2587 msg
.msg_control
= &msg_control
;
2588 msg
.msg_controllen
= sizeof(msg_control
);
2590 #ifdef MSG_CMSG_CLOEXEC
2591 flags
|= MSG_CMSG_CLOEXEC
;
2593 ret
= recvmsg(s
->fd
, &msg
, flags
);
2594 if (ret
> 0 && s
->is_unix
) {
2595 unix_process_msgfd(chr
, &msg
);
2601 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2603 TCPCharDriver
*s
= chr
->opaque
;
2604 return qemu_recv(s
->fd
, buf
, len
, 0);
2608 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2610 TCPCharDriver
*s
= chr
->opaque
;
2611 return g_io_create_watch(s
->chan
, cond
);
2614 static void tcp_chr_disconnect(CharDriverState
*chr
)
2616 TCPCharDriver
*s
= chr
->opaque
;
2619 if (s
->listen_chan
) {
2620 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2621 tcp_chr_accept
, chr
);
2623 remove_fd_in_watch(chr
);
2624 g_io_channel_unref(s
->chan
);
2628 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2631 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2633 CharDriverState
*chr
= opaque
;
2634 TCPCharDriver
*s
= chr
->opaque
;
2635 uint8_t buf
[READ_BUF_LEN
];
2638 if (!s
->connected
|| s
->max_size
<= 0) {
2642 if (len
> s
->max_size
)
2644 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2646 /* connection closed */
2647 tcp_chr_disconnect(chr
);
2648 } else if (size
> 0) {
2649 if (s
->do_telnetopt
)
2650 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2652 qemu_chr_be_write(chr
, buf
, size
);
2658 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2660 TCPCharDriver
*s
= chr
->opaque
;
2663 if (!s
->connected
) {
2667 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2669 /* connection closed */
2670 tcp_chr_disconnect(chr
);
2677 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2679 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2682 chr
->avail_connections
= 1;
2689 static gboolean
tcp_chr_chan_close(GIOChannel
*channel
, GIOCondition cond
,
2692 CharDriverState
*chr
= opaque
;
2694 if (cond
!= G_IO_HUP
) {
2698 /* connection closed */
2699 tcp_chr_disconnect(chr
);
2700 if (chr
->fd_hup_tag
) {
2701 g_source_remove(chr
->fd_hup_tag
);
2702 chr
->fd_hup_tag
= 0;
2708 static void tcp_chr_connect(void *opaque
)
2710 CharDriverState
*chr
= opaque
;
2711 TCPCharDriver
*s
= chr
->opaque
;
2715 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2717 chr
->fd_hup_tag
= g_io_add_watch(s
->chan
, G_IO_HUP
, tcp_chr_chan_close
,
2720 qemu_chr_be_generic_open(chr
);
2723 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2725 TCPCharDriver
*s
= chr
->opaque
;
2727 remove_fd_in_watch(chr
);
2729 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2734 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2735 static void tcp_chr_telnet_init(int fd
)
2738 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2739 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2740 send(fd
, (char *)buf
, 3, 0);
2741 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2742 send(fd
, (char *)buf
, 3, 0);
2743 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2744 send(fd
, (char *)buf
, 3, 0);
2745 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2746 send(fd
, (char *)buf
, 3, 0);
2749 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2751 TCPCharDriver
*s
= chr
->opaque
;
2755 qemu_set_nonblock(fd
);
2757 socket_set_nodelay(fd
);
2759 s
->chan
= io_channel_from_socket(fd
);
2760 if (s
->listen_tag
) {
2761 g_source_remove(s
->listen_tag
);
2764 tcp_chr_connect(chr
);
2769 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2771 CharDriverState
*chr
= opaque
;
2772 TCPCharDriver
*s
= chr
->opaque
;
2773 struct sockaddr_in saddr
;
2775 struct sockaddr_un uaddr
;
2777 struct sockaddr
*addr
;
2784 len
= sizeof(uaddr
);
2785 addr
= (struct sockaddr
*)&uaddr
;
2789 len
= sizeof(saddr
);
2790 addr
= (struct sockaddr
*)&saddr
;
2792 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2793 if (fd
< 0 && errno
!= EINTR
) {
2796 } else if (fd
>= 0) {
2797 if (s
->do_telnetopt
)
2798 tcp_chr_telnet_init(fd
);
2802 if (tcp_chr_add_client(chr
, fd
) < 0)
2808 static void tcp_chr_close(CharDriverState
*chr
)
2810 TCPCharDriver
*s
= chr
->opaque
;
2813 remove_fd_in_watch(chr
);
2815 g_io_channel_unref(s
->chan
);
2819 if (s
->listen_fd
>= 0) {
2820 if (s
->listen_tag
) {
2821 g_source_remove(s
->listen_tag
);
2824 if (s
->listen_chan
) {
2825 g_io_channel_unref(s
->listen_chan
);
2827 closesocket(s
->listen_fd
);
2829 if (s
->read_msgfds_num
) {
2830 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2831 close(s
->read_msgfds
[i
]);
2833 g_free(s
->read_msgfds
);
2835 if (s
->write_msgfds_num
) {
2836 g_free(s
->write_msgfds
);
2839 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2842 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2843 bool is_listen
, bool is_telnet
,
2844 bool is_waitconnect
,
2847 CharDriverState
*chr
= NULL
;
2848 TCPCharDriver
*s
= NULL
;
2849 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2850 const char *left
= "", *right
= "";
2851 struct sockaddr_storage ss
;
2852 socklen_t ss_len
= sizeof(ss
);
2854 memset(&ss
, 0, ss_len
);
2855 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2856 error_setg_errno(errp
, errno
, "getsockname");
2860 chr
= g_malloc0(sizeof(CharDriverState
));
2861 s
= g_malloc0(sizeof(TCPCharDriver
));
2867 s
->read_msgfds_num
= 0;
2868 s
->write_msgfds
= 0;
2869 s
->write_msgfds_num
= 0;
2871 chr
->filename
= g_malloc(256);
2872 switch (ss
.ss_family
) {
2876 snprintf(chr
->filename
, 256, "unix:%s%s",
2877 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2878 is_listen
? ",server" : "");
2886 s
->do_nodelay
= do_nodelay
;
2887 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2888 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2889 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2890 is_telnet
? "telnet" : "tcp",
2891 left
, host
, right
, serv
,
2892 is_listen
? ",server" : "");
2897 chr
->chr_write
= tcp_chr_write
;
2898 chr
->chr_sync_read
= tcp_chr_sync_read
;
2899 chr
->chr_close
= tcp_chr_close
;
2900 chr
->get_msgfds
= tcp_get_msgfds
;
2901 chr
->set_msgfds
= tcp_set_msgfds
;
2902 chr
->chr_add_client
= tcp_chr_add_client
;
2903 chr
->chr_add_watch
= tcp_chr_add_watch
;
2904 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
2905 /* be isn't opened until we get a connection */
2906 chr
->explicit_be_open
= true;
2910 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2911 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2913 s
->do_telnetopt
= 1;
2918 socket_set_nodelay(fd
);
2919 s
->chan
= io_channel_from_socket(s
->fd
);
2920 tcp_chr_connect(chr
);
2923 if (is_listen
&& is_waitconnect
) {
2924 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2926 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2927 qemu_set_nonblock(s
->listen_fd
);
2932 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2934 CharDriverState
*chr
= NULL
;
2935 Error
*local_err
= NULL
;
2938 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2939 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2940 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2941 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2942 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2946 fd
= unix_listen_opts(opts
, &local_err
);
2948 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2952 fd
= inet_listen_opts(opts
, 0, &local_err
);
2954 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2961 if (!is_waitconnect
)
2962 qemu_set_nonblock(fd
);
2964 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2965 is_waitconnect
, &local_err
);
2974 qerror_report_err(local_err
);
2975 error_free(local_err
);
2981 g_free(chr
->opaque
);
2987 /*********************************************************/
2988 /* Ring buffer chardev */
2995 } RingBufCharDriver
;
2997 static size_t ringbuf_count(const CharDriverState
*chr
)
2999 const RingBufCharDriver
*d
= chr
->opaque
;
3001 return d
->prod
- d
->cons
;
3004 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3006 RingBufCharDriver
*d
= chr
->opaque
;
3009 if (!buf
|| (len
< 0)) {
3013 for (i
= 0; i
< len
; i
++ ) {
3014 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3015 if (d
->prod
- d
->cons
> d
->size
) {
3016 d
->cons
= d
->prod
- d
->size
;
3023 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3025 RingBufCharDriver
*d
= chr
->opaque
;
3028 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3029 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3035 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3037 RingBufCharDriver
*d
= chr
->opaque
;
3044 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3047 CharDriverState
*chr
;
3048 RingBufCharDriver
*d
;
3050 chr
= g_malloc0(sizeof(CharDriverState
));
3051 d
= g_malloc(sizeof(*d
));
3053 d
->size
= opts
->has_size
? opts
->size
: 65536;
3055 /* The size must be power of 2 */
3056 if (d
->size
& (d
->size
- 1)) {
3057 error_setg(errp
, "size of ringbuf chardev must be power of two");
3063 d
->cbuf
= g_malloc0(d
->size
);
3066 chr
->chr_write
= ringbuf_chr_write
;
3067 chr
->chr_close
= ringbuf_chr_close
;
3077 bool chr_is_ringbuf(const CharDriverState
*chr
)
3079 return chr
->chr_write
== ringbuf_chr_write
;
3082 void qmp_ringbuf_write(const char *device
, const char *data
,
3083 bool has_format
, enum DataFormat format
,
3086 CharDriverState
*chr
;
3087 const uint8_t *write_data
;
3091 chr
= qemu_chr_find(device
);
3093 error_setg(errp
, "Device '%s' not found", device
);
3097 if (!chr_is_ringbuf(chr
)) {
3098 error_setg(errp
,"%s is not a ringbuf device", device
);
3102 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3103 write_data
= g_base64_decode(data
, &write_count
);
3105 write_data
= (uint8_t *)data
;
3106 write_count
= strlen(data
);
3109 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3111 if (write_data
!= (uint8_t *)data
) {
3112 g_free((void *)write_data
);
3116 error_setg(errp
, "Failed to write to device %s", device
);
3121 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3122 bool has_format
, enum DataFormat format
,
3125 CharDriverState
*chr
;
3130 chr
= qemu_chr_find(device
);
3132 error_setg(errp
, "Device '%s' not found", device
);
3136 if (!chr_is_ringbuf(chr
)) {
3137 error_setg(errp
,"%s is not a ringbuf device", device
);
3142 error_setg(errp
, "size must be greater than zero");
3146 count
= ringbuf_count(chr
);
3147 size
= size
> count
? count
: size
;
3148 read_data
= g_malloc(size
+ 1);
3150 ringbuf_chr_read(chr
, read_data
, size
);
3152 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3153 data
= g_base64_encode(read_data
, size
);
3157 * FIXME should read only complete, valid UTF-8 characters up
3158 * to @size bytes. Invalid sequences should be replaced by a
3159 * suitable replacement character. Except when (and only
3160 * when) ring buffer lost characters since last read, initial
3161 * continuation characters should be dropped.
3163 read_data
[size
] = 0;
3164 data
= (char *)read_data
;
3170 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3172 char host
[65], port
[33], width
[8], height
[8];
3176 Error
*local_err
= NULL
;
3178 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3180 qerror_report_err(local_err
);
3181 error_free(local_err
);
3185 if (strstart(filename
, "mon:", &p
)) {
3187 qemu_opt_set(opts
, "mux", "on");
3188 if (strcmp(filename
, "stdio") == 0) {
3189 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3190 * but pass it to the guest. Handle this only for compat syntax,
3191 * for -chardev syntax we have special option for this.
3192 * This is what -nographic did, redirecting+muxing serial+monitor
3193 * to stdio causing Ctrl+C to be passed to guest. */
3194 qemu_opt_set(opts
, "signal", "off");
3198 if (strcmp(filename
, "null") == 0 ||
3199 strcmp(filename
, "pty") == 0 ||
3200 strcmp(filename
, "msmouse") == 0 ||
3201 strcmp(filename
, "braille") == 0 ||
3202 strcmp(filename
, "stdio") == 0) {
3203 qemu_opt_set(opts
, "backend", filename
);
3206 if (strstart(filename
, "vc", &p
)) {
3207 qemu_opt_set(opts
, "backend", "vc");
3209 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3211 qemu_opt_set(opts
, "width", width
);
3212 qemu_opt_set(opts
, "height", height
);
3213 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3215 qemu_opt_set(opts
, "cols", width
);
3216 qemu_opt_set(opts
, "rows", height
);
3223 if (strcmp(filename
, "con:") == 0) {
3224 qemu_opt_set(opts
, "backend", "console");
3227 if (strstart(filename
, "COM", NULL
)) {
3228 qemu_opt_set(opts
, "backend", "serial");
3229 qemu_opt_set(opts
, "path", filename
);
3232 if (strstart(filename
, "file:", &p
)) {
3233 qemu_opt_set(opts
, "backend", "file");
3234 qemu_opt_set(opts
, "path", p
);
3237 if (strstart(filename
, "pipe:", &p
)) {
3238 qemu_opt_set(opts
, "backend", "pipe");
3239 qemu_opt_set(opts
, "path", p
);
3242 if (strstart(filename
, "tcp:", &p
) ||
3243 strstart(filename
, "telnet:", &p
)) {
3244 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3246 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3249 qemu_opt_set(opts
, "backend", "socket");
3250 qemu_opt_set(opts
, "host", host
);
3251 qemu_opt_set(opts
, "port", port
);
3252 if (p
[pos
] == ',') {
3253 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3256 if (strstart(filename
, "telnet:", &p
))
3257 qemu_opt_set(opts
, "telnet", "on");
3260 if (strstart(filename
, "udp:", &p
)) {
3261 qemu_opt_set(opts
, "backend", "udp");
3262 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3264 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3268 qemu_opt_set(opts
, "host", host
);
3269 qemu_opt_set(opts
, "port", port
);
3270 if (p
[pos
] == '@') {
3272 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3274 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3278 qemu_opt_set(opts
, "localaddr", host
);
3279 qemu_opt_set(opts
, "localport", port
);
3283 if (strstart(filename
, "unix:", &p
)) {
3284 qemu_opt_set(opts
, "backend", "socket");
3285 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3289 if (strstart(filename
, "/dev/parport", NULL
) ||
3290 strstart(filename
, "/dev/ppi", NULL
)) {
3291 qemu_opt_set(opts
, "backend", "parport");
3292 qemu_opt_set(opts
, "path", filename
);
3295 if (strstart(filename
, "/dev/", NULL
)) {
3296 qemu_opt_set(opts
, "backend", "tty");
3297 qemu_opt_set(opts
, "path", filename
);
3302 qemu_opts_del(opts
);
3306 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3309 const char *path
= qemu_opt_get(opts
, "path");
3312 error_setg(errp
, "chardev: file: no filename given");
3315 backend
->file
= g_new0(ChardevFile
, 1);
3316 backend
->file
->out
= g_strdup(path
);
3319 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3322 backend
->stdio
= g_new0(ChardevStdio
, 1);
3323 backend
->stdio
->has_signal
= true;
3324 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3327 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3330 const char *device
= qemu_opt_get(opts
, "path");
3332 if (device
== NULL
) {
3333 error_setg(errp
, "chardev: serial/tty: no device path given");
3336 backend
->serial
= g_new0(ChardevHostdev
, 1);
3337 backend
->serial
->device
= g_strdup(device
);
3340 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3343 const char *device
= qemu_opt_get(opts
, "path");
3345 if (device
== NULL
) {
3346 error_setg(errp
, "chardev: parallel: no device path given");
3349 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3350 backend
->parallel
->device
= g_strdup(device
);
3353 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3356 const char *device
= qemu_opt_get(opts
, "path");
3358 if (device
== NULL
) {
3359 error_setg(errp
, "chardev: pipe: no device path given");
3362 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3363 backend
->pipe
->device
= g_strdup(device
);
3366 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3371 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3373 val
= qemu_opt_get_size(opts
, "size", 0);
3375 backend
->ringbuf
->has_size
= true;
3376 backend
->ringbuf
->size
= val
;
3380 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3383 const char *chardev
= qemu_opt_get(opts
, "chardev");
3385 if (chardev
== NULL
) {
3386 error_setg(errp
, "chardev: mux: no chardev given");
3389 backend
->mux
= g_new0(ChardevMux
, 1);
3390 backend
->mux
->chardev
= g_strdup(chardev
);
3393 typedef struct CharDriver
{
3396 CharDriverState
*(*open
)(QemuOpts
*opts
);
3397 /* new, qapi-based */
3398 ChardevBackendKind kind
;
3399 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3402 static GSList
*backends
;
3404 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3408 s
= g_malloc0(sizeof(*s
));
3409 s
->name
= g_strdup(name
);
3412 backends
= g_slist_append(backends
, s
);
3415 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3416 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3420 s
= g_malloc0(sizeof(*s
));
3421 s
->name
= g_strdup(name
);
3425 backends
= g_slist_append(backends
, s
);
3428 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3429 void (*init
)(struct CharDriverState
*s
),
3432 Error
*local_err
= NULL
;
3434 CharDriverState
*chr
;
3437 if (qemu_opts_id(opts
) == NULL
) {
3438 error_setg(errp
, "chardev: no id specified");
3442 if (qemu_opt_get(opts
, "backend") == NULL
) {
3443 error_setg(errp
, "chardev: \"%s\" missing backend",
3444 qemu_opts_id(opts
));
3447 for (i
= backends
; i
; i
= i
->next
) {
3450 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3455 error_setg(errp
, "chardev: backend \"%s\" not found",
3456 qemu_opt_get(opts
, "backend"));
3461 /* using new, qapi init */
3462 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3463 ChardevReturn
*ret
= NULL
;
3464 const char *id
= qemu_opts_id(opts
);
3467 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3468 bid
= g_strdup_printf("%s-base", id
);
3472 backend
->kind
= cd
->kind
;
3474 cd
->parse(opts
, backend
, &local_err
);
3476 error_propagate(errp
, local_err
);
3480 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3486 qapi_free_ChardevBackend(backend
);
3487 qapi_free_ChardevReturn(ret
);
3488 backend
= g_new0(ChardevBackend
, 1);
3489 backend
->mux
= g_new0(ChardevMux
, 1);
3490 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3491 backend
->mux
->chardev
= g_strdup(bid
);
3492 ret
= qmp_chardev_add(id
, backend
, errp
);
3494 chr
= qemu_chr_find(bid
);
3495 qemu_chr_delete(chr
);
3501 chr
= qemu_chr_find(id
);
3505 qapi_free_ChardevBackend(backend
);
3506 qapi_free_ChardevReturn(ret
);
3511 chr
= cd
->open(opts
);
3513 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3514 qemu_opt_get(opts
, "backend"));
3519 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3521 /* if we didn't create the chardev via qmp_chardev_add, we
3522 * need to send the OPENED event here
3524 if (!chr
->explicit_be_open
) {
3525 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3527 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3529 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3530 CharDriverState
*base
= chr
;
3531 int len
= strlen(qemu_opts_id(opts
)) + 6;
3532 base
->label
= g_malloc(len
);
3533 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3534 chr
= qemu_chr_open_mux(base
);
3535 chr
->filename
= base
->filename
;
3536 chr
->avail_connections
= MAX_MUX
;
3537 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3539 chr
->avail_connections
= 1;
3541 chr
->label
= g_strdup(qemu_opts_id(opts
));
3546 qemu_opts_del(opts
);
3550 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3553 CharDriverState
*chr
;
3557 if (strstart(filename
, "chardev:", &p
)) {
3558 return qemu_chr_find(p
);
3561 opts
= qemu_chr_parse_compat(label
, filename
);
3565 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3567 error_report("%s", error_get_pretty(err
));
3570 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3571 qemu_chr_fe_claim_no_fail(chr
);
3572 monitor_init(chr
, MONITOR_USE_READLINE
);
3577 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3579 if (chr
->chr_set_echo
) {
3580 chr
->chr_set_echo(chr
, echo
);
3584 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3586 if (chr
->fe_open
== fe_open
) {
3589 chr
->fe_open
= fe_open
;
3590 if (chr
->chr_set_fe_open
) {
3591 chr
->chr_set_fe_open(chr
, fe_open
);
3595 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3597 if (chr
->chr_fe_event
) {
3598 chr
->chr_fe_event(chr
, event
);
3602 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3603 GIOFunc func
, void *user_data
)
3608 if (s
->chr_add_watch
== NULL
) {
3612 src
= s
->chr_add_watch(s
, cond
);
3613 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3614 tag
= g_source_attach(src
, NULL
);
3615 g_source_unref(src
);
3620 int qemu_chr_fe_claim(CharDriverState
*s
)
3622 if (s
->avail_connections
< 1) {
3625 s
->avail_connections
--;
3629 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3631 if (qemu_chr_fe_claim(s
) != 0) {
3632 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3633 __func__
, s
->label
);
3638 void qemu_chr_fe_release(CharDriverState
*s
)
3640 s
->avail_connections
++;
3643 void qemu_chr_delete(CharDriverState
*chr
)
3645 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3646 if (chr
->chr_close
) {
3647 chr
->chr_close(chr
);
3649 g_free(chr
->filename
);
3652 qemu_opts_del(chr
->opts
);
3657 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3659 ChardevInfoList
*chr_list
= NULL
;
3660 CharDriverState
*chr
;
3662 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3663 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3664 info
->value
= g_malloc0(sizeof(*info
->value
));
3665 info
->value
->label
= g_strdup(chr
->label
);
3666 info
->value
->filename
= g_strdup(chr
->filename
);
3668 info
->next
= chr_list
;
3675 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3677 ChardevBackendInfoList
*backend_list
= NULL
;
3678 CharDriver
*c
= NULL
;
3681 for (i
= backends
; i
; i
= i
->next
) {
3682 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3684 info
->value
= g_malloc0(sizeof(*info
->value
));
3685 info
->value
->name
= g_strdup(c
->name
);
3687 info
->next
= backend_list
;
3688 backend_list
= info
;
3691 return backend_list
;
3694 CharDriverState
*qemu_chr_find(const char *name
)
3696 CharDriverState
*chr
;
3698 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3699 if (strcmp(chr
->label
, name
) != 0)
3706 /* Get a character (serial) device interface. */
3707 CharDriverState
*qemu_char_get_next_serial(void)
3709 static int next_serial
;
3710 CharDriverState
*chr
;
3712 /* FIXME: This function needs to go away: use chardev properties! */
3714 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3715 chr
= serial_hds
[next_serial
++];
3716 qemu_chr_fe_claim_no_fail(chr
);
3722 QemuOptsList qemu_chardev_opts
= {
3724 .implied_opt_name
= "backend",
3725 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3729 .type
= QEMU_OPT_STRING
,
3732 .type
= QEMU_OPT_STRING
,
3735 .type
= QEMU_OPT_STRING
,
3738 .type
= QEMU_OPT_STRING
,
3740 .name
= "localaddr",
3741 .type
= QEMU_OPT_STRING
,
3743 .name
= "localport",
3744 .type
= QEMU_OPT_STRING
,
3747 .type
= QEMU_OPT_NUMBER
,
3750 .type
= QEMU_OPT_BOOL
,
3753 .type
= QEMU_OPT_BOOL
,
3756 .type
= QEMU_OPT_BOOL
,
3759 .type
= QEMU_OPT_BOOL
,
3762 .type
= QEMU_OPT_BOOL
,
3765 .type
= QEMU_OPT_BOOL
,
3768 .type
= QEMU_OPT_NUMBER
,
3771 .type
= QEMU_OPT_NUMBER
,
3774 .type
= QEMU_OPT_NUMBER
,
3777 .type
= QEMU_OPT_NUMBER
,
3780 .type
= QEMU_OPT_BOOL
,
3783 .type
= QEMU_OPT_BOOL
,
3786 .type
= QEMU_OPT_STRING
,
3789 .type
= QEMU_OPT_NUMBER
,
3792 .type
= QEMU_OPT_SIZE
,
3795 .type
= QEMU_OPT_STRING
,
3797 { /* end of list */ }
3803 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3808 error_setg(errp
, "input file not supported");
3812 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3813 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3814 if (out
== INVALID_HANDLE_VALUE
) {
3815 error_setg(errp
, "open %s failed", file
->out
);
3818 return qemu_chr_open_win_file(out
);
3821 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3824 return qemu_chr_open_win_path(serial
->device
);
3827 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3830 error_setg(errp
, "character device backend type 'parallel' not supported");
3836 static int qmp_chardev_open_file_source(char *src
, int flags
,
3841 TFR(fd
= qemu_open(src
, flags
, 0666));
3843 error_setg_file_open(errp
, errno
, src
);
3848 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3850 int flags
, in
= -1, out
;
3852 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3853 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3860 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3867 return qemu_chr_open_fd(in
, out
);
3870 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3873 #ifdef HAVE_CHARDEV_TTY
3876 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3880 qemu_set_nonblock(fd
);
3881 return qemu_chr_open_tty_fd(fd
);
3883 error_setg(errp
, "character device backend type 'serial' not supported");
3888 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3891 #ifdef HAVE_CHARDEV_PARPORT
3894 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3898 return qemu_chr_open_pp_fd(fd
);
3900 error_setg(errp
, "character device backend type 'parallel' not supported");
3907 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3910 SocketAddress
*addr
= sock
->addr
;
3911 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3912 bool is_listen
= sock
->has_server
? sock
->server
: true;
3913 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3914 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3918 fd
= socket_listen(addr
, errp
);
3920 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3925 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3926 is_telnet
, is_waitconnect
, errp
);
3929 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3934 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3938 return qemu_chr_open_udp_fd(fd
);
3941 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3944 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3945 CharDriverState
*base
, *chr
= NULL
;
3947 chr
= qemu_chr_find(id
);
3949 error_setg(errp
, "Chardev '%s' already exists", id
);
3954 switch (backend
->kind
) {
3955 case CHARDEV_BACKEND_KIND_FILE
:
3956 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3958 case CHARDEV_BACKEND_KIND_SERIAL
:
3959 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3961 case CHARDEV_BACKEND_KIND_PARALLEL
:
3962 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3964 case CHARDEV_BACKEND_KIND_PIPE
:
3965 chr
= qemu_chr_open_pipe(backend
->pipe
);
3967 case CHARDEV_BACKEND_KIND_SOCKET
:
3968 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3970 case CHARDEV_BACKEND_KIND_UDP
:
3971 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
3973 #ifdef HAVE_CHARDEV_TTY
3974 case CHARDEV_BACKEND_KIND_PTY
:
3975 chr
= qemu_chr_open_pty(id
, ret
);
3978 case CHARDEV_BACKEND_KIND_NULL
:
3979 chr
= qemu_chr_open_null();
3981 case CHARDEV_BACKEND_KIND_MUX
:
3982 base
= qemu_chr_find(backend
->mux
->chardev
);
3984 error_setg(errp
, "mux: base chardev %s not found",
3985 backend
->mux
->chardev
);
3988 chr
= qemu_chr_open_mux(base
);
3990 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3991 chr
= qemu_chr_open_msmouse();
3993 #ifdef CONFIG_BRLAPI
3994 case CHARDEV_BACKEND_KIND_BRAILLE
:
3995 chr
= chr_baum_init();
3998 case CHARDEV_BACKEND_KIND_STDIO
:
3999 chr
= qemu_chr_open_stdio(backend
->stdio
);
4002 case CHARDEV_BACKEND_KIND_CONSOLE
:
4003 chr
= qemu_chr_open_win_con();
4007 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4008 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
4010 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4011 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
4014 case CHARDEV_BACKEND_KIND_VC
:
4015 chr
= vc_init(backend
->vc
);
4017 case CHARDEV_BACKEND_KIND_RINGBUF
:
4018 case CHARDEV_BACKEND_KIND_MEMORY
:
4019 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
4022 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4027 * Character backend open hasn't been fully converted to the Error
4028 * API. Some opens fail without setting an error. Set a generic
4030 * TODO full conversion to Error API
4032 if (chr
== NULL
&& errp
&& !*errp
) {
4033 error_setg(errp
, "Failed to create chardev");
4036 chr
->label
= g_strdup(id
);
4037 chr
->avail_connections
=
4038 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4039 if (!chr
->filename
) {
4040 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4042 if (!chr
->explicit_be_open
) {
4043 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4045 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4053 void qmp_chardev_remove(const char *id
, Error
**errp
)
4055 CharDriverState
*chr
;
4057 chr
= qemu_chr_find(id
);
4059 error_setg(errp
, "Chardev '%s' not found", id
);
4062 if (chr
->chr_can_read
|| chr
->chr_read
||
4063 chr
->chr_event
|| chr
->handler_opaque
) {
4064 error_setg(errp
, "Chardev '%s' is busy", id
);
4067 qemu_chr_delete(chr
);
4070 static void register_types(void)
4072 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
4073 register_char_driver("socket", qemu_chr_open_socket
);
4074 register_char_driver("udp", qemu_chr_open_udp
);
4075 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4076 qemu_chr_parse_ringbuf
);
4077 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
4078 qemu_chr_parse_file_out
);
4079 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4080 qemu_chr_parse_stdio
);
4081 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4082 qemu_chr_parse_serial
);
4083 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4084 qemu_chr_parse_serial
);
4085 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4086 qemu_chr_parse_parallel
);
4087 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4088 qemu_chr_parse_parallel
);
4089 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
4090 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
4091 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4092 qemu_chr_parse_pipe
);
4093 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
4094 qemu_chr_parse_mux
);
4095 /* Bug-compatibility: */
4096 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4097 qemu_chr_parse_ringbuf
);
4098 /* this must be done after machine init, since we register FEs with muxes
4099 * as part of realize functions like serial_isa_realizefn when -nographic
4102 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4105 type_init(register_types
);