4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "ui/console.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/timer.h"
29 #include "sysemu/char.h"
31 #include "qmp-commands.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <arpa/inet.h>
53 #include <sys/select.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57 #include <dev/ppbus/ppi.h>
58 #include <dev/ppbus/ppbconf.h>
59 #elif defined(__DragonFly__)
60 #include <dev/misc/ppi/ppi.h>
61 #include <bus/ppbus/ppbconf.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
83 #include "qemu/sockets.h"
84 #include "ui/qemu-spice.h"
86 #define READ_BUF_LEN 4096
87 #define READ_RETRIES 10
89 /***********************************************************/
90 /* character device */
92 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
93 QTAILQ_HEAD_INITIALIZER(chardevs
);
95 void qemu_chr_be_event(CharDriverState
*s
, int event
)
97 /* Keep track if the char device is open */
99 case CHR_EVENT_OPENED
:
102 case CHR_EVENT_CLOSED
:
109 s
->chr_event(s
->handler_opaque
, event
);
112 void qemu_chr_be_generic_open(CharDriverState
*s
)
114 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
117 int qemu_chr_fe_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
119 return s
->chr_write(s
, buf
, len
);
122 int qemu_chr_fe_write_all(CharDriverState
*s
, const uint8_t *buf
, int len
)
127 while (offset
< len
) {
129 res
= s
->chr_write(s
, buf
+ offset
, len
- offset
);
130 if (res
== -1 && errno
== EAGAIN
) {
133 } while (res
== -1 && errno
== EAGAIN
);
149 int qemu_chr_fe_read_all(CharDriverState
*s
, uint8_t *buf
, int len
)
151 int offset
= 0, counter
= 10;
154 if (!s
->chr_sync_read
) {
158 while (offset
< len
) {
160 res
= s
->chr_sync_read(s
, buf
+ offset
, len
- offset
);
161 if (res
== -1 && errno
== EAGAIN
) {
164 } while (res
== -1 && errno
== EAGAIN
);
184 int qemu_chr_fe_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
188 return s
->chr_ioctl(s
, cmd
, arg
);
191 int qemu_chr_be_can_write(CharDriverState
*s
)
193 if (!s
->chr_can_read
)
195 return s
->chr_can_read(s
->handler_opaque
);
198 void qemu_chr_be_write(CharDriverState
*s
, uint8_t *buf
, int len
)
201 s
->chr_read(s
->handler_opaque
, buf
, len
);
205 int qemu_chr_fe_get_msgfd(CharDriverState
*s
)
208 return (qemu_chr_fe_get_msgfds(s
, &fd
, 1) >= 0) ? fd
: -1;
211 int qemu_chr_fe_get_msgfds(CharDriverState
*s
, int *fds
, int len
)
213 return s
->get_msgfds
? s
->get_msgfds(s
, fds
, len
) : -1;
216 int qemu_chr_fe_set_msgfds(CharDriverState
*s
, int *fds
, int num
)
218 return s
->set_msgfds
? s
->set_msgfds(s
, fds
, num
) : -1;
221 int qemu_chr_add_client(CharDriverState
*s
, int fd
)
223 return s
->chr_add_client
? s
->chr_add_client(s
, fd
) : -1;
226 void qemu_chr_accept_input(CharDriverState
*s
)
228 if (s
->chr_accept_input
)
229 s
->chr_accept_input(s
);
233 void qemu_chr_fe_printf(CharDriverState
*s
, const char *fmt
, ...)
235 char buf
[READ_BUF_LEN
];
238 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
239 qemu_chr_fe_write(s
, (uint8_t *)buf
, strlen(buf
));
243 static void remove_fd_in_watch(CharDriverState
*chr
);
245 void qemu_chr_add_handlers(CharDriverState
*s
,
246 IOCanReadHandler
*fd_can_read
,
247 IOReadHandler
*fd_read
,
248 IOEventHandler
*fd_event
,
253 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
255 remove_fd_in_watch(s
);
259 s
->chr_can_read
= fd_can_read
;
260 s
->chr_read
= fd_read
;
261 s
->chr_event
= fd_event
;
262 s
->handler_opaque
= opaque
;
263 if (fe_open
&& s
->chr_update_read_handler
)
264 s
->chr_update_read_handler(s
);
266 if (!s
->explicit_fe_open
) {
267 qemu_chr_fe_set_open(s
, fe_open
);
270 /* We're connecting to an already opened device, so let's make sure we
271 also get the open event */
272 if (fe_open
&& s
->be_open
) {
273 qemu_chr_be_generic_open(s
);
277 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
282 static CharDriverState
*qemu_chr_open_null(void)
284 CharDriverState
*chr
;
286 chr
= g_malloc0(sizeof(CharDriverState
));
287 chr
->chr_write
= null_chr_write
;
288 chr
->explicit_be_open
= true;
292 /* MUX driver for serial I/O splitting */
294 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
295 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
297 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
298 IOReadHandler
*chr_read
[MAX_MUX
];
299 IOEventHandler
*chr_event
[MAX_MUX
];
300 void *ext_opaque
[MAX_MUX
];
301 CharDriverState
*drv
;
306 /* Intermediate input buffer allows to catch escape sequences even if the
307 currently active device is not accepting any input - but only until it
309 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
314 int64_t timestamps_start
;
318 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
320 MuxDriver
*d
= chr
->opaque
;
322 if (!d
->timestamps
) {
323 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
328 for (i
= 0; i
< len
; i
++) {
334 ti
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
335 if (d
->timestamps_start
== -1)
336 d
->timestamps_start
= ti
;
337 ti
-= d
->timestamps_start
;
339 snprintf(buf1
, sizeof(buf1
),
340 "[%02d:%02d:%02d.%03d] ",
345 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
348 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
349 if (buf
[i
] == '\n') {
357 static const char * const mux_help
[] = {
358 "% h print this help\n\r",
359 "% x exit emulator\n\r",
360 "% s save disk data back to file (if -snapshot)\n\r",
361 "% t toggle console timestamps\n\r"
362 "% b send break (magic sysrq)\n\r",
363 "% c switch between console and monitor\n\r",
368 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
369 static void mux_print_help(CharDriverState
*chr
)
372 char ebuf
[15] = "Escape-Char";
373 char cbuf
[50] = "\n\r";
375 if (term_escape_char
> 0 && term_escape_char
< 26) {
376 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
377 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
379 snprintf(cbuf
, sizeof(cbuf
),
380 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
383 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
384 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
385 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
386 if (mux_help
[i
][j
] == '%')
387 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
389 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
394 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
396 if (d
->chr_event
[mux_nr
])
397 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
400 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
402 if (d
->term_got_escape
) {
403 d
->term_got_escape
= 0;
404 if (ch
== term_escape_char
)
413 const char *term
= "QEMU: Terminated\n\r";
414 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
422 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
425 /* Switch to the next registered device */
426 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
428 if (d
->focus
>= d
->mux_cnt
)
430 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
433 d
->timestamps
= !d
->timestamps
;
434 d
->timestamps_start
= -1;
438 } else if (ch
== term_escape_char
) {
439 d
->term_got_escape
= 1;
447 static void mux_chr_accept_input(CharDriverState
*chr
)
449 MuxDriver
*d
= chr
->opaque
;
452 while (d
->prod
[m
] != d
->cons
[m
] &&
453 d
->chr_can_read
[m
] &&
454 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
455 d
->chr_read
[m
](d
->ext_opaque
[m
],
456 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
460 static int mux_chr_can_read(void *opaque
)
462 CharDriverState
*chr
= opaque
;
463 MuxDriver
*d
= chr
->opaque
;
466 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
468 if (d
->chr_can_read
[m
])
469 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
473 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
475 CharDriverState
*chr
= opaque
;
476 MuxDriver
*d
= chr
->opaque
;
480 mux_chr_accept_input (opaque
);
482 for(i
= 0; i
< size
; i
++)
483 if (mux_proc_byte(chr
, d
, buf
[i
])) {
484 if (d
->prod
[m
] == d
->cons
[m
] &&
485 d
->chr_can_read
[m
] &&
486 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
487 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
489 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
493 static void mux_chr_event(void *opaque
, int event
)
495 CharDriverState
*chr
= opaque
;
496 MuxDriver
*d
= chr
->opaque
;
499 /* Send the event to all registered listeners */
500 for (i
= 0; i
< d
->mux_cnt
; i
++)
501 mux_chr_send_event(d
, i
, event
);
504 static void mux_chr_update_read_handler(CharDriverState
*chr
)
506 MuxDriver
*d
= chr
->opaque
;
508 if (d
->mux_cnt
>= MAX_MUX
) {
509 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
512 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
513 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
514 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
515 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
516 /* Fix up the real driver with mux routines */
517 if (d
->mux_cnt
== 0) {
518 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
521 if (d
->focus
!= -1) {
522 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
524 d
->focus
= d
->mux_cnt
;
526 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
529 static bool muxes_realized
;
532 * Called after processing of default and command-line-specified
533 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
534 * to a mux chardev. This is done here to ensure that
535 * output/prompts/banners are only displayed for the FE that has
536 * focus when initial command-line processing/machine init is
539 * After this point, any new FE attached to any new or existing
540 * mux will receive CHR_EVENT_OPENED notifications for the BE
543 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
545 CharDriverState
*chr
;
547 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
549 MuxDriver
*d
= chr
->opaque
;
552 /* send OPENED to all already-attached FEs */
553 for (i
= 0; i
< d
->mux_cnt
; i
++) {
554 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
556 /* mark mux as OPENED so any new FEs will immediately receive
559 qemu_chr_be_generic_open(chr
);
562 muxes_realized
= true;
565 static Notifier muxes_realize_notify
= {
566 .notify
= muxes_realize_done
,
569 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
571 CharDriverState
*chr
;
574 chr
= g_malloc0(sizeof(CharDriverState
));
575 d
= g_malloc0(sizeof(MuxDriver
));
580 chr
->chr_write
= mux_chr_write
;
581 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
582 chr
->chr_accept_input
= mux_chr_accept_input
;
583 /* Frontend guest-open / -close notification is not support with muxes */
584 chr
->chr_set_fe_open
= NULL
;
585 /* only default to opened state if we've realized the initial
588 chr
->explicit_be_open
= muxes_realized
? 0 : 1;
596 int send_all(int fd
, const void *buf
, int len1
)
602 ret
= send(fd
, buf
, len
, 0);
604 errno
= WSAGetLastError();
605 if (errno
!= WSAEWOULDBLOCK
) {
608 } else if (ret
== 0) {
620 int send_all(int fd
, const void *_buf
, int len1
)
623 const uint8_t *buf
= _buf
;
627 ret
= write(fd
, buf
, len
);
629 if (errno
!= EINTR
&& errno
!= EAGAIN
)
631 } else if (ret
== 0) {
641 int recv_all(int fd
, void *_buf
, int len1
, bool single_read
)
647 while ((len
> 0) && (ret
= read(fd
, buf
, len
)) != 0) {
649 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
666 typedef struct IOWatchPoll
673 IOCanReadHandler
*fd_can_read
;
678 static IOWatchPoll
*io_watch_poll_from_source(GSource
*source
)
680 return container_of(source
, IOWatchPoll
, parent
);
683 static gboolean
io_watch_poll_prepare(GSource
*source
, gint
*timeout_
)
685 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
686 bool now_active
= iwp
->fd_can_read(iwp
->opaque
) > 0;
687 bool was_active
= iwp
->src
!= NULL
;
688 if (was_active
== now_active
) {
693 iwp
->src
= g_io_create_watch(iwp
->channel
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
);
694 g_source_set_callback(iwp
->src
, iwp
->fd_read
, iwp
->opaque
, NULL
);
695 g_source_attach(iwp
->src
, NULL
);
697 g_source_destroy(iwp
->src
);
698 g_source_unref(iwp
->src
);
704 static gboolean
io_watch_poll_check(GSource
*source
)
709 static gboolean
io_watch_poll_dispatch(GSource
*source
, GSourceFunc callback
,
715 static void io_watch_poll_finalize(GSource
*source
)
717 /* Due to a glib bug, removing the last reference to a source
718 * inside a finalize callback causes recursive locking (and a
719 * deadlock). This is not a problem inside other callbacks,
720 * including dispatch callbacks, so we call io_remove_watch_poll
721 * to remove this source. At this point, iwp->src must
722 * be NULL, or we would leak it.
724 * This would be solved much more elegantly by child sources,
725 * but we support older glib versions that do not have them.
727 IOWatchPoll
*iwp
= io_watch_poll_from_source(source
);
728 assert(iwp
->src
== NULL
);
731 static GSourceFuncs io_watch_poll_funcs
= {
732 .prepare
= io_watch_poll_prepare
,
733 .check
= io_watch_poll_check
,
734 .dispatch
= io_watch_poll_dispatch
,
735 .finalize
= io_watch_poll_finalize
,
738 /* Can only be used for read */
739 static guint
io_add_watch_poll(GIOChannel
*channel
,
740 IOCanReadHandler
*fd_can_read
,
747 iwp
= (IOWatchPoll
*) g_source_new(&io_watch_poll_funcs
, sizeof(IOWatchPoll
));
748 iwp
->fd_can_read
= fd_can_read
;
749 iwp
->opaque
= user_data
;
750 iwp
->channel
= channel
;
751 iwp
->fd_read
= (GSourceFunc
) fd_read
;
754 tag
= g_source_attach(&iwp
->parent
, NULL
);
755 g_source_unref(&iwp
->parent
);
759 static void io_remove_watch_poll(guint tag
)
764 g_return_if_fail (tag
> 0);
766 source
= g_main_context_find_source_by_id(NULL
, tag
);
767 g_return_if_fail (source
!= NULL
);
769 iwp
= io_watch_poll_from_source(source
);
771 g_source_destroy(iwp
->src
);
772 g_source_unref(iwp
->src
);
775 g_source_destroy(&iwp
->parent
);
778 static void remove_fd_in_watch(CharDriverState
*chr
)
780 if (chr
->fd_in_tag
) {
781 io_remove_watch_poll(chr
->fd_in_tag
);
787 static GIOChannel
*io_channel_from_fd(int fd
)
795 chan
= g_io_channel_unix_new(fd
);
797 g_io_channel_set_encoding(chan
, NULL
, NULL
);
798 g_io_channel_set_buffered(chan
, FALSE
);
804 static GIOChannel
*io_channel_from_socket(int fd
)
813 chan
= g_io_channel_win32_new_socket(fd
);
815 chan
= g_io_channel_unix_new(fd
);
818 g_io_channel_set_encoding(chan
, NULL
, NULL
);
819 g_io_channel_set_buffered(chan
, FALSE
);
824 static int io_channel_send(GIOChannel
*fd
, const void *buf
, size_t len
)
827 GIOStatus status
= G_IO_STATUS_NORMAL
;
829 while (offset
< len
&& status
== G_IO_STATUS_NORMAL
) {
830 gsize bytes_written
= 0;
832 status
= g_io_channel_write_chars(fd
, buf
+ offset
, len
- offset
,
833 &bytes_written
, NULL
);
834 offset
+= bytes_written
;
841 case G_IO_STATUS_NORMAL
:
844 case G_IO_STATUS_AGAIN
:
856 typedef struct FDCharDriver
{
857 CharDriverState
*chr
;
858 GIOChannel
*fd_in
, *fd_out
;
860 QTAILQ_ENTRY(FDCharDriver
) node
;
863 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
865 FDCharDriver
*s
= chr
->opaque
;
867 return io_channel_send(s
->fd_out
, buf
, len
);
870 static gboolean
fd_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
872 CharDriverState
*chr
= opaque
;
873 FDCharDriver
*s
= chr
->opaque
;
875 uint8_t buf
[READ_BUF_LEN
];
880 if (len
> s
->max_size
) {
887 status
= g_io_channel_read_chars(chan
, (gchar
*)buf
,
888 len
, &bytes_read
, NULL
);
889 if (status
== G_IO_STATUS_EOF
) {
890 remove_fd_in_watch(chr
);
891 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
894 if (status
== G_IO_STATUS_NORMAL
) {
895 qemu_chr_be_write(chr
, buf
, bytes_read
);
901 static int fd_chr_read_poll(void *opaque
)
903 CharDriverState
*chr
= opaque
;
904 FDCharDriver
*s
= chr
->opaque
;
906 s
->max_size
= qemu_chr_be_can_write(chr
);
910 static GSource
*fd_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
912 FDCharDriver
*s
= chr
->opaque
;
913 return g_io_create_watch(s
->fd_out
, cond
);
916 static void fd_chr_update_read_handler(CharDriverState
*chr
)
918 FDCharDriver
*s
= chr
->opaque
;
920 remove_fd_in_watch(chr
);
922 chr
->fd_in_tag
= io_add_watch_poll(s
->fd_in
, fd_chr_read_poll
,
927 static void fd_chr_close(struct CharDriverState
*chr
)
929 FDCharDriver
*s
= chr
->opaque
;
931 remove_fd_in_watch(chr
);
933 g_io_channel_unref(s
->fd_in
);
936 g_io_channel_unref(s
->fd_out
);
940 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
943 /* open a character device to a unix fd */
944 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
946 CharDriverState
*chr
;
949 chr
= g_malloc0(sizeof(CharDriverState
));
950 s
= g_malloc0(sizeof(FDCharDriver
));
951 s
->fd_in
= io_channel_from_fd(fd_in
);
952 s
->fd_out
= io_channel_from_fd(fd_out
);
953 fcntl(fd_out
, F_SETFL
, O_NONBLOCK
);
956 chr
->chr_add_watch
= fd_chr_add_watch
;
957 chr
->chr_write
= fd_chr_write
;
958 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
959 chr
->chr_close
= fd_chr_close
;
964 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
967 char filename_in
[256], filename_out
[256];
968 const char *filename
= opts
->device
;
970 if (filename
== NULL
) {
971 fprintf(stderr
, "chardev: pipe: no filename given\n");
975 snprintf(filename_in
, 256, "%s.in", filename
);
976 snprintf(filename_out
, 256, "%s.out", filename
);
977 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
978 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
979 if (fd_in
< 0 || fd_out
< 0) {
984 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
989 return qemu_chr_open_fd(fd_in
, fd_out
);
992 /* init terminal so that we can grab keys */
993 static struct termios oldtty
;
994 static int old_fd0_flags
;
995 static bool stdio_allow_signal
;
997 static void term_exit(void)
999 tcsetattr (0, TCSANOW
, &oldtty
);
1000 fcntl(0, F_SETFL
, old_fd0_flags
);
1003 static void qemu_chr_set_echo_stdio(CharDriverState
*chr
, bool echo
)
1009 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1010 |INLCR
|IGNCR
|ICRNL
|IXON
);
1011 tty
.c_oflag
|= OPOST
;
1012 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1013 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1016 tty
.c_cc
[VTIME
] = 0;
1018 if (!stdio_allow_signal
)
1019 tty
.c_lflag
&= ~ISIG
;
1021 tcsetattr (0, TCSANOW
, &tty
);
1024 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
1030 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
1032 CharDriverState
*chr
;
1034 if (is_daemonized()) {
1035 error_report("cannot use stdio with -daemonize");
1038 old_fd0_flags
= fcntl(0, F_GETFL
);
1039 tcgetattr (0, &oldtty
);
1040 fcntl(0, F_SETFL
, O_NONBLOCK
);
1043 chr
= qemu_chr_open_fd(0, 1);
1044 chr
->chr_close
= qemu_chr_close_stdio
;
1045 chr
->chr_set_echo
= qemu_chr_set_echo_stdio
;
1046 if (opts
->has_signal
) {
1047 stdio_allow_signal
= opts
->signal
;
1049 qemu_chr_fe_set_echo(chr
, false);
1054 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
1055 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
1056 || defined(__GLIBC__)
1058 #define HAVE_CHARDEV_TTY 1
1067 static void pty_chr_update_read_handler(CharDriverState
*chr
);
1068 static void pty_chr_state(CharDriverState
*chr
, int connected
);
1070 static gboolean
pty_chr_timer(gpointer opaque
)
1072 struct CharDriverState
*chr
= opaque
;
1073 PtyCharDriver
*s
= chr
->opaque
;
1076 if (!s
->connected
) {
1078 pty_chr_update_read_handler(chr
);
1083 static void pty_chr_rearm_timer(CharDriverState
*chr
, int ms
)
1085 PtyCharDriver
*s
= chr
->opaque
;
1088 g_source_remove(s
->timer_tag
);
1093 s
->timer_tag
= g_timeout_add_seconds(1, pty_chr_timer
, chr
);
1095 s
->timer_tag
= g_timeout_add(ms
, pty_chr_timer
, chr
);
1099 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1101 PtyCharDriver
*s
= chr
->opaque
;
1103 if (!s
->connected
) {
1104 /* guest sends data, check for (re-)connect */
1105 pty_chr_update_read_handler(chr
);
1108 return io_channel_send(s
->fd
, buf
, len
);
1111 static GSource
*pty_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
1113 PtyCharDriver
*s
= chr
->opaque
;
1114 return g_io_create_watch(s
->fd
, cond
);
1117 static int pty_chr_read_poll(void *opaque
)
1119 CharDriverState
*chr
= opaque
;
1120 PtyCharDriver
*s
= chr
->opaque
;
1122 s
->read_bytes
= qemu_chr_be_can_write(chr
);
1123 return s
->read_bytes
;
1126 static gboolean
pty_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1128 CharDriverState
*chr
= opaque
;
1129 PtyCharDriver
*s
= chr
->opaque
;
1131 uint8_t buf
[READ_BUF_LEN
];
1135 if (len
> s
->read_bytes
)
1136 len
= s
->read_bytes
;
1140 status
= g_io_channel_read_chars(s
->fd
, (gchar
*)buf
, len
, &size
, NULL
);
1141 if (status
!= G_IO_STATUS_NORMAL
) {
1142 pty_chr_state(chr
, 0);
1145 pty_chr_state(chr
, 1);
1146 qemu_chr_be_write(chr
, buf
, size
);
1151 static void pty_chr_update_read_handler(CharDriverState
*chr
)
1153 PtyCharDriver
*s
= chr
->opaque
;
1156 pfd
.fd
= g_io_channel_unix_get_fd(s
->fd
);
1157 pfd
.events
= G_IO_OUT
;
1160 if (pfd
.revents
& G_IO_HUP
) {
1161 pty_chr_state(chr
, 0);
1163 pty_chr_state(chr
, 1);
1167 static void pty_chr_state(CharDriverState
*chr
, int connected
)
1169 PtyCharDriver
*s
= chr
->opaque
;
1172 remove_fd_in_watch(chr
);
1174 /* (re-)connect poll interval for idle guests: once per second.
1175 * We check more frequently in case the guests sends data to
1176 * the virtual device linked to our pty. */
1177 pty_chr_rearm_timer(chr
, 1000);
1180 g_source_remove(s
->timer_tag
);
1183 if (!s
->connected
) {
1185 qemu_chr_be_generic_open(chr
);
1187 if (!chr
->fd_in_tag
) {
1188 chr
->fd_in_tag
= io_add_watch_poll(s
->fd
, pty_chr_read_poll
,
1194 static void pty_chr_close(struct CharDriverState
*chr
)
1196 PtyCharDriver
*s
= chr
->opaque
;
1199 remove_fd_in_watch(chr
);
1200 fd
= g_io_channel_unix_get_fd(s
->fd
);
1201 g_io_channel_unref(s
->fd
);
1204 g_source_remove(s
->timer_tag
);
1208 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1211 static CharDriverState
*qemu_chr_open_pty(const char *id
,
1214 CharDriverState
*chr
;
1216 int master_fd
, slave_fd
;
1217 char pty_name
[PATH_MAX
];
1219 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
1220 if (master_fd
< 0) {
1226 chr
= g_malloc0(sizeof(CharDriverState
));
1228 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
1229 ret
->pty
= g_strdup(pty_name
);
1230 ret
->has_pty
= true;
1232 fprintf(stderr
, "char device redirected to %s (label %s)\n",
1235 s
= g_malloc0(sizeof(PtyCharDriver
));
1237 chr
->chr_write
= pty_chr_write
;
1238 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1239 chr
->chr_close
= pty_chr_close
;
1240 chr
->chr_add_watch
= pty_chr_add_watch
;
1241 chr
->explicit_be_open
= true;
1243 s
->fd
= io_channel_from_fd(master_fd
);
1249 static void tty_serial_init(int fd
, int speed
,
1250 int parity
, int data_bits
, int stop_bits
)
1256 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1257 speed
, parity
, data_bits
, stop_bits
);
1259 tcgetattr (fd
, &tty
);
1261 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1262 speed
= speed
* 10 / 11;
1279 /* Non-Posix values follow. They may be unsupported on some systems. */
1281 check_speed(115200);
1283 check_speed(230400);
1286 check_speed(460800);
1289 check_speed(500000);
1292 check_speed(576000);
1295 check_speed(921600);
1298 check_speed(1000000);
1301 check_speed(1152000);
1304 check_speed(1500000);
1307 check_speed(2000000);
1310 check_speed(2500000);
1313 check_speed(3000000);
1316 check_speed(3500000);
1319 check_speed(4000000);
1324 cfsetispeed(&tty
, spd
);
1325 cfsetospeed(&tty
, spd
);
1327 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1328 |INLCR
|IGNCR
|ICRNL
|IXON
);
1329 tty
.c_oflag
|= OPOST
;
1330 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1331 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1352 tty
.c_cflag
|= PARENB
;
1355 tty
.c_cflag
|= PARENB
| PARODD
;
1359 tty
.c_cflag
|= CSTOPB
;
1361 tcsetattr (fd
, TCSANOW
, &tty
);
1364 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1366 FDCharDriver
*s
= chr
->opaque
;
1369 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1371 QEMUSerialSetParams
*ssp
= arg
;
1372 tty_serial_init(g_io_channel_unix_get_fd(s
->fd_in
),
1373 ssp
->speed
, ssp
->parity
,
1374 ssp
->data_bits
, ssp
->stop_bits
);
1377 case CHR_IOCTL_SERIAL_SET_BREAK
:
1379 int enable
= *(int *)arg
;
1381 tcsendbreak(g_io_channel_unix_get_fd(s
->fd_in
), 1);
1385 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1388 int *targ
= (int *)arg
;
1389 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &sarg
);
1391 if (sarg
& TIOCM_CTS
)
1392 *targ
|= CHR_TIOCM_CTS
;
1393 if (sarg
& TIOCM_CAR
)
1394 *targ
|= CHR_TIOCM_CAR
;
1395 if (sarg
& TIOCM_DSR
)
1396 *targ
|= CHR_TIOCM_DSR
;
1397 if (sarg
& TIOCM_RI
)
1398 *targ
|= CHR_TIOCM_RI
;
1399 if (sarg
& TIOCM_DTR
)
1400 *targ
|= CHR_TIOCM_DTR
;
1401 if (sarg
& TIOCM_RTS
)
1402 *targ
|= CHR_TIOCM_RTS
;
1405 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1407 int sarg
= *(int *)arg
;
1409 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMGET
, &targ
);
1410 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1411 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1412 if (sarg
& CHR_TIOCM_CTS
)
1414 if (sarg
& CHR_TIOCM_CAR
)
1416 if (sarg
& CHR_TIOCM_DSR
)
1418 if (sarg
& CHR_TIOCM_RI
)
1420 if (sarg
& CHR_TIOCM_DTR
)
1422 if (sarg
& CHR_TIOCM_RTS
)
1424 ioctl(g_io_channel_unix_get_fd(s
->fd_in
), TIOCMSET
, &targ
);
1433 static void qemu_chr_close_tty(CharDriverState
*chr
)
1435 FDCharDriver
*s
= chr
->opaque
;
1439 fd
= g_io_channel_unix_get_fd(s
->fd_in
);
1449 static CharDriverState
*qemu_chr_open_tty_fd(int fd
)
1451 CharDriverState
*chr
;
1453 tty_serial_init(fd
, 115200, 'N', 8, 1);
1454 chr
= qemu_chr_open_fd(fd
, fd
);
1455 chr
->chr_ioctl
= tty_serial_ioctl
;
1456 chr
->chr_close
= qemu_chr_close_tty
;
1459 #endif /* __linux__ || __sun__ */
1461 #if defined(__linux__)
1463 #define HAVE_CHARDEV_PARPORT 1
1468 } ParallelCharDriver
;
1470 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1472 if (s
->mode
!= mode
) {
1474 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1481 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1483 ParallelCharDriver
*drv
= chr
->opaque
;
1488 case CHR_IOCTL_PP_READ_DATA
:
1489 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1491 *(uint8_t *)arg
= b
;
1493 case CHR_IOCTL_PP_WRITE_DATA
:
1494 b
= *(uint8_t *)arg
;
1495 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1498 case CHR_IOCTL_PP_READ_CONTROL
:
1499 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1501 /* Linux gives only the lowest bits, and no way to know data
1502 direction! For better compatibility set the fixed upper
1504 *(uint8_t *)arg
= b
| 0xc0;
1506 case CHR_IOCTL_PP_WRITE_CONTROL
:
1507 b
= *(uint8_t *)arg
;
1508 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1511 case CHR_IOCTL_PP_READ_STATUS
:
1512 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1514 *(uint8_t *)arg
= b
;
1516 case CHR_IOCTL_PP_DATA_DIR
:
1517 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1520 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1521 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1522 struct ParallelIOArg
*parg
= arg
;
1523 int n
= read(fd
, parg
->buffer
, parg
->count
);
1524 if (n
!= parg
->count
) {
1529 case CHR_IOCTL_PP_EPP_READ
:
1530 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1531 struct ParallelIOArg
*parg
= arg
;
1532 int n
= read(fd
, parg
->buffer
, parg
->count
);
1533 if (n
!= parg
->count
) {
1538 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1539 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1540 struct ParallelIOArg
*parg
= arg
;
1541 int n
= write(fd
, parg
->buffer
, parg
->count
);
1542 if (n
!= parg
->count
) {
1547 case CHR_IOCTL_PP_EPP_WRITE
:
1548 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1549 struct ParallelIOArg
*parg
= arg
;
1550 int n
= write(fd
, parg
->buffer
, parg
->count
);
1551 if (n
!= parg
->count
) {
1562 static void pp_close(CharDriverState
*chr
)
1564 ParallelCharDriver
*drv
= chr
->opaque
;
1567 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1568 ioctl(fd
, PPRELEASE
);
1571 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1574 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1576 CharDriverState
*chr
;
1577 ParallelCharDriver
*drv
;
1579 if (ioctl(fd
, PPCLAIM
) < 0) {
1584 drv
= g_malloc0(sizeof(ParallelCharDriver
));
1586 drv
->mode
= IEEE1284_MODE_COMPAT
;
1588 chr
= g_malloc0(sizeof(CharDriverState
));
1589 chr
->chr_write
= null_chr_write
;
1590 chr
->chr_ioctl
= pp_ioctl
;
1591 chr
->chr_close
= pp_close
;
1596 #endif /* __linux__ */
1598 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1600 #define HAVE_CHARDEV_PARPORT 1
1602 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1604 int fd
= (int)(intptr_t)chr
->opaque
;
1608 case CHR_IOCTL_PP_READ_DATA
:
1609 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1611 *(uint8_t *)arg
= b
;
1613 case CHR_IOCTL_PP_WRITE_DATA
:
1614 b
= *(uint8_t *)arg
;
1615 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1618 case CHR_IOCTL_PP_READ_CONTROL
:
1619 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1621 *(uint8_t *)arg
= b
;
1623 case CHR_IOCTL_PP_WRITE_CONTROL
:
1624 b
= *(uint8_t *)arg
;
1625 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1628 case CHR_IOCTL_PP_READ_STATUS
:
1629 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1631 *(uint8_t *)arg
= b
;
1639 static CharDriverState
*qemu_chr_open_pp_fd(int fd
)
1641 CharDriverState
*chr
;
1643 chr
= g_malloc0(sizeof(CharDriverState
));
1644 chr
->opaque
= (void *)(intptr_t)fd
;
1645 chr
->chr_write
= null_chr_write
;
1646 chr
->chr_ioctl
= pp_ioctl
;
1647 chr
->explicit_be_open
= true;
1656 HANDLE hcom
, hrecv
, hsend
;
1657 OVERLAPPED orecv
, osend
;
1664 HANDLE hInputReadyEvent
;
1665 HANDLE hInputDoneEvent
;
1666 HANDLE hInputThread
;
1667 uint8_t win_stdio_buf
;
1668 } WinStdioCharState
;
1670 #define NSENDBUF 2048
1671 #define NRECVBUF 2048
1672 #define MAXCONNECT 1
1673 #define NTIMEOUT 5000
1675 static int win_chr_poll(void *opaque
);
1676 static int win_chr_pipe_poll(void *opaque
);
1678 static void win_chr_close(CharDriverState
*chr
)
1680 WinCharState
*s
= chr
->opaque
;
1683 CloseHandle(s
->hsend
);
1687 CloseHandle(s
->hrecv
);
1691 CloseHandle(s
->hcom
);
1695 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1697 qemu_del_polling_cb(win_chr_poll
, chr
);
1699 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1702 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1704 WinCharState
*s
= chr
->opaque
;
1706 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1711 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1713 fprintf(stderr
, "Failed CreateEvent\n");
1716 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1718 fprintf(stderr
, "Failed CreateEvent\n");
1722 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1723 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1724 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1725 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1730 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1731 fprintf(stderr
, "Failed SetupComm\n");
1735 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1736 size
= sizeof(COMMCONFIG
);
1737 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1738 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1739 CommConfigDialog(filename
, NULL
, &comcfg
);
1741 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1742 fprintf(stderr
, "Failed SetCommState\n");
1746 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1747 fprintf(stderr
, "Failed SetCommMask\n");
1751 cto
.ReadIntervalTimeout
= MAXDWORD
;
1752 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1753 fprintf(stderr
, "Failed SetCommTimeouts\n");
1757 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1758 fprintf(stderr
, "Failed ClearCommError\n");
1761 qemu_add_polling_cb(win_chr_poll
, chr
);
1769 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1771 WinCharState
*s
= chr
->opaque
;
1772 DWORD len
, ret
, size
, err
;
1775 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1776 s
->osend
.hEvent
= s
->hsend
;
1779 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1781 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1783 err
= GetLastError();
1784 if (err
== ERROR_IO_PENDING
) {
1785 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1803 static int win_chr_read_poll(CharDriverState
*chr
)
1805 WinCharState
*s
= chr
->opaque
;
1807 s
->max_size
= qemu_chr_be_can_write(chr
);
1811 static void win_chr_readfile(CharDriverState
*chr
)
1813 WinCharState
*s
= chr
->opaque
;
1815 uint8_t buf
[READ_BUF_LEN
];
1818 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1819 s
->orecv
.hEvent
= s
->hrecv
;
1820 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1822 err
= GetLastError();
1823 if (err
== ERROR_IO_PENDING
) {
1824 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1829 qemu_chr_be_write(chr
, buf
, size
);
1833 static void win_chr_read(CharDriverState
*chr
)
1835 WinCharState
*s
= chr
->opaque
;
1837 if (s
->len
> s
->max_size
)
1838 s
->len
= s
->max_size
;
1842 win_chr_readfile(chr
);
1845 static int win_chr_poll(void *opaque
)
1847 CharDriverState
*chr
= opaque
;
1848 WinCharState
*s
= chr
->opaque
;
1852 ClearCommError(s
->hcom
, &comerr
, &status
);
1853 if (status
.cbInQue
> 0) {
1854 s
->len
= status
.cbInQue
;
1855 win_chr_read_poll(chr
);
1862 static CharDriverState
*qemu_chr_open_win_path(const char *filename
)
1864 CharDriverState
*chr
;
1867 chr
= g_malloc0(sizeof(CharDriverState
));
1868 s
= g_malloc0(sizeof(WinCharState
));
1870 chr
->chr_write
= win_chr_write
;
1871 chr
->chr_close
= win_chr_close
;
1873 if (win_chr_init(chr
, filename
) < 0) {
1881 static int win_chr_pipe_poll(void *opaque
)
1883 CharDriverState
*chr
= opaque
;
1884 WinCharState
*s
= chr
->opaque
;
1887 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1890 win_chr_read_poll(chr
);
1897 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1899 WinCharState
*s
= chr
->opaque
;
1907 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1909 fprintf(stderr
, "Failed CreateEvent\n");
1912 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1914 fprintf(stderr
, "Failed CreateEvent\n");
1918 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1919 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1920 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1922 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1923 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1924 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1929 ZeroMemory(&ov
, sizeof(ov
));
1930 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1931 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1933 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1937 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1939 fprintf(stderr
, "Failed GetOverlappedResult\n");
1941 CloseHandle(ov
.hEvent
);
1948 CloseHandle(ov
.hEvent
);
1951 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1960 static CharDriverState
*qemu_chr_open_pipe(ChardevHostdev
*opts
)
1962 const char *filename
= opts
->device
;
1963 CharDriverState
*chr
;
1966 chr
= g_malloc0(sizeof(CharDriverState
));
1967 s
= g_malloc0(sizeof(WinCharState
));
1969 chr
->chr_write
= win_chr_write
;
1970 chr
->chr_close
= win_chr_close
;
1972 if (win_chr_pipe_init(chr
, filename
) < 0) {
1980 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1982 CharDriverState
*chr
;
1985 chr
= g_malloc0(sizeof(CharDriverState
));
1986 s
= g_malloc0(sizeof(WinCharState
));
1989 chr
->chr_write
= win_chr_write
;
1993 static CharDriverState
*qemu_chr_open_win_con(void)
1995 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1998 static int win_stdio_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2000 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
2007 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
2017 static void win_stdio_wait_func(void *opaque
)
2019 CharDriverState
*chr
= opaque
;
2020 WinStdioCharState
*stdio
= chr
->opaque
;
2021 INPUT_RECORD buf
[4];
2026 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
2029 /* Avoid error storm */
2030 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
2034 for (i
= 0; i
< dwSize
; i
++) {
2035 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
2037 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
2039 if (kev
->uChar
.AsciiChar
!= 0) {
2040 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
2041 if (qemu_chr_be_can_write(chr
)) {
2042 uint8_t c
= kev
->uChar
.AsciiChar
;
2043 qemu_chr_be_write(chr
, &c
, 1);
2051 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
2053 CharDriverState
*chr
= param
;
2054 WinStdioCharState
*stdio
= chr
->opaque
;
2060 /* Wait for one byte */
2061 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
2063 /* Exit in case of error, continue if nothing read */
2071 /* Some terminal emulator returns \r\n for Enter, just pass \n */
2072 if (stdio
->win_stdio_buf
== '\r') {
2076 /* Signal the main thread and wait until the byte was eaten */
2077 if (!SetEvent(stdio
->hInputReadyEvent
)) {
2080 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
2086 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
2090 static void win_stdio_thread_wait_func(void *opaque
)
2092 CharDriverState
*chr
= opaque
;
2093 WinStdioCharState
*stdio
= chr
->opaque
;
2095 if (qemu_chr_be_can_write(chr
)) {
2096 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
2099 SetEvent(stdio
->hInputDoneEvent
);
2102 static void qemu_chr_set_echo_win_stdio(CharDriverState
*chr
, bool echo
)
2104 WinStdioCharState
*stdio
= chr
->opaque
;
2107 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
2110 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
2112 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
2116 static void win_stdio_close(CharDriverState
*chr
)
2118 WinStdioCharState
*stdio
= chr
->opaque
;
2120 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
2121 CloseHandle(stdio
->hInputReadyEvent
);
2123 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
2124 CloseHandle(stdio
->hInputDoneEvent
);
2126 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
2127 TerminateThread(stdio
->hInputThread
, 0);
2130 g_free(chr
->opaque
);
2134 static CharDriverState
*qemu_chr_open_stdio(ChardevStdio
*opts
)
2136 CharDriverState
*chr
;
2137 WinStdioCharState
*stdio
;
2141 chr
= g_malloc0(sizeof(CharDriverState
));
2142 stdio
= g_malloc0(sizeof(WinStdioCharState
));
2144 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
2145 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
2146 fprintf(stderr
, "cannot open stdio: invalid handle\n");
2150 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
2152 chr
->opaque
= stdio
;
2153 chr
->chr_write
= win_stdio_write
;
2154 chr
->chr_close
= win_stdio_close
;
2157 if (qemu_add_wait_object(stdio
->hStdIn
,
2158 win_stdio_wait_func
, chr
)) {
2159 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2164 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2165 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2166 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
2169 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
2170 || stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
2171 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
2172 fprintf(stderr
, "cannot create stdio thread or event\n");
2175 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
2176 win_stdio_thread_wait_func
, chr
)) {
2177 fprintf(stderr
, "qemu_add_wait_object: failed\n");
2181 dwMode
|= ENABLE_LINE_INPUT
;
2184 /* set the terminal in raw mode */
2185 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
2186 dwMode
|= ENABLE_PROCESSED_INPUT
;
2189 SetConsoleMode(stdio
->hStdIn
, dwMode
);
2191 chr
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
2192 qemu_chr_fe_set_echo(chr
, false);
2196 #endif /* !_WIN32 */
2199 /***********************************************************/
2200 /* UDP Net console */
2205 uint8_t buf
[READ_BUF_LEN
];
2211 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2213 NetCharDriver
*s
= chr
->opaque
;
2214 gsize bytes_written
;
2217 status
= g_io_channel_write_chars(s
->chan
, (const gchar
*)buf
, len
, &bytes_written
, NULL
);
2218 if (status
== G_IO_STATUS_EOF
) {
2220 } else if (status
!= G_IO_STATUS_NORMAL
) {
2224 return bytes_written
;
2227 static int udp_chr_read_poll(void *opaque
)
2229 CharDriverState
*chr
= opaque
;
2230 NetCharDriver
*s
= chr
->opaque
;
2232 s
->max_size
= qemu_chr_be_can_write(chr
);
2234 /* If there were any stray characters in the queue process them
2237 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2238 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2240 s
->max_size
= qemu_chr_be_can_write(chr
);
2245 static gboolean
udp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2247 CharDriverState
*chr
= opaque
;
2248 NetCharDriver
*s
= chr
->opaque
;
2249 gsize bytes_read
= 0;
2252 if (s
->max_size
== 0) {
2255 status
= g_io_channel_read_chars(s
->chan
, (gchar
*)s
->buf
, sizeof(s
->buf
),
2257 s
->bufcnt
= bytes_read
;
2258 s
->bufptr
= s
->bufcnt
;
2259 if (status
!= G_IO_STATUS_NORMAL
) {
2260 remove_fd_in_watch(chr
);
2265 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
2266 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
2268 s
->max_size
= qemu_chr_be_can_write(chr
);
2274 static void udp_chr_update_read_handler(CharDriverState
*chr
)
2276 NetCharDriver
*s
= chr
->opaque
;
2278 remove_fd_in_watch(chr
);
2280 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, udp_chr_read_poll
,
2285 static void udp_chr_close(CharDriverState
*chr
)
2287 NetCharDriver
*s
= chr
->opaque
;
2289 remove_fd_in_watch(chr
);
2291 g_io_channel_unref(s
->chan
);
2295 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2298 static CharDriverState
*qemu_chr_open_udp_fd(int fd
)
2300 CharDriverState
*chr
= NULL
;
2301 NetCharDriver
*s
= NULL
;
2303 chr
= g_malloc0(sizeof(CharDriverState
));
2304 s
= g_malloc0(sizeof(NetCharDriver
));
2307 s
->chan
= io_channel_from_socket(s
->fd
);
2311 chr
->chr_write
= udp_chr_write
;
2312 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
2313 chr
->chr_close
= udp_chr_close
;
2314 /* be isn't opened until we get a connection */
2315 chr
->explicit_be_open
= true;
2319 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
2321 Error
*local_err
= NULL
;
2324 fd
= inet_dgram_opts(opts
, &local_err
);
2326 qerror_report_err(local_err
);
2327 error_free(local_err
);
2330 return qemu_chr_open_udp_fd(fd
);
2333 /***********************************************************/
2334 /* TCP Net console */
2338 GIOChannel
*chan
, *listen_chan
;
2347 int read_msgfds_num
;
2349 int write_msgfds_num
;
2352 static gboolean
tcp_chr_accept(GIOChannel
*chan
, GIOCondition cond
, void *opaque
);
2355 static int unix_send_msgfds(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2357 TCPCharDriver
*s
= chr
->opaque
;
2362 size_t fd_size
= s
->write_msgfds_num
* sizeof(int);
2363 char control
[CMSG_SPACE(fd_size
)];
2364 struct cmsghdr
*cmsg
;
2366 memset(&msgh
, 0, sizeof(msgh
));
2367 memset(control
, 0, sizeof(control
));
2369 /* set the payload */
2370 iov
.iov_base
= (uint8_t *) buf
;
2373 msgh
.msg_iov
= &iov
;
2374 msgh
.msg_iovlen
= 1;
2376 msgh
.msg_control
= control
;
2377 msgh
.msg_controllen
= sizeof(control
);
2379 cmsg
= CMSG_FIRSTHDR(&msgh
);
2381 cmsg
->cmsg_len
= CMSG_LEN(fd_size
);
2382 cmsg
->cmsg_level
= SOL_SOCKET
;
2383 cmsg
->cmsg_type
= SCM_RIGHTS
;
2384 memcpy(CMSG_DATA(cmsg
), s
->write_msgfds
, fd_size
);
2387 r
= sendmsg(s
->fd
, &msgh
, 0);
2388 } while (r
< 0 && errno
== EINTR
);
2390 /* free the written msgfds, no matter what */
2391 if (s
->write_msgfds_num
) {
2392 g_free(s
->write_msgfds
);
2393 s
->write_msgfds
= 0;
2394 s
->write_msgfds_num
= 0;
2401 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2403 TCPCharDriver
*s
= chr
->opaque
;
2406 if (s
->is_unix
&& s
->write_msgfds_num
) {
2407 return unix_send_msgfds(chr
, buf
, len
);
2411 return io_channel_send(s
->chan
, buf
, len
);
2414 /* XXX: indicate an error ? */
2419 static int tcp_chr_read_poll(void *opaque
)
2421 CharDriverState
*chr
= opaque
;
2422 TCPCharDriver
*s
= chr
->opaque
;
2425 s
->max_size
= qemu_chr_be_can_write(chr
);
2430 #define IAC_BREAK 243
2431 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
2433 uint8_t *buf
, int *size
)
2435 /* Handle any telnet client's basic IAC options to satisfy char by
2436 * char mode with no echo. All IAC options will be removed from
2437 * the buf and the do_telnetopt variable will be used to track the
2438 * state of the width of the IAC information.
2440 * IAC commands come in sets of 3 bytes with the exception of the
2441 * "IAC BREAK" command and the double IAC.
2447 for (i
= 0; i
< *size
; i
++) {
2448 if (s
->do_telnetopt
> 1) {
2449 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
2450 /* Double IAC means send an IAC */
2454 s
->do_telnetopt
= 1;
2456 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
2457 /* Handle IAC break commands by sending a serial break */
2458 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
2463 if (s
->do_telnetopt
>= 4) {
2464 s
->do_telnetopt
= 1;
2467 if ((unsigned char)buf
[i
] == IAC
) {
2468 s
->do_telnetopt
= 2;
2479 static int tcp_get_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2481 TCPCharDriver
*s
= chr
->opaque
;
2482 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
2485 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
2487 g_free(s
->read_msgfds
);
2489 s
->read_msgfds_num
= 0;
2495 static int tcp_set_msgfds(CharDriverState
*chr
, int *fds
, int num
)
2497 TCPCharDriver
*s
= chr
->opaque
;
2499 /* clear old pending fd array */
2500 if (s
->write_msgfds
) {
2501 g_free(s
->write_msgfds
);
2505 s
->write_msgfds
= g_malloc(num
* sizeof(int));
2506 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
2509 s
->write_msgfds_num
= num
;
2515 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2517 TCPCharDriver
*s
= chr
->opaque
;
2518 struct cmsghdr
*cmsg
;
2520 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2523 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
2524 cmsg
->cmsg_level
!= SOL_SOCKET
||
2525 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
2529 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
2535 /* close and clean read_msgfds */
2536 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2537 close(s
->read_msgfds
[i
]);
2540 if (s
->read_msgfds_num
) {
2541 g_free(s
->read_msgfds
);
2544 s
->read_msgfds_num
= fd_size
/ sizeof(int);
2545 s
->read_msgfds
= g_malloc(fd_size
);
2546 memcpy(s
->read_msgfds
, CMSG_DATA(cmsg
), fd_size
);
2548 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2549 int fd
= s
->read_msgfds
[i
];
2554 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
2557 #ifndef MSG_CMSG_CLOEXEC
2558 qemu_set_cloexec(fd
);
2564 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2566 TCPCharDriver
*s
= chr
->opaque
;
2567 struct msghdr msg
= { NULL
, };
2568 struct iovec iov
[1];
2570 struct cmsghdr cmsg
;
2571 char control
[CMSG_SPACE(sizeof(int))];
2576 iov
[0].iov_base
= buf
;
2577 iov
[0].iov_len
= len
;
2581 msg
.msg_control
= &msg_control
;
2582 msg
.msg_controllen
= sizeof(msg_control
);
2584 #ifdef MSG_CMSG_CLOEXEC
2585 flags
|= MSG_CMSG_CLOEXEC
;
2587 ret
= recvmsg(s
->fd
, &msg
, flags
);
2588 if (ret
> 0 && s
->is_unix
) {
2589 unix_process_msgfd(chr
, &msg
);
2595 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2597 TCPCharDriver
*s
= chr
->opaque
;
2598 return qemu_recv(s
->fd
, buf
, len
, 0);
2602 static GSource
*tcp_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
2604 TCPCharDriver
*s
= chr
->opaque
;
2605 return g_io_create_watch(s
->chan
, cond
);
2608 static void tcp_chr_disconnect(CharDriverState
*chr
)
2610 TCPCharDriver
*s
= chr
->opaque
;
2613 if (s
->listen_chan
) {
2614 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
,
2615 tcp_chr_accept
, chr
);
2617 remove_fd_in_watch(chr
);
2618 g_io_channel_unref(s
->chan
);
2622 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2625 static gboolean
tcp_chr_read(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
2627 CharDriverState
*chr
= opaque
;
2628 TCPCharDriver
*s
= chr
->opaque
;
2629 uint8_t buf
[READ_BUF_LEN
];
2632 if (!s
->connected
|| s
->max_size
<= 0) {
2636 if (len
> s
->max_size
)
2638 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2640 /* connection closed */
2641 tcp_chr_disconnect(chr
);
2642 } else if (size
> 0) {
2643 if (s
->do_telnetopt
)
2644 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2646 qemu_chr_be_write(chr
, buf
, size
);
2652 static int tcp_chr_sync_read(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2654 TCPCharDriver
*s
= chr
->opaque
;
2657 if (!s
->connected
) {
2661 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
2663 /* connection closed */
2664 tcp_chr_disconnect(chr
);
2671 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2673 CharDriverState
*chr
= qemu_chr_open_fd(eventfd
, eventfd
);
2676 chr
->avail_connections
= 1;
2683 static gboolean
tcp_chr_chan_close(GIOChannel
*channel
, GIOCondition cond
,
2686 CharDriverState
*chr
= opaque
;
2688 if (cond
!= G_IO_HUP
) {
2692 /* connection closed */
2693 tcp_chr_disconnect(chr
);
2694 if (chr
->fd_hup_tag
) {
2695 g_source_remove(chr
->fd_hup_tag
);
2696 chr
->fd_hup_tag
= 0;
2702 static void tcp_chr_connect(void *opaque
)
2704 CharDriverState
*chr
= opaque
;
2705 TCPCharDriver
*s
= chr
->opaque
;
2709 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2711 chr
->fd_hup_tag
= g_io_add_watch(s
->chan
, G_IO_HUP
, tcp_chr_chan_close
,
2714 qemu_chr_be_generic_open(chr
);
2717 static void tcp_chr_update_read_handler(CharDriverState
*chr
)
2719 TCPCharDriver
*s
= chr
->opaque
;
2721 remove_fd_in_watch(chr
);
2723 chr
->fd_in_tag
= io_add_watch_poll(s
->chan
, tcp_chr_read_poll
,
2728 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2729 static void tcp_chr_telnet_init(int fd
)
2732 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2733 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2734 send(fd
, (char *)buf
, 3, 0);
2735 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2736 send(fd
, (char *)buf
, 3, 0);
2737 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2738 send(fd
, (char *)buf
, 3, 0);
2739 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2740 send(fd
, (char *)buf
, 3, 0);
2743 static int tcp_chr_add_client(CharDriverState
*chr
, int fd
)
2745 TCPCharDriver
*s
= chr
->opaque
;
2749 qemu_set_nonblock(fd
);
2751 socket_set_nodelay(fd
);
2753 s
->chan
= io_channel_from_socket(fd
);
2754 if (s
->listen_tag
) {
2755 g_source_remove(s
->listen_tag
);
2758 tcp_chr_connect(chr
);
2763 static gboolean
tcp_chr_accept(GIOChannel
*channel
, GIOCondition cond
, void *opaque
)
2765 CharDriverState
*chr
= opaque
;
2766 TCPCharDriver
*s
= chr
->opaque
;
2767 struct sockaddr_in saddr
;
2769 struct sockaddr_un uaddr
;
2771 struct sockaddr
*addr
;
2778 len
= sizeof(uaddr
);
2779 addr
= (struct sockaddr
*)&uaddr
;
2783 len
= sizeof(saddr
);
2784 addr
= (struct sockaddr
*)&saddr
;
2786 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2787 if (fd
< 0 && errno
!= EINTR
) {
2790 } else if (fd
>= 0) {
2791 if (s
->do_telnetopt
)
2792 tcp_chr_telnet_init(fd
);
2796 if (tcp_chr_add_client(chr
, fd
) < 0)
2802 static void tcp_chr_close(CharDriverState
*chr
)
2804 TCPCharDriver
*s
= chr
->opaque
;
2807 remove_fd_in_watch(chr
);
2809 g_io_channel_unref(s
->chan
);
2813 if (s
->listen_fd
>= 0) {
2814 if (s
->listen_tag
) {
2815 g_source_remove(s
->listen_tag
);
2818 if (s
->listen_chan
) {
2819 g_io_channel_unref(s
->listen_chan
);
2821 closesocket(s
->listen_fd
);
2823 if (s
->read_msgfds_num
) {
2824 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
2825 close(s
->read_msgfds
[i
]);
2827 g_free(s
->read_msgfds
);
2829 if (s
->write_msgfds_num
) {
2830 g_free(s
->write_msgfds
);
2833 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
2836 static CharDriverState
*qemu_chr_open_socket_fd(int fd
, bool do_nodelay
,
2837 bool is_listen
, bool is_telnet
,
2838 bool is_waitconnect
,
2841 CharDriverState
*chr
= NULL
;
2842 TCPCharDriver
*s
= NULL
;
2843 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
2844 const char *left
= "", *right
= "";
2845 struct sockaddr_storage ss
;
2846 socklen_t ss_len
= sizeof(ss
);
2848 memset(&ss
, 0, ss_len
);
2849 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) != 0) {
2850 error_setg_errno(errp
, errno
, "getsockname");
2854 chr
= g_malloc0(sizeof(CharDriverState
));
2855 s
= g_malloc0(sizeof(TCPCharDriver
));
2861 s
->read_msgfds_num
= 0;
2862 s
->write_msgfds
= 0;
2863 s
->write_msgfds_num
= 0;
2865 chr
->filename
= g_malloc(256);
2866 switch (ss
.ss_family
) {
2870 snprintf(chr
->filename
, 256, "unix:%s%s",
2871 ((struct sockaddr_un
*)(&ss
))->sun_path
,
2872 is_listen
? ",server" : "");
2880 s
->do_nodelay
= do_nodelay
;
2881 getnameinfo((struct sockaddr
*) &ss
, ss_len
, host
, sizeof(host
),
2882 serv
, sizeof(serv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
2883 snprintf(chr
->filename
, 256, "%s:%s%s%s:%s%s",
2884 is_telnet
? "telnet" : "tcp",
2885 left
, host
, right
, serv
,
2886 is_listen
? ",server" : "");
2891 chr
->chr_write
= tcp_chr_write
;
2892 chr
->chr_sync_read
= tcp_chr_sync_read
;
2893 chr
->chr_close
= tcp_chr_close
;
2894 chr
->get_msgfds
= tcp_get_msgfds
;
2895 chr
->set_msgfds
= tcp_set_msgfds
;
2896 chr
->chr_add_client
= tcp_chr_add_client
;
2897 chr
->chr_add_watch
= tcp_chr_add_watch
;
2898 chr
->chr_update_read_handler
= tcp_chr_update_read_handler
;
2899 /* be isn't opened until we get a connection */
2900 chr
->explicit_be_open
= true;
2904 s
->listen_chan
= io_channel_from_socket(s
->listen_fd
);
2905 s
->listen_tag
= g_io_add_watch(s
->listen_chan
, G_IO_IN
, tcp_chr_accept
, chr
);
2907 s
->do_telnetopt
= 1;
2912 socket_set_nodelay(fd
);
2913 s
->chan
= io_channel_from_socket(s
->fd
);
2914 tcp_chr_connect(chr
);
2917 if (is_listen
&& is_waitconnect
) {
2918 fprintf(stderr
, "QEMU waiting for connection on: %s\n",
2920 tcp_chr_accept(s
->listen_chan
, G_IO_IN
, chr
);
2921 qemu_set_nonblock(s
->listen_fd
);
2926 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2928 CharDriverState
*chr
= NULL
;
2929 Error
*local_err
= NULL
;
2932 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
2933 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
2934 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
2935 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
2936 bool is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2940 fd
= unix_listen_opts(opts
, &local_err
);
2942 fd
= unix_connect_opts(opts
, &local_err
, NULL
, NULL
);
2946 fd
= inet_listen_opts(opts
, 0, &local_err
);
2948 fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
2955 if (!is_waitconnect
)
2956 qemu_set_nonblock(fd
);
2958 chr
= qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
, is_telnet
,
2959 is_waitconnect
, &local_err
);
2968 qerror_report_err(local_err
);
2969 error_free(local_err
);
2975 g_free(chr
->opaque
);
2981 /*********************************************************/
2982 /* Ring buffer chardev */
2989 } RingBufCharDriver
;
2991 static size_t ringbuf_count(const CharDriverState
*chr
)
2993 const RingBufCharDriver
*d
= chr
->opaque
;
2995 return d
->prod
- d
->cons
;
2998 static int ringbuf_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
3000 RingBufCharDriver
*d
= chr
->opaque
;
3003 if (!buf
|| (len
< 0)) {
3007 for (i
= 0; i
< len
; i
++ ) {
3008 d
->cbuf
[d
->prod
++ & (d
->size
- 1)] = buf
[i
];
3009 if (d
->prod
- d
->cons
> d
->size
) {
3010 d
->cons
= d
->prod
- d
->size
;
3017 static int ringbuf_chr_read(CharDriverState
*chr
, uint8_t *buf
, int len
)
3019 RingBufCharDriver
*d
= chr
->opaque
;
3022 for (i
= 0; i
< len
&& d
->cons
!= d
->prod
; i
++) {
3023 buf
[i
] = d
->cbuf
[d
->cons
++ & (d
->size
- 1)];
3029 static void ringbuf_chr_close(struct CharDriverState
*chr
)
3031 RingBufCharDriver
*d
= chr
->opaque
;
3038 static CharDriverState
*qemu_chr_open_ringbuf(ChardevRingbuf
*opts
,
3041 CharDriverState
*chr
;
3042 RingBufCharDriver
*d
;
3044 chr
= g_malloc0(sizeof(CharDriverState
));
3045 d
= g_malloc(sizeof(*d
));
3047 d
->size
= opts
->has_size
? opts
->size
: 65536;
3049 /* The size must be power of 2 */
3050 if (d
->size
& (d
->size
- 1)) {
3051 error_setg(errp
, "size of ringbuf chardev must be power of two");
3057 d
->cbuf
= g_malloc0(d
->size
);
3060 chr
->chr_write
= ringbuf_chr_write
;
3061 chr
->chr_close
= ringbuf_chr_close
;
3071 bool chr_is_ringbuf(const CharDriverState
*chr
)
3073 return chr
->chr_write
== ringbuf_chr_write
;
3076 void qmp_ringbuf_write(const char *device
, const char *data
,
3077 bool has_format
, enum DataFormat format
,
3080 CharDriverState
*chr
;
3081 const uint8_t *write_data
;
3085 chr
= qemu_chr_find(device
);
3087 error_setg(errp
, "Device '%s' not found", device
);
3091 if (!chr_is_ringbuf(chr
)) {
3092 error_setg(errp
,"%s is not a ringbuf device", device
);
3096 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3097 write_data
= g_base64_decode(data
, &write_count
);
3099 write_data
= (uint8_t *)data
;
3100 write_count
= strlen(data
);
3103 ret
= ringbuf_chr_write(chr
, write_data
, write_count
);
3105 if (write_data
!= (uint8_t *)data
) {
3106 g_free((void *)write_data
);
3110 error_setg(errp
, "Failed to write to device %s", device
);
3115 char *qmp_ringbuf_read(const char *device
, int64_t size
,
3116 bool has_format
, enum DataFormat format
,
3119 CharDriverState
*chr
;
3124 chr
= qemu_chr_find(device
);
3126 error_setg(errp
, "Device '%s' not found", device
);
3130 if (!chr_is_ringbuf(chr
)) {
3131 error_setg(errp
,"%s is not a ringbuf device", device
);
3136 error_setg(errp
, "size must be greater than zero");
3140 count
= ringbuf_count(chr
);
3141 size
= size
> count
? count
: size
;
3142 read_data
= g_malloc(size
+ 1);
3144 ringbuf_chr_read(chr
, read_data
, size
);
3146 if (has_format
&& (format
== DATA_FORMAT_BASE64
)) {
3147 data
= g_base64_encode(read_data
, size
);
3151 * FIXME should read only complete, valid UTF-8 characters up
3152 * to @size bytes. Invalid sequences should be replaced by a
3153 * suitable replacement character. Except when (and only
3154 * when) ring buffer lost characters since last read, initial
3155 * continuation characters should be dropped.
3157 read_data
[size
] = 0;
3158 data
= (char *)read_data
;
3164 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
3166 char host
[65], port
[33], width
[8], height
[8];
3170 Error
*local_err
= NULL
;
3172 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
3174 qerror_report_err(local_err
);
3175 error_free(local_err
);
3179 if (strstart(filename
, "mon:", &p
)) {
3181 qemu_opt_set(opts
, "mux", "on");
3182 if (strcmp(filename
, "stdio") == 0) {
3183 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
3184 * but pass it to the guest. Handle this only for compat syntax,
3185 * for -chardev syntax we have special option for this.
3186 * This is what -nographic did, redirecting+muxing serial+monitor
3187 * to stdio causing Ctrl+C to be passed to guest. */
3188 qemu_opt_set(opts
, "signal", "off");
3192 if (strcmp(filename
, "null") == 0 ||
3193 strcmp(filename
, "pty") == 0 ||
3194 strcmp(filename
, "msmouse") == 0 ||
3195 strcmp(filename
, "braille") == 0 ||
3196 strcmp(filename
, "stdio") == 0) {
3197 qemu_opt_set(opts
, "backend", filename
);
3200 if (strstart(filename
, "vc", &p
)) {
3201 qemu_opt_set(opts
, "backend", "vc");
3203 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
3205 qemu_opt_set(opts
, "width", width
);
3206 qemu_opt_set(opts
, "height", height
);
3207 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
3209 qemu_opt_set(opts
, "cols", width
);
3210 qemu_opt_set(opts
, "rows", height
);
3217 if (strcmp(filename
, "con:") == 0) {
3218 qemu_opt_set(opts
, "backend", "console");
3221 if (strstart(filename
, "COM", NULL
)) {
3222 qemu_opt_set(opts
, "backend", "serial");
3223 qemu_opt_set(opts
, "path", filename
);
3226 if (strstart(filename
, "file:", &p
)) {
3227 qemu_opt_set(opts
, "backend", "file");
3228 qemu_opt_set(opts
, "path", p
);
3231 if (strstart(filename
, "pipe:", &p
)) {
3232 qemu_opt_set(opts
, "backend", "pipe");
3233 qemu_opt_set(opts
, "path", p
);
3236 if (strstart(filename
, "tcp:", &p
) ||
3237 strstart(filename
, "telnet:", &p
)) {
3238 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3240 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
3243 qemu_opt_set(opts
, "backend", "socket");
3244 qemu_opt_set(opts
, "host", host
);
3245 qemu_opt_set(opts
, "port", port
);
3246 if (p
[pos
] == ',') {
3247 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
3250 if (strstart(filename
, "telnet:", &p
))
3251 qemu_opt_set(opts
, "telnet", "on");
3254 if (strstart(filename
, "udp:", &p
)) {
3255 qemu_opt_set(opts
, "backend", "udp");
3256 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
3258 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
3262 qemu_opt_set(opts
, "host", host
);
3263 qemu_opt_set(opts
, "port", port
);
3264 if (p
[pos
] == '@') {
3266 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
3268 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
3272 qemu_opt_set(opts
, "localaddr", host
);
3273 qemu_opt_set(opts
, "localport", port
);
3277 if (strstart(filename
, "unix:", &p
)) {
3278 qemu_opt_set(opts
, "backend", "socket");
3279 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
3283 if (strstart(filename
, "/dev/parport", NULL
) ||
3284 strstart(filename
, "/dev/ppi", NULL
)) {
3285 qemu_opt_set(opts
, "backend", "parport");
3286 qemu_opt_set(opts
, "path", filename
);
3289 if (strstart(filename
, "/dev/", NULL
)) {
3290 qemu_opt_set(opts
, "backend", "tty");
3291 qemu_opt_set(opts
, "path", filename
);
3296 qemu_opts_del(opts
);
3300 static void qemu_chr_parse_file_out(QemuOpts
*opts
, ChardevBackend
*backend
,
3303 const char *path
= qemu_opt_get(opts
, "path");
3306 error_setg(errp
, "chardev: file: no filename given");
3309 backend
->file
= g_new0(ChardevFile
, 1);
3310 backend
->file
->out
= g_strdup(path
);
3313 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
3316 backend
->stdio
= g_new0(ChardevStdio
, 1);
3317 backend
->stdio
->has_signal
= true;
3318 backend
->stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
3321 static void qemu_chr_parse_serial(QemuOpts
*opts
, ChardevBackend
*backend
,
3324 const char *device
= qemu_opt_get(opts
, "path");
3326 if (device
== NULL
) {
3327 error_setg(errp
, "chardev: serial/tty: no device path given");
3330 backend
->serial
= g_new0(ChardevHostdev
, 1);
3331 backend
->serial
->device
= g_strdup(device
);
3334 static void qemu_chr_parse_parallel(QemuOpts
*opts
, ChardevBackend
*backend
,
3337 const char *device
= qemu_opt_get(opts
, "path");
3339 if (device
== NULL
) {
3340 error_setg(errp
, "chardev: parallel: no device path given");
3343 backend
->parallel
= g_new0(ChardevHostdev
, 1);
3344 backend
->parallel
->device
= g_strdup(device
);
3347 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
3350 const char *device
= qemu_opt_get(opts
, "path");
3352 if (device
== NULL
) {
3353 error_setg(errp
, "chardev: pipe: no device path given");
3356 backend
->pipe
= g_new0(ChardevHostdev
, 1);
3357 backend
->pipe
->device
= g_strdup(device
);
3360 static void qemu_chr_parse_ringbuf(QemuOpts
*opts
, ChardevBackend
*backend
,
3365 backend
->ringbuf
= g_new0(ChardevRingbuf
, 1);
3367 val
= qemu_opt_get_size(opts
, "size", 0);
3369 backend
->ringbuf
->has_size
= true;
3370 backend
->ringbuf
->size
= val
;
3374 static void qemu_chr_parse_mux(QemuOpts
*opts
, ChardevBackend
*backend
,
3377 const char *chardev
= qemu_opt_get(opts
, "chardev");
3379 if (chardev
== NULL
) {
3380 error_setg(errp
, "chardev: mux: no chardev given");
3383 backend
->mux
= g_new0(ChardevMux
, 1);
3384 backend
->mux
->chardev
= g_strdup(chardev
);
3387 typedef struct CharDriver
{
3390 CharDriverState
*(*open
)(QemuOpts
*opts
);
3391 /* new, qapi-based */
3392 ChardevBackendKind kind
;
3393 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
3396 static GSList
*backends
;
3398 void register_char_driver(const char *name
, CharDriverState
*(*open
)(QemuOpts
*))
3402 s
= g_malloc0(sizeof(*s
));
3403 s
->name
= g_strdup(name
);
3406 backends
= g_slist_append(backends
, s
);
3409 void register_char_driver_qapi(const char *name
, ChardevBackendKind kind
,
3410 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
))
3414 s
= g_malloc0(sizeof(*s
));
3415 s
->name
= g_strdup(name
);
3419 backends
= g_slist_append(backends
, s
);
3422 CharDriverState
*qemu_chr_new_from_opts(QemuOpts
*opts
,
3423 void (*init
)(struct CharDriverState
*s
),
3426 Error
*local_err
= NULL
;
3428 CharDriverState
*chr
;
3431 if (qemu_opts_id(opts
) == NULL
) {
3432 error_setg(errp
, "chardev: no id specified");
3436 if (qemu_opt_get(opts
, "backend") == NULL
) {
3437 error_setg(errp
, "chardev: \"%s\" missing backend",
3438 qemu_opts_id(opts
));
3441 for (i
= backends
; i
; i
= i
->next
) {
3444 if (strcmp(cd
->name
, qemu_opt_get(opts
, "backend")) == 0) {
3449 error_setg(errp
, "chardev: backend \"%s\" not found",
3450 qemu_opt_get(opts
, "backend"));
3455 /* using new, qapi init */
3456 ChardevBackend
*backend
= g_new0(ChardevBackend
, 1);
3457 ChardevReturn
*ret
= NULL
;
3458 const char *id
= qemu_opts_id(opts
);
3461 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3462 bid
= g_strdup_printf("%s-base", id
);
3466 backend
->kind
= cd
->kind
;
3468 cd
->parse(opts
, backend
, &local_err
);
3470 error_propagate(errp
, local_err
);
3474 ret
= qmp_chardev_add(bid
? bid
: id
, backend
, errp
);
3480 qapi_free_ChardevBackend(backend
);
3481 qapi_free_ChardevReturn(ret
);
3482 backend
= g_new0(ChardevBackend
, 1);
3483 backend
->mux
= g_new0(ChardevMux
, 1);
3484 backend
->kind
= CHARDEV_BACKEND_KIND_MUX
;
3485 backend
->mux
->chardev
= g_strdup(bid
);
3486 ret
= qmp_chardev_add(id
, backend
, errp
);
3488 chr
= qemu_chr_find(bid
);
3489 qemu_chr_delete(chr
);
3495 chr
= qemu_chr_find(id
);
3499 qapi_free_ChardevBackend(backend
);
3500 qapi_free_ChardevReturn(ret
);
3505 chr
= cd
->open(opts
);
3507 error_setg(errp
, "chardev: opening backend \"%s\" failed",
3508 qemu_opt_get(opts
, "backend"));
3513 chr
->filename
= g_strdup(qemu_opt_get(opts
, "backend"));
3515 /* if we didn't create the chardev via qmp_chardev_add, we
3516 * need to send the OPENED event here
3518 if (!chr
->explicit_be_open
) {
3519 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
3521 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3523 if (qemu_opt_get_bool(opts
, "mux", 0)) {
3524 CharDriverState
*base
= chr
;
3525 int len
= strlen(qemu_opts_id(opts
)) + 6;
3526 base
->label
= g_malloc(len
);
3527 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
3528 chr
= qemu_chr_open_mux(base
);
3529 chr
->filename
= base
->filename
;
3530 chr
->avail_connections
= MAX_MUX
;
3531 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
3533 chr
->avail_connections
= 1;
3535 chr
->label
= g_strdup(qemu_opts_id(opts
));
3540 qemu_opts_del(opts
);
3544 CharDriverState
*qemu_chr_new(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
3547 CharDriverState
*chr
;
3551 if (strstart(filename
, "chardev:", &p
)) {
3552 return qemu_chr_find(p
);
3555 opts
= qemu_chr_parse_compat(label
, filename
);
3559 chr
= qemu_chr_new_from_opts(opts
, init
, &err
);
3561 error_report("%s", error_get_pretty(err
));
3564 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
3565 qemu_chr_fe_claim_no_fail(chr
);
3566 monitor_init(chr
, MONITOR_USE_READLINE
);
3571 void qemu_chr_fe_set_echo(struct CharDriverState
*chr
, bool echo
)
3573 if (chr
->chr_set_echo
) {
3574 chr
->chr_set_echo(chr
, echo
);
3578 void qemu_chr_fe_set_open(struct CharDriverState
*chr
, int fe_open
)
3580 if (chr
->fe_open
== fe_open
) {
3583 chr
->fe_open
= fe_open
;
3584 if (chr
->chr_set_fe_open
) {
3585 chr
->chr_set_fe_open(chr
, fe_open
);
3589 void qemu_chr_fe_event(struct CharDriverState
*chr
, int event
)
3591 if (chr
->chr_fe_event
) {
3592 chr
->chr_fe_event(chr
, event
);
3596 int qemu_chr_fe_add_watch(CharDriverState
*s
, GIOCondition cond
,
3597 GIOFunc func
, void *user_data
)
3602 if (s
->chr_add_watch
== NULL
) {
3606 src
= s
->chr_add_watch(s
, cond
);
3607 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
3608 tag
= g_source_attach(src
, NULL
);
3609 g_source_unref(src
);
3614 int qemu_chr_fe_claim(CharDriverState
*s
)
3616 if (s
->avail_connections
< 1) {
3619 s
->avail_connections
--;
3623 void qemu_chr_fe_claim_no_fail(CharDriverState
*s
)
3625 if (qemu_chr_fe_claim(s
) != 0) {
3626 fprintf(stderr
, "%s: error chardev \"%s\" already used\n",
3627 __func__
, s
->label
);
3632 void qemu_chr_fe_release(CharDriverState
*s
)
3634 s
->avail_connections
++;
3637 void qemu_chr_delete(CharDriverState
*chr
)
3639 QTAILQ_REMOVE(&chardevs
, chr
, next
);
3640 if (chr
->chr_close
) {
3641 chr
->chr_close(chr
);
3643 g_free(chr
->filename
);
3646 qemu_opts_del(chr
->opts
);
3651 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
3653 ChardevInfoList
*chr_list
= NULL
;
3654 CharDriverState
*chr
;
3656 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3657 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
3658 info
->value
= g_malloc0(sizeof(*info
->value
));
3659 info
->value
->label
= g_strdup(chr
->label
);
3660 info
->value
->filename
= g_strdup(chr
->filename
);
3662 info
->next
= chr_list
;
3669 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
3671 ChardevBackendInfoList
*backend_list
= NULL
;
3672 CharDriver
*c
= NULL
;
3675 for (i
= backends
; i
; i
= i
->next
) {
3676 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
3678 info
->value
= g_malloc0(sizeof(*info
->value
));
3679 info
->value
->name
= g_strdup(c
->name
);
3681 info
->next
= backend_list
;
3682 backend_list
= info
;
3685 return backend_list
;
3688 CharDriverState
*qemu_chr_find(const char *name
)
3690 CharDriverState
*chr
;
3692 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
3693 if (strcmp(chr
->label
, name
) != 0)
3700 /* Get a character (serial) device interface. */
3701 CharDriverState
*qemu_char_get_next_serial(void)
3703 static int next_serial
;
3704 CharDriverState
*chr
;
3706 /* FIXME: This function needs to go away: use chardev properties! */
3708 while (next_serial
< MAX_SERIAL_PORTS
&& serial_hds
[next_serial
]) {
3709 chr
= serial_hds
[next_serial
++];
3710 qemu_chr_fe_claim_no_fail(chr
);
3716 QemuOptsList qemu_chardev_opts
= {
3718 .implied_opt_name
= "backend",
3719 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
3723 .type
= QEMU_OPT_STRING
,
3726 .type
= QEMU_OPT_STRING
,
3729 .type
= QEMU_OPT_STRING
,
3732 .type
= QEMU_OPT_STRING
,
3734 .name
= "localaddr",
3735 .type
= QEMU_OPT_STRING
,
3737 .name
= "localport",
3738 .type
= QEMU_OPT_STRING
,
3741 .type
= QEMU_OPT_NUMBER
,
3744 .type
= QEMU_OPT_BOOL
,
3747 .type
= QEMU_OPT_BOOL
,
3750 .type
= QEMU_OPT_BOOL
,
3753 .type
= QEMU_OPT_BOOL
,
3756 .type
= QEMU_OPT_BOOL
,
3759 .type
= QEMU_OPT_BOOL
,
3762 .type
= QEMU_OPT_NUMBER
,
3765 .type
= QEMU_OPT_NUMBER
,
3768 .type
= QEMU_OPT_NUMBER
,
3771 .type
= QEMU_OPT_NUMBER
,
3774 .type
= QEMU_OPT_BOOL
,
3777 .type
= QEMU_OPT_BOOL
,
3780 .type
= QEMU_OPT_STRING
,
3783 .type
= QEMU_OPT_NUMBER
,
3786 .type
= QEMU_OPT_SIZE
,
3789 .type
= QEMU_OPT_STRING
,
3791 { /* end of list */ }
3797 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3802 error_setg(errp
, "input file not supported");
3806 out
= CreateFile(file
->out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
3807 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3808 if (out
== INVALID_HANDLE_VALUE
) {
3809 error_setg(errp
, "open %s failed", file
->out
);
3812 return qemu_chr_open_win_file(out
);
3815 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3818 return qemu_chr_open_win_path(serial
->device
);
3821 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3824 error_setg(errp
, "character device backend type 'parallel' not supported");
3830 static int qmp_chardev_open_file_source(char *src
, int flags
,
3835 TFR(fd
= qemu_open(src
, flags
, 0666));
3837 error_setg_file_open(errp
, errno
, src
);
3842 static CharDriverState
*qmp_chardev_open_file(ChardevFile
*file
, Error
**errp
)
3844 int flags
, in
= -1, out
;
3846 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
;
3847 out
= qmp_chardev_open_file_source(file
->out
, flags
, errp
);
3854 in
= qmp_chardev_open_file_source(file
->in
, flags
, errp
);
3861 return qemu_chr_open_fd(in
, out
);
3864 static CharDriverState
*qmp_chardev_open_serial(ChardevHostdev
*serial
,
3867 #ifdef HAVE_CHARDEV_TTY
3870 fd
= qmp_chardev_open_file_source(serial
->device
, O_RDWR
, errp
);
3874 qemu_set_nonblock(fd
);
3875 return qemu_chr_open_tty_fd(fd
);
3877 error_setg(errp
, "character device backend type 'serial' not supported");
3882 static CharDriverState
*qmp_chardev_open_parallel(ChardevHostdev
*parallel
,
3885 #ifdef HAVE_CHARDEV_PARPORT
3888 fd
= qmp_chardev_open_file_source(parallel
->device
, O_RDWR
, errp
);
3892 return qemu_chr_open_pp_fd(fd
);
3894 error_setg(errp
, "character device backend type 'parallel' not supported");
3901 static CharDriverState
*qmp_chardev_open_socket(ChardevSocket
*sock
,
3904 SocketAddress
*addr
= sock
->addr
;
3905 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
3906 bool is_listen
= sock
->has_server
? sock
->server
: true;
3907 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
3908 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
3912 fd
= socket_listen(addr
, errp
);
3914 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
3919 return qemu_chr_open_socket_fd(fd
, do_nodelay
, is_listen
,
3920 is_telnet
, is_waitconnect
, errp
);
3923 static CharDriverState
*qmp_chardev_open_udp(ChardevUdp
*udp
,
3928 fd
= socket_dgram(udp
->remote
, udp
->local
, errp
);
3932 return qemu_chr_open_udp_fd(fd
);
3935 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
3938 ChardevReturn
*ret
= g_new0(ChardevReturn
, 1);
3939 CharDriverState
*base
, *chr
= NULL
;
3941 chr
= qemu_chr_find(id
);
3943 error_setg(errp
, "Chardev '%s' already exists", id
);
3948 switch (backend
->kind
) {
3949 case CHARDEV_BACKEND_KIND_FILE
:
3950 chr
= qmp_chardev_open_file(backend
->file
, errp
);
3952 case CHARDEV_BACKEND_KIND_SERIAL
:
3953 chr
= qmp_chardev_open_serial(backend
->serial
, errp
);
3955 case CHARDEV_BACKEND_KIND_PARALLEL
:
3956 chr
= qmp_chardev_open_parallel(backend
->parallel
, errp
);
3958 case CHARDEV_BACKEND_KIND_PIPE
:
3959 chr
= qemu_chr_open_pipe(backend
->pipe
);
3961 case CHARDEV_BACKEND_KIND_SOCKET
:
3962 chr
= qmp_chardev_open_socket(backend
->socket
, errp
);
3964 case CHARDEV_BACKEND_KIND_UDP
:
3965 chr
= qmp_chardev_open_udp(backend
->udp
, errp
);
3967 #ifdef HAVE_CHARDEV_TTY
3968 case CHARDEV_BACKEND_KIND_PTY
:
3969 chr
= qemu_chr_open_pty(id
, ret
);
3972 case CHARDEV_BACKEND_KIND_NULL
:
3973 chr
= qemu_chr_open_null();
3975 case CHARDEV_BACKEND_KIND_MUX
:
3976 base
= qemu_chr_find(backend
->mux
->chardev
);
3978 error_setg(errp
, "mux: base chardev %s not found",
3979 backend
->mux
->chardev
);
3982 chr
= qemu_chr_open_mux(base
);
3984 case CHARDEV_BACKEND_KIND_MSMOUSE
:
3985 chr
= qemu_chr_open_msmouse();
3987 #ifdef CONFIG_BRLAPI
3988 case CHARDEV_BACKEND_KIND_BRAILLE
:
3989 chr
= chr_baum_init();
3992 case CHARDEV_BACKEND_KIND_STDIO
:
3993 chr
= qemu_chr_open_stdio(backend
->stdio
);
3996 case CHARDEV_BACKEND_KIND_CONSOLE
:
3997 chr
= qemu_chr_open_win_con();
4001 case CHARDEV_BACKEND_KIND_SPICEVMC
:
4002 chr
= qemu_chr_open_spice_vmc(backend
->spicevmc
->type
);
4004 case CHARDEV_BACKEND_KIND_SPICEPORT
:
4005 chr
= qemu_chr_open_spice_port(backend
->spiceport
->fqdn
);
4008 case CHARDEV_BACKEND_KIND_VC
:
4009 chr
= vc_init(backend
->vc
);
4011 case CHARDEV_BACKEND_KIND_RINGBUF
:
4012 case CHARDEV_BACKEND_KIND_MEMORY
:
4013 chr
= qemu_chr_open_ringbuf(backend
->ringbuf
, errp
);
4016 error_setg(errp
, "unknown chardev backend (%d)", backend
->kind
);
4021 * Character backend open hasn't been fully converted to the Error
4022 * API. Some opens fail without setting an error. Set a generic
4024 * TODO full conversion to Error API
4026 if (chr
== NULL
&& errp
&& !*errp
) {
4027 error_setg(errp
, "Failed to create chardev");
4030 chr
->label
= g_strdup(id
);
4031 chr
->avail_connections
=
4032 (backend
->kind
== CHARDEV_BACKEND_KIND_MUX
) ? MAX_MUX
: 1;
4033 if (!chr
->filename
) {
4034 chr
->filename
= g_strdup(ChardevBackendKind_lookup
[backend
->kind
]);
4036 if (!chr
->explicit_be_open
) {
4037 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
4039 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
4047 void qmp_chardev_remove(const char *id
, Error
**errp
)
4049 CharDriverState
*chr
;
4051 chr
= qemu_chr_find(id
);
4053 error_setg(errp
, "Chardev '%s' not found", id
);
4056 if (chr
->chr_can_read
|| chr
->chr_read
||
4057 chr
->chr_event
|| chr
->handler_opaque
) {
4058 error_setg(errp
, "Chardev '%s' is busy", id
);
4061 qemu_chr_delete(chr
);
4064 static void register_types(void)
4066 register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL
, NULL
);
4067 register_char_driver("socket", qemu_chr_open_socket
);
4068 register_char_driver("udp", qemu_chr_open_udp
);
4069 register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF
,
4070 qemu_chr_parse_ringbuf
);
4071 register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE
,
4072 qemu_chr_parse_file_out
);
4073 register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO
,
4074 qemu_chr_parse_stdio
);
4075 register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL
,
4076 qemu_chr_parse_serial
);
4077 register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL
,
4078 qemu_chr_parse_serial
);
4079 register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL
,
4080 qemu_chr_parse_parallel
);
4081 register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL
,
4082 qemu_chr_parse_parallel
);
4083 register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY
, NULL
);
4084 register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE
, NULL
);
4085 register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE
,
4086 qemu_chr_parse_pipe
);
4087 register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX
,
4088 qemu_chr_parse_mux
);
4089 /* Bug-compatibility: */
4090 register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY
,
4091 qemu_chr_parse_ringbuf
);
4092 /* this must be done after machine init, since we register FEs with muxes
4093 * as part of realize functions like serial_isa_realizefn when -nographic
4096 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
4099 type_init(register_types
);