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/osdep.h"
25 #include "qemu-common.h"
26 #include "qemu/cutils.h"
27 #include "monitor/monitor.h"
28 #include "sysemu/sysemu.h"
29 #include "qemu/config-file.h"
30 #include "qemu/error-report.h"
31 #include "sysemu/char.h"
32 #include "qmp-commands.h"
33 #include "qapi-visit.h"
34 #include "sysemu/replay.h"
35 #include "qemu/help_option.h"
39 #include "char-parallel.h"
40 #include "char-serial.h"
42 /***********************************************************/
43 /* character device */
45 static QTAILQ_HEAD(ChardevHead
, Chardev
) chardevs
=
46 QTAILQ_HEAD_INITIALIZER(chardevs
);
48 void qemu_chr_be_event(Chardev
*s
, int event
)
50 CharBackend
*be
= s
->be
;
52 /* Keep track if the char device is open */
54 case CHR_EVENT_OPENED
:
57 case CHR_EVENT_CLOSED
:
62 if (!be
|| !be
->chr_event
) {
66 be
->chr_event(be
->opaque
, event
);
69 void qemu_chr_be_generic_open(Chardev
*s
)
71 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
75 /* Not reporting errors from writing to logfile, as logs are
76 * defined to be "best effort" only */
77 static void qemu_chr_fe_write_log(Chardev
*s
,
78 const uint8_t *buf
, size_t len
)
89 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
90 if (ret
== -1 && errno
== EAGAIN
) {
102 static int qemu_chr_fe_write_buffer(Chardev
*s
,
103 const uint8_t *buf
, int len
, int *offset
)
105 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
109 qemu_mutex_lock(&s
->chr_write_lock
);
110 while (*offset
< len
) {
112 res
= cc
->chr_write(s
, buf
+ *offset
, len
- *offset
);
113 if (res
< 0 && errno
== EAGAIN
) {
125 qemu_chr_fe_write_log(s
, buf
, *offset
);
127 qemu_mutex_unlock(&s
->chr_write_lock
);
132 static bool qemu_chr_replay(Chardev
*chr
)
134 return qemu_chr_has_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
137 int qemu_chr_fe_write(CharBackend
*be
, const uint8_t *buf
, int len
)
139 Chardev
*s
= be
->chr
;
147 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
149 replay_char_write_event_load(&ret
, &offset
);
150 assert(offset
<= len
);
151 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
155 cc
= CHARDEV_GET_CLASS(s
);
156 qemu_mutex_lock(&s
->chr_write_lock
);
157 ret
= cc
->chr_write(s
, buf
, len
);
160 qemu_chr_fe_write_log(s
, buf
, ret
);
163 qemu_mutex_unlock(&s
->chr_write_lock
);
165 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
166 replay_char_write_event_save(ret
, ret
< 0 ? 0 : ret
);
172 int qemu_chr_write_all(Chardev
*s
, const uint8_t *buf
, int len
)
177 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
178 replay_char_write_event_load(&res
, &offset
);
179 assert(offset
<= len
);
180 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
184 res
= qemu_chr_fe_write_buffer(s
, buf
, len
, &offset
);
186 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
187 replay_char_write_event_save(res
, offset
);
196 int qemu_chr_fe_write_all(CharBackend
*be
, const uint8_t *buf
, int len
)
198 Chardev
*s
= be
->chr
;
204 return qemu_chr_write_all(s
, buf
, len
);
207 int qemu_chr_fe_read_all(CharBackend
*be
, uint8_t *buf
, int len
)
209 Chardev
*s
= be
->chr
;
210 int offset
= 0, counter
= 10;
213 if (!s
|| !CHARDEV_GET_CLASS(s
)->chr_sync_read
) {
217 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
218 return replay_char_read_all_load(buf
);
221 while (offset
< len
) {
223 res
= CHARDEV_GET_CLASS(s
)->chr_sync_read(s
, buf
+ offset
,
225 if (res
== -1 && errno
== EAGAIN
) {
235 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
236 replay_char_read_all_save_error(res
);
248 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
249 replay_char_read_all_save_buf(buf
, offset
);
254 int qemu_chr_fe_ioctl(CharBackend
*be
, int cmd
, void *arg
)
256 Chardev
*s
= be
->chr
;
259 if (!s
|| !CHARDEV_GET_CLASS(s
)->chr_ioctl
|| qemu_chr_replay(s
)) {
262 res
= CHARDEV_GET_CLASS(s
)->chr_ioctl(s
, cmd
, arg
);
268 int qemu_chr_be_can_write(Chardev
*s
)
270 CharBackend
*be
= s
->be
;
272 if (!be
|| !be
->chr_can_read
) {
276 return be
->chr_can_read(be
->opaque
);
279 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
)
281 CharBackend
*be
= s
->be
;
283 if (be
&& be
->chr_read
) {
284 be
->chr_read(be
->opaque
, buf
, len
);
288 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
)
290 if (qemu_chr_replay(s
)) {
291 if (replay_mode
== REPLAY_MODE_PLAY
) {
294 replay_chr_be_write(s
, buf
, len
);
296 qemu_chr_be_write_impl(s
, buf
, len
);
300 int qemu_chr_fe_get_msgfd(CharBackend
*be
)
302 Chardev
*s
= be
->chr
;
304 int res
= (qemu_chr_fe_get_msgfds(be
, &fd
, 1) == 1) ? fd
: -1;
305 if (s
&& qemu_chr_replay(s
)) {
306 error_report("Replay: get msgfd is not supported "
307 "for serial devices yet");
313 int qemu_chr_fe_get_msgfds(CharBackend
*be
, int *fds
, int len
)
315 Chardev
*s
= be
->chr
;
321 return CHARDEV_GET_CLASS(s
)->get_msgfds
?
322 CHARDEV_GET_CLASS(s
)->get_msgfds(s
, fds
, len
) : -1;
325 int qemu_chr_fe_set_msgfds(CharBackend
*be
, int *fds
, int num
)
327 Chardev
*s
= be
->chr
;
333 return CHARDEV_GET_CLASS(s
)->set_msgfds
?
334 CHARDEV_GET_CLASS(s
)->set_msgfds(s
, fds
, num
) : -1;
337 int qemu_chr_add_client(Chardev
*s
, int fd
)
339 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
340 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
343 void qemu_chr_fe_accept_input(CharBackend
*be
)
345 Chardev
*s
= be
->chr
;
351 if (CHARDEV_GET_CLASS(s
)->chr_accept_input
) {
352 CHARDEV_GET_CLASS(s
)->chr_accept_input(s
);
357 void qemu_chr_fe_printf(CharBackend
*be
, const char *fmt
, ...)
359 char buf
[CHR_READ_BUF_LEN
];
362 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
363 /* XXX this blocks entire thread. Rewrite to use
364 * qemu_chr_fe_write and background I/O callbacks */
365 qemu_chr_fe_write_all(be
, (uint8_t *)buf
, strlen(buf
));
369 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
370 bool *be_opened
, Error
**errp
)
372 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
373 /* Any ChardevCommon member would work */
374 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
376 if (common
&& common
->has_logfile
) {
377 int flags
= O_WRONLY
| O_CREAT
;
378 if (common
->has_logappend
&&
384 chr
->logfd
= qemu_open(common
->logfile
, flags
, 0666);
385 if (chr
->logfd
< 0) {
386 error_setg_errno(errp
, errno
,
387 "Unable to open logfile %s",
394 cc
->open(chr
, backend
, be_opened
, errp
);
398 static void char_init(Object
*obj
)
400 Chardev
*chr
= CHARDEV(obj
);
403 qemu_mutex_init(&chr
->chr_write_lock
);
406 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
411 static void char_class_init(ObjectClass
*oc
, void *data
)
413 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
415 cc
->chr_write
= null_chr_write
;
418 static void char_finalize(Object
*obj
)
420 Chardev
*chr
= CHARDEV(obj
);
425 g_free(chr
->filename
);
427 if (chr
->logfd
!= -1) {
430 qemu_mutex_destroy(&chr
->chr_write_lock
);
433 static const TypeInfo char_type_info
= {
434 .name
= TYPE_CHARDEV
,
435 .parent
= TYPE_OBJECT
,
436 .instance_size
= sizeof(Chardev
),
437 .instance_init
= char_init
,
438 .instance_finalize
= char_finalize
,
440 .class_size
= sizeof(ChardevClass
),
441 .class_init
= char_class_init
,
445 * Called after processing of default and command-line-specified
446 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
447 * to a mux chardev. This is done here to ensure that
448 * output/prompts/banners are only displayed for the FE that has
449 * focus when initial command-line processing/machine init is
452 * After this point, any new FE attached to any new or existing
453 * mux will receive CHR_EVENT_OPENED notifications for the BE
456 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
460 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
461 if (CHARDEV_IS_MUX(chr
)) {
462 MuxChardev
*d
= MUX_CHARDEV(chr
);
465 /* send OPENED to all already-attached FEs */
466 for (i
= 0; i
< d
->mux_cnt
; i
++) {
467 mux_chr_send_event(d
, i
, CHR_EVENT_OPENED
);
469 /* mark mux as OPENED so any new FEs will immediately receive
472 qemu_chr_be_generic_open(chr
);
475 muxes_realized
= true;
478 static Notifier muxes_realize_notify
= {
479 .notify
= muxes_realize_done
,
482 Chardev
*qemu_chr_fe_get_driver(CharBackend
*be
)
487 bool qemu_chr_fe_init(CharBackend
*b
, Chardev
*s
, Error
**errp
)
491 if (CHARDEV_IS_MUX(s
)) {
492 MuxChardev
*d
= MUX_CHARDEV(s
);
494 if (d
->mux_cnt
>= MAX_MUX
) {
498 d
->backends
[d
->mux_cnt
] = b
;
512 error_setg(errp
, QERR_DEVICE_IN_USE
, s
->label
);
516 static bool qemu_chr_is_busy(Chardev
*s
)
518 if (CHARDEV_IS_MUX(s
)) {
519 MuxChardev
*d
= MUX_CHARDEV(s
);
520 return d
->mux_cnt
>= 0;
522 return s
->be
!= NULL
;
526 void qemu_chr_fe_deinit(CharBackend
*b
)
531 qemu_chr_fe_set_handlers(b
, NULL
, NULL
, NULL
, NULL
, NULL
, true);
532 if (b
->chr
->be
== b
) {
535 if (CHARDEV_IS_MUX(b
->chr
)) {
536 MuxChardev
*d
= MUX_CHARDEV(b
->chr
);
537 d
->backends
[b
->tag
] = NULL
;
543 void qemu_chr_fe_set_handlers(CharBackend
*b
,
544 IOCanReadHandler
*fd_can_read
,
545 IOReadHandler
*fd_read
,
546 IOEventHandler
*fd_event
,
548 GMainContext
*context
,
560 cc
= CHARDEV_GET_CLASS(s
);
561 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
563 remove_fd_in_watch(s
);
567 b
->chr_can_read
= fd_can_read
;
568 b
->chr_read
= fd_read
;
569 b
->chr_event
= fd_event
;
571 if (cc
->chr_update_read_handler
) {
572 cc
->chr_update_read_handler(s
, context
);
576 qemu_chr_fe_set_open(b
, fe_open
);
580 qemu_chr_fe_take_focus(b
);
581 /* We're connecting to an already opened device, so let's make sure we
582 also get the open event */
584 qemu_chr_be_generic_open(s
);
588 if (CHARDEV_IS_MUX(s
)) {
589 mux_chr_set_handlers(s
, context
);
593 void qemu_chr_fe_take_focus(CharBackend
*b
)
599 if (CHARDEV_IS_MUX(b
->chr
)) {
600 mux_set_focus(b
->chr
, b
->tag
);
604 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
606 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
608 if (cc
->chr_wait_connected
) {
609 return cc
->chr_wait_connected(chr
, errp
);
615 int qemu_chr_fe_wait_connected(CharBackend
*be
, Error
**errp
)
618 error_setg(errp
, "missing associated backend");
622 return qemu_chr_wait_connected(be
->chr
, errp
);
625 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
627 char host
[65], port
[33], width
[8], height
[8];
631 Error
*local_err
= NULL
;
633 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
635 error_report_err(local_err
);
639 if (strstart(filename
, "mon:", &p
)) {
641 qemu_opt_set(opts
, "mux", "on", &error_abort
);
642 if (strcmp(filename
, "stdio") == 0) {
643 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
644 * but pass it to the guest. Handle this only for compat syntax,
645 * for -chardev syntax we have special option for this.
646 * This is what -nographic did, redirecting+muxing serial+monitor
647 * to stdio causing Ctrl+C to be passed to guest. */
648 qemu_opt_set(opts
, "signal", "off", &error_abort
);
652 if (strcmp(filename
, "null") == 0 ||
653 strcmp(filename
, "pty") == 0 ||
654 strcmp(filename
, "msmouse") == 0 ||
655 strcmp(filename
, "wctablet") == 0 ||
656 strcmp(filename
, "braille") == 0 ||
657 strcmp(filename
, "testdev") == 0 ||
658 strcmp(filename
, "stdio") == 0) {
659 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
662 if (strstart(filename
, "vc", &p
)) {
663 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
665 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
667 qemu_opt_set(opts
, "width", width
, &error_abort
);
668 qemu_opt_set(opts
, "height", height
, &error_abort
);
669 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
671 qemu_opt_set(opts
, "cols", width
, &error_abort
);
672 qemu_opt_set(opts
, "rows", height
, &error_abort
);
679 if (strcmp(filename
, "con:") == 0) {
680 qemu_opt_set(opts
, "backend", "console", &error_abort
);
683 if (strstart(filename
, "COM", NULL
)) {
684 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
685 qemu_opt_set(opts
, "path", filename
, &error_abort
);
688 if (strstart(filename
, "file:", &p
)) {
689 qemu_opt_set(opts
, "backend", "file", &error_abort
);
690 qemu_opt_set(opts
, "path", p
, &error_abort
);
693 if (strstart(filename
, "pipe:", &p
)) {
694 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
695 qemu_opt_set(opts
, "path", p
, &error_abort
);
698 if (strstart(filename
, "tcp:", &p
) ||
699 strstart(filename
, "telnet:", &p
)) {
700 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
702 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
705 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
706 qemu_opt_set(opts
, "host", host
, &error_abort
);
707 qemu_opt_set(opts
, "port", port
, &error_abort
);
709 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
711 error_report_err(local_err
);
715 if (strstart(filename
, "telnet:", &p
))
716 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
719 if (strstart(filename
, "udp:", &p
)) {
720 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
721 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
723 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
727 qemu_opt_set(opts
, "host", host
, &error_abort
);
728 qemu_opt_set(opts
, "port", port
, &error_abort
);
731 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
733 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
737 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
738 qemu_opt_set(opts
, "localport", port
, &error_abort
);
742 if (strstart(filename
, "unix:", &p
)) {
743 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
744 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
746 error_report_err(local_err
);
751 if (strstart(filename
, "/dev/parport", NULL
) ||
752 strstart(filename
, "/dev/ppi", NULL
)) {
753 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
754 qemu_opt_set(opts
, "path", filename
, &error_abort
);
757 if (strstart(filename
, "/dev/", NULL
)) {
758 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
759 qemu_opt_set(opts
, "path", filename
, &error_abort
);
768 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
770 const char *logfile
= qemu_opt_get(opts
, "logfile");
772 backend
->has_logfile
= logfile
!= NULL
;
773 backend
->logfile
= logfile
? g_strdup(logfile
) : NULL
;
775 backend
->has_logappend
= true;
776 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
779 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
782 const ChardevClass
*cc
;
783 char *typename
= g_strdup_printf("chardev-%s", driver
);
785 oc
= object_class_by_name(typename
);
788 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
789 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
793 if (object_class_is_abstract(oc
)) {
794 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
795 "abstract device type");
799 cc
= CHARDEV_CLASS(oc
);
801 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
808 static Chardev
*qemu_chardev_add(const char *id
, const char *typename
,
809 ChardevBackend
*backend
, Error
**errp
)
813 chr
= qemu_chr_find(id
);
815 error_setg(errp
, "Chardev '%s' already exists", id
);
819 chr
= qemu_chardev_new(id
, typename
, backend
, errp
);
824 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
828 static const struct ChardevAlias
{
829 const char *typename
;
831 } chardev_alias_table
[] = {
832 #ifdef HAVE_CHARDEV_PARPORT
833 { "parallel", "parport" },
835 #ifdef HAVE_CHARDEV_SERIAL
840 typedef struct ChadevClassFE
{
841 void (*fn
)(const char *name
, void *opaque
);
846 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
848 ChadevClassFE
*fe
= opaque
;
850 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
851 if (CHARDEV_CLASS(klass
)->internal
) {
855 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
859 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
), void *opaque
)
861 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
864 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
866 for (i
= 0; i
< ARRAY_SIZE(chardev_alias_table
); i
++) {
867 fn(chardev_alias_table
[i
].alias
, opaque
);
872 help_string_append(const char *name
, void *opaque
)
874 GString
*str
= opaque
;
876 g_string_append_printf(str
, "\n%s", name
);
879 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
,
882 Error
*local_err
= NULL
;
883 const ChardevClass
*cc
;
886 ChardevBackend
*backend
= NULL
;
887 const char *name
= qemu_opt_get(opts
, "backend");
888 const char *id
= qemu_opts_id(opts
);
892 error_setg(errp
, "chardev: \"%s\" missing backend",
897 if (is_help_option(name
)) {
898 GString
*str
= g_string_new("");
900 chardev_name_foreach(help_string_append
, str
);
902 error_report("Available chardev backend types: %s", str
->str
);
903 g_string_free(str
, true);
908 error_setg(errp
, "chardev: no id specified");
912 for (i
= 0; i
< ARRAY_SIZE(chardev_alias_table
); i
++) {
913 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
914 name
= chardev_alias_table
[i
].typename
;
919 cc
= char_get_class(name
, errp
);
924 backend
= g_new0(ChardevBackend
, 1);
925 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
927 if (qemu_opt_get_bool(opts
, "mux", 0)) {
928 bid
= g_strdup_printf("%s-base", id
);
933 cc
->parse(opts
, backend
, &local_err
);
935 error_propagate(errp
, local_err
);
939 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
940 qemu_chr_parse_common(opts
, ccom
);
941 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
944 chr
= qemu_chardev_add(bid
? bid
: id
,
945 object_class_get_name(OBJECT_CLASS(cc
)),
953 qapi_free_ChardevBackend(backend
);
954 backend
= g_new0(ChardevBackend
, 1);
955 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
956 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
957 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
958 mux
= qemu_chardev_add(id
, TYPE_CHARDEV_MUX
, backend
, errp
);
960 qemu_chr_delete(chr
);
968 qapi_free_ChardevBackend(backend
);
973 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
)
980 if (strstart(filename
, "chardev:", &p
)) {
981 return qemu_chr_find(p
);
984 opts
= qemu_chr_parse_compat(label
, filename
);
988 chr
= qemu_chr_new_from_opts(opts
, &err
);
990 error_report_err(err
);
992 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
993 monitor_init(chr
, MONITOR_USE_READLINE
);
999 Chardev
*qemu_chr_new(const char *label
, const char *filename
)
1002 chr
= qemu_chr_new_noreplay(label
, filename
);
1004 if (replay_mode
!= REPLAY_MODE_NONE
) {
1005 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
1007 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
1008 error_report("Replay: ioctl is not supported "
1009 "for serial devices yet");
1011 replay_register_char_driver(chr
);
1016 void qemu_chr_fe_set_echo(CharBackend
*be
, bool echo
)
1018 Chardev
*chr
= be
->chr
;
1020 if (chr
&& CHARDEV_GET_CLASS(chr
)->chr_set_echo
) {
1021 CHARDEV_GET_CLASS(chr
)->chr_set_echo(chr
, echo
);
1025 void qemu_chr_fe_set_open(CharBackend
*be
, int fe_open
)
1027 Chardev
*chr
= be
->chr
;
1033 if (be
->fe_open
== fe_open
) {
1036 be
->fe_open
= fe_open
;
1037 if (CHARDEV_GET_CLASS(chr
)->chr_set_fe_open
) {
1038 CHARDEV_GET_CLASS(chr
)->chr_set_fe_open(chr
, fe_open
);
1042 guint
qemu_chr_fe_add_watch(CharBackend
*be
, GIOCondition cond
,
1043 GIOFunc func
, void *user_data
)
1045 Chardev
*s
= be
->chr
;
1049 if (!s
|| CHARDEV_GET_CLASS(s
)->chr_add_watch
== NULL
) {
1053 src
= CHARDEV_GET_CLASS(s
)->chr_add_watch(s
, cond
);
1058 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
1059 tag
= g_source_attach(src
, NULL
);
1060 g_source_unref(src
);
1065 void qemu_chr_fe_disconnect(CharBackend
*be
)
1067 Chardev
*chr
= be
->chr
;
1069 if (chr
&& CHARDEV_GET_CLASS(chr
)->chr_disconnect
) {
1070 CHARDEV_GET_CLASS(chr
)->chr_disconnect(chr
);
1074 void qemu_chr_delete(Chardev
*chr
)
1076 QTAILQ_REMOVE(&chardevs
, chr
, next
);
1077 object_unref(OBJECT(chr
));
1080 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
1082 ChardevInfoList
*chr_list
= NULL
;
1085 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
1086 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
1087 info
->value
= g_malloc0(sizeof(*info
->value
));
1088 info
->value
->label
= g_strdup(chr
->label
);
1089 info
->value
->filename
= g_strdup(chr
->filename
);
1090 info
->value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
1092 info
->next
= chr_list
;
1100 qmp_prepend_backend(const char *name
, void *opaque
)
1102 ChardevBackendInfoList
**list
= opaque
;
1103 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
1105 info
->value
= g_malloc0(sizeof(*info
->value
));
1106 info
->value
->name
= g_strdup(name
);
1111 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
1113 ChardevBackendInfoList
*backend_list
= NULL
;
1115 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
1117 return backend_list
;
1120 Chardev
*qemu_chr_find(const char *name
)
1124 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
1125 if (strcmp(chr
->label
, name
) != 0)
1132 QemuOptsList qemu_chardev_opts
= {
1134 .implied_opt_name
= "backend",
1135 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
1139 .type
= QEMU_OPT_STRING
,
1142 .type
= QEMU_OPT_STRING
,
1145 .type
= QEMU_OPT_STRING
,
1148 .type
= QEMU_OPT_STRING
,
1150 .name
= "localaddr",
1151 .type
= QEMU_OPT_STRING
,
1153 .name
= "localport",
1154 .type
= QEMU_OPT_STRING
,
1157 .type
= QEMU_OPT_NUMBER
,
1160 .type
= QEMU_OPT_BOOL
,
1163 .type
= QEMU_OPT_BOOL
,
1166 .type
= QEMU_OPT_BOOL
,
1169 .type
= QEMU_OPT_BOOL
,
1172 .type
= QEMU_OPT_BOOL
,
1174 .name
= "reconnect",
1175 .type
= QEMU_OPT_NUMBER
,
1178 .type
= QEMU_OPT_BOOL
,
1180 .name
= "tls-creds",
1181 .type
= QEMU_OPT_STRING
,
1184 .type
= QEMU_OPT_NUMBER
,
1187 .type
= QEMU_OPT_NUMBER
,
1190 .type
= QEMU_OPT_NUMBER
,
1193 .type
= QEMU_OPT_NUMBER
,
1196 .type
= QEMU_OPT_BOOL
,
1199 .type
= QEMU_OPT_BOOL
,
1202 .type
= QEMU_OPT_STRING
,
1205 .type
= QEMU_OPT_NUMBER
,
1208 .type
= QEMU_OPT_SIZE
,
1211 .type
= QEMU_OPT_STRING
,
1214 .type
= QEMU_OPT_BOOL
,
1217 .type
= QEMU_OPT_STRING
,
1219 .name
= "logappend",
1220 .type
= QEMU_OPT_BOOL
,
1222 { /* end of list */ }
1226 bool qemu_chr_has_feature(Chardev
*chr
,
1227 ChardevFeature feature
)
1229 return test_bit(feature
, chr
->features
);
1232 void qemu_chr_set_feature(Chardev
*chr
,
1233 ChardevFeature feature
)
1235 return set_bit(feature
, chr
->features
);
1238 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
1239 ChardevBackend
*backend
, Error
**errp
)
1241 Chardev
*chr
= NULL
;
1242 Error
*local_err
= NULL
;
1243 bool be_opened
= true;
1245 assert(g_str_has_prefix(typename
, "chardev-"));
1247 chr
= CHARDEV(object_new(typename
));
1248 chr
->label
= g_strdup(id
);
1250 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
1252 error_propagate(errp
, local_err
);
1253 object_unref(OBJECT(chr
));
1257 if (!chr
->filename
) {
1258 chr
->filename
= g_strdup(typename
+ 8);
1261 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1267 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
1270 const ChardevClass
*cc
;
1274 cc
= char_get_class(ChardevBackendKind_lookup
[backend
->type
], errp
);
1279 chr
= qemu_chardev_add(id
, object_class_get_name(OBJECT_CLASS(cc
)),
1285 ret
= g_new0(ChardevReturn
, 1);
1286 if (CHARDEV_IS_PTY(chr
)) {
1287 ret
->pty
= g_strdup(chr
->filename
+ 4);
1288 ret
->has_pty
= true;
1294 void qmp_chardev_remove(const char *id
, Error
**errp
)
1298 chr
= qemu_chr_find(id
);
1300 error_setg(errp
, "Chardev '%s' not found", id
);
1303 if (qemu_chr_is_busy(chr
)) {
1304 error_setg(errp
, "Chardev '%s' is busy", id
);
1307 if (qemu_chr_replay(chr
)) {
1309 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1312 qemu_chr_delete(chr
);
1315 void qemu_chr_cleanup(void)
1319 QTAILQ_FOREACH_SAFE(chr
, &chardevs
, next
, tmp
) {
1320 qemu_chr_delete(chr
);
1324 static void register_types(void)
1326 type_register_static(&char_type_info
);
1328 /* this must be done after machine init, since we register FEs with muxes
1329 * as part of realize functions like serial_isa_realizefn when -nographic
1332 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
1335 type_init(register_types
);