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 Object
*get_chardevs_root(void)
47 return container_get(object_get_root(), "/chardevs");
50 void qemu_chr_be_event(Chardev
*s
, int event
)
52 CharBackend
*be
= s
->be
;
54 /* Keep track if the char device is open */
56 case CHR_EVENT_OPENED
:
59 case CHR_EVENT_CLOSED
:
64 if (!be
|| !be
->chr_event
) {
68 be
->chr_event(be
->opaque
, event
);
71 /* Not reporting errors from writing to logfile, as logs are
72 * defined to be "best effort" only */
73 static void qemu_chr_fe_write_log(Chardev
*s
,
74 const uint8_t *buf
, size_t len
)
85 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
86 if (ret
== -1 && errno
== EAGAIN
) {
98 static int qemu_chr_fe_write_buffer(Chardev
*s
,
99 const uint8_t *buf
, int len
, int *offset
)
101 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
105 qemu_mutex_lock(&s
->chr_write_lock
);
106 while (*offset
< len
) {
108 res
= cc
->chr_write(s
, buf
+ *offset
, len
- *offset
);
109 if (res
< 0 && errno
== EAGAIN
) {
121 qemu_chr_fe_write_log(s
, buf
, *offset
);
123 qemu_mutex_unlock(&s
->chr_write_lock
);
128 static bool qemu_chr_replay(Chardev
*chr
)
130 return qemu_chr_has_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
133 int qemu_chr_fe_write(CharBackend
*be
, const uint8_t *buf
, int len
)
135 Chardev
*s
= be
->chr
;
143 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
145 replay_char_write_event_load(&ret
, &offset
);
146 assert(offset
<= len
);
147 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
151 cc
= CHARDEV_GET_CLASS(s
);
152 qemu_mutex_lock(&s
->chr_write_lock
);
153 ret
= cc
->chr_write(s
, buf
, len
);
156 qemu_chr_fe_write_log(s
, buf
, ret
);
159 qemu_mutex_unlock(&s
->chr_write_lock
);
161 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
162 replay_char_write_event_save(ret
, ret
< 0 ? 0 : ret
);
168 int qemu_chr_write_all(Chardev
*s
, const uint8_t *buf
, int len
)
173 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
174 replay_char_write_event_load(&res
, &offset
);
175 assert(offset
<= len
);
176 qemu_chr_fe_write_buffer(s
, buf
, offset
, &offset
);
180 res
= qemu_chr_fe_write_buffer(s
, buf
, len
, &offset
);
182 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
183 replay_char_write_event_save(res
, offset
);
192 int qemu_chr_fe_write_all(CharBackend
*be
, const uint8_t *buf
, int len
)
194 Chardev
*s
= be
->chr
;
200 return qemu_chr_write_all(s
, buf
, len
);
203 int qemu_chr_fe_read_all(CharBackend
*be
, uint8_t *buf
, int len
)
205 Chardev
*s
= be
->chr
;
206 int offset
= 0, counter
= 10;
209 if (!s
|| !CHARDEV_GET_CLASS(s
)->chr_sync_read
) {
213 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
214 return replay_char_read_all_load(buf
);
217 while (offset
< len
) {
219 res
= CHARDEV_GET_CLASS(s
)->chr_sync_read(s
, buf
+ offset
,
221 if (res
== -1 && errno
== EAGAIN
) {
231 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
232 replay_char_read_all_save_error(res
);
244 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
245 replay_char_read_all_save_buf(buf
, offset
);
250 int qemu_chr_fe_ioctl(CharBackend
*be
, int cmd
, void *arg
)
252 Chardev
*s
= be
->chr
;
255 if (!s
|| !CHARDEV_GET_CLASS(s
)->chr_ioctl
|| qemu_chr_replay(s
)) {
258 res
= CHARDEV_GET_CLASS(s
)->chr_ioctl(s
, cmd
, arg
);
264 int qemu_chr_be_can_write(Chardev
*s
)
266 CharBackend
*be
= s
->be
;
268 if (!be
|| !be
->chr_can_read
) {
272 return be
->chr_can_read(be
->opaque
);
275 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
)
277 CharBackend
*be
= s
->be
;
279 if (be
&& be
->chr_read
) {
280 be
->chr_read(be
->opaque
, buf
, len
);
284 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
)
286 if (qemu_chr_replay(s
)) {
287 if (replay_mode
== REPLAY_MODE_PLAY
) {
290 replay_chr_be_write(s
, buf
, len
);
292 qemu_chr_be_write_impl(s
, buf
, len
);
296 int qemu_chr_fe_get_msgfd(CharBackend
*be
)
298 Chardev
*s
= be
->chr
;
300 int res
= (qemu_chr_fe_get_msgfds(be
, &fd
, 1) == 1) ? fd
: -1;
301 if (s
&& qemu_chr_replay(s
)) {
302 error_report("Replay: get msgfd is not supported "
303 "for serial devices yet");
309 int qemu_chr_fe_get_msgfds(CharBackend
*be
, int *fds
, int len
)
311 Chardev
*s
= be
->chr
;
317 return CHARDEV_GET_CLASS(s
)->get_msgfds
?
318 CHARDEV_GET_CLASS(s
)->get_msgfds(s
, fds
, len
) : -1;
321 int qemu_chr_fe_set_msgfds(CharBackend
*be
, int *fds
, int num
)
323 Chardev
*s
= be
->chr
;
329 return CHARDEV_GET_CLASS(s
)->set_msgfds
?
330 CHARDEV_GET_CLASS(s
)->set_msgfds(s
, fds
, num
) : -1;
333 int qemu_chr_add_client(Chardev
*s
, int fd
)
335 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
336 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
339 void qemu_chr_fe_accept_input(CharBackend
*be
)
341 Chardev
*s
= be
->chr
;
347 if (CHARDEV_GET_CLASS(s
)->chr_accept_input
) {
348 CHARDEV_GET_CLASS(s
)->chr_accept_input(s
);
353 void qemu_chr_fe_printf(CharBackend
*be
, const char *fmt
, ...)
355 char buf
[CHR_READ_BUF_LEN
];
358 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
359 /* XXX this blocks entire thread. Rewrite to use
360 * qemu_chr_fe_write and background I/O callbacks */
361 qemu_chr_fe_write_all(be
, (uint8_t *)buf
, strlen(buf
));
365 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
366 bool *be_opened
, Error
**errp
)
368 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
369 /* Any ChardevCommon member would work */
370 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
372 if (common
&& common
->has_logfile
) {
373 int flags
= O_WRONLY
| O_CREAT
;
374 if (common
->has_logappend
&&
380 chr
->logfd
= qemu_open(common
->logfile
, flags
, 0666);
381 if (chr
->logfd
< 0) {
382 error_setg_errno(errp
, errno
,
383 "Unable to open logfile %s",
390 cc
->open(chr
, backend
, be_opened
, errp
);
394 static void char_init(Object
*obj
)
396 Chardev
*chr
= CHARDEV(obj
);
399 qemu_mutex_init(&chr
->chr_write_lock
);
402 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
407 static void char_class_init(ObjectClass
*oc
, void *data
)
409 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
411 cc
->chr_write
= null_chr_write
;
414 static void char_finalize(Object
*obj
)
416 Chardev
*chr
= CHARDEV(obj
);
421 g_free(chr
->filename
);
423 if (chr
->logfd
!= -1) {
426 qemu_mutex_destroy(&chr
->chr_write_lock
);
429 static const TypeInfo char_type_info
= {
430 .name
= TYPE_CHARDEV
,
431 .parent
= TYPE_OBJECT
,
432 .instance_size
= sizeof(Chardev
),
433 .instance_init
= char_init
,
434 .instance_finalize
= char_finalize
,
436 .class_size
= sizeof(ChardevClass
),
437 .class_init
= char_class_init
,
441 * Called after processing of default and command-line-specified
442 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
443 * to a mux chardev. This is done here to ensure that
444 * output/prompts/banners are only displayed for the FE that has
445 * focus when initial command-line processing/machine init is
448 * After this point, any new FE attached to any new or existing
449 * mux will receive CHR_EVENT_OPENED notifications for the BE
452 static int open_muxes(Object
*child
, void *opaque
)
454 if (CHARDEV_IS_MUX(child
)) {
455 /* send OPENED to all already-attached FEs */
456 mux_chr_send_all_event(CHARDEV(child
), CHR_EVENT_OPENED
);
457 /* mark mux as OPENED so any new FEs will immediately receive
460 qemu_chr_be_event(CHARDEV(child
), CHR_EVENT_OPENED
);
466 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
468 muxes_realized
= true;
469 object_child_foreach(get_chardevs_root(), open_muxes
, NULL
);
472 static Notifier muxes_realize_notify
= {
473 .notify
= muxes_realize_done
,
476 Chardev
*qemu_chr_fe_get_driver(CharBackend
*be
)
481 bool qemu_chr_fe_init(CharBackend
*b
, Chardev
*s
, Error
**errp
)
485 if (CHARDEV_IS_MUX(s
)) {
486 MuxChardev
*d
= MUX_CHARDEV(s
);
488 if (d
->mux_cnt
>= MAX_MUX
) {
492 d
->backends
[d
->mux_cnt
] = b
;
506 error_setg(errp
, QERR_DEVICE_IN_USE
, s
->label
);
510 static bool qemu_chr_is_busy(Chardev
*s
)
512 if (CHARDEV_IS_MUX(s
)) {
513 MuxChardev
*d
= MUX_CHARDEV(s
);
514 return d
->mux_cnt
>= 0;
516 return s
->be
!= NULL
;
520 void qemu_chr_fe_deinit(CharBackend
*b
)
525 qemu_chr_fe_set_handlers(b
, NULL
, NULL
, NULL
, NULL
, NULL
, true);
526 if (b
->chr
->be
== b
) {
529 if (CHARDEV_IS_MUX(b
->chr
)) {
530 MuxChardev
*d
= MUX_CHARDEV(b
->chr
);
531 d
->backends
[b
->tag
] = NULL
;
537 void qemu_chr_fe_set_handlers(CharBackend
*b
,
538 IOCanReadHandler
*fd_can_read
,
539 IOReadHandler
*fd_read
,
540 IOEventHandler
*fd_event
,
542 GMainContext
*context
,
554 cc
= CHARDEV_GET_CLASS(s
);
555 if (!opaque
&& !fd_can_read
&& !fd_read
&& !fd_event
) {
557 remove_fd_in_watch(s
);
561 b
->chr_can_read
= fd_can_read
;
562 b
->chr_read
= fd_read
;
563 b
->chr_event
= fd_event
;
565 if (cc
->chr_update_read_handler
) {
566 cc
->chr_update_read_handler(s
, context
);
570 qemu_chr_fe_set_open(b
, fe_open
);
574 qemu_chr_fe_take_focus(b
);
575 /* We're connecting to an already opened device, so let's make sure we
576 also get the open event */
578 qemu_chr_be_event(s
, CHR_EVENT_OPENED
);
582 if (CHARDEV_IS_MUX(s
)) {
583 mux_chr_set_handlers(s
, context
);
587 void qemu_chr_fe_take_focus(CharBackend
*b
)
593 if (CHARDEV_IS_MUX(b
->chr
)) {
594 mux_set_focus(b
->chr
, b
->tag
);
598 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
600 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
602 if (cc
->chr_wait_connected
) {
603 return cc
->chr_wait_connected(chr
, errp
);
609 int qemu_chr_fe_wait_connected(CharBackend
*be
, Error
**errp
)
612 error_setg(errp
, "missing associated backend");
616 return qemu_chr_wait_connected(be
->chr
, errp
);
619 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
621 char host
[65], port
[33], width
[8], height
[8];
625 Error
*local_err
= NULL
;
627 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
629 error_report_err(local_err
);
633 if (strstart(filename
, "mon:", &p
)) {
635 qemu_opt_set(opts
, "mux", "on", &error_abort
);
636 if (strcmp(filename
, "stdio") == 0) {
637 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
638 * but pass it to the guest. Handle this only for compat syntax,
639 * for -chardev syntax we have special option for this.
640 * This is what -nographic did, redirecting+muxing serial+monitor
641 * to stdio causing Ctrl+C to be passed to guest. */
642 qemu_opt_set(opts
, "signal", "off", &error_abort
);
646 if (strcmp(filename
, "null") == 0 ||
647 strcmp(filename
, "pty") == 0 ||
648 strcmp(filename
, "msmouse") == 0 ||
649 strcmp(filename
, "wctablet") == 0 ||
650 strcmp(filename
, "braille") == 0 ||
651 strcmp(filename
, "testdev") == 0 ||
652 strcmp(filename
, "stdio") == 0) {
653 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
656 if (strstart(filename
, "vc", &p
)) {
657 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
659 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
661 qemu_opt_set(opts
, "width", width
, &error_abort
);
662 qemu_opt_set(opts
, "height", height
, &error_abort
);
663 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
665 qemu_opt_set(opts
, "cols", width
, &error_abort
);
666 qemu_opt_set(opts
, "rows", height
, &error_abort
);
673 if (strcmp(filename
, "con:") == 0) {
674 qemu_opt_set(opts
, "backend", "console", &error_abort
);
677 if (strstart(filename
, "COM", NULL
)) {
678 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
679 qemu_opt_set(opts
, "path", filename
, &error_abort
);
682 if (strstart(filename
, "file:", &p
)) {
683 qemu_opt_set(opts
, "backend", "file", &error_abort
);
684 qemu_opt_set(opts
, "path", p
, &error_abort
);
687 if (strstart(filename
, "pipe:", &p
)) {
688 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
689 qemu_opt_set(opts
, "path", p
, &error_abort
);
692 if (strstart(filename
, "tcp:", &p
) ||
693 strstart(filename
, "telnet:", &p
) ||
694 strstart(filename
, "tn3270:", &p
)) {
695 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
697 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
700 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
701 qemu_opt_set(opts
, "host", host
, &error_abort
);
702 qemu_opt_set(opts
, "port", port
, &error_abort
);
704 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
706 error_report_err(local_err
);
710 if (strstart(filename
, "telnet:", &p
)) {
711 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
712 } else if (strstart(filename
, "tn3270:", &p
)) {
713 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
717 if (strstart(filename
, "udp:", &p
)) {
718 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
719 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
721 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
725 qemu_opt_set(opts
, "host", host
, &error_abort
);
726 qemu_opt_set(opts
, "port", port
, &error_abort
);
729 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
731 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
735 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
736 qemu_opt_set(opts
, "localport", port
, &error_abort
);
740 if (strstart(filename
, "unix:", &p
)) {
741 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
742 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
744 error_report_err(local_err
);
749 if (strstart(filename
, "/dev/parport", NULL
) ||
750 strstart(filename
, "/dev/ppi", NULL
)) {
751 qemu_opt_set(opts
, "backend", "parport", &error_abort
);
752 qemu_opt_set(opts
, "path", filename
, &error_abort
);
755 if (strstart(filename
, "/dev/", NULL
)) {
756 qemu_opt_set(opts
, "backend", "tty", &error_abort
);
757 qemu_opt_set(opts
, "path", filename
, &error_abort
);
766 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
768 const char *logfile
= qemu_opt_get(opts
, "logfile");
770 backend
->has_logfile
= logfile
!= NULL
;
771 backend
->logfile
= g_strdup(logfile
);
773 backend
->has_logappend
= true;
774 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
777 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
780 const ChardevClass
*cc
;
781 char *typename
= g_strdup_printf("chardev-%s", driver
);
783 oc
= object_class_by_name(typename
);
786 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
787 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
791 if (object_class_is_abstract(oc
)) {
792 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
793 "abstract device type");
797 cc
= CHARDEV_CLASS(oc
);
799 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
806 static const struct ChardevAlias
{
807 const char *typename
;
809 } chardev_alias_table
[] = {
810 #ifdef HAVE_CHARDEV_PARPORT
811 { "parallel", "parport" },
813 #ifdef HAVE_CHARDEV_SERIAL
818 typedef struct ChadevClassFE
{
819 void (*fn
)(const char *name
, void *opaque
);
824 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
826 ChadevClassFE
*fe
= opaque
;
828 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
829 if (CHARDEV_CLASS(klass
)->internal
) {
833 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
837 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
), void *opaque
)
839 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
842 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
844 for (i
= 0; i
< ARRAY_SIZE(chardev_alias_table
); i
++) {
845 fn(chardev_alias_table
[i
].alias
, opaque
);
850 help_string_append(const char *name
, void *opaque
)
852 GString
*str
= opaque
;
854 g_string_append_printf(str
, "\n%s", name
);
857 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
,
860 Error
*local_err
= NULL
;
861 const ChardevClass
*cc
;
864 ChardevBackend
*backend
= NULL
;
865 const char *name
= qemu_opt_get(opts
, "backend");
866 const char *id
= qemu_opts_id(opts
);
870 error_setg(errp
, "chardev: \"%s\" missing backend",
875 if (is_help_option(name
)) {
876 GString
*str
= g_string_new("");
878 chardev_name_foreach(help_string_append
, str
);
880 error_report("Available chardev backend types: %s", str
->str
);
881 g_string_free(str
, true);
886 error_setg(errp
, "chardev: no id specified");
890 for (i
= 0; i
< ARRAY_SIZE(chardev_alias_table
); i
++) {
891 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
892 name
= chardev_alias_table
[i
].typename
;
897 cc
= char_get_class(name
, errp
);
902 backend
= g_new0(ChardevBackend
, 1);
903 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
905 if (qemu_opt_get_bool(opts
, "mux", 0)) {
906 bid
= g_strdup_printf("%s-base", id
);
911 cc
->parse(opts
, backend
, &local_err
);
913 error_propagate(errp
, local_err
);
917 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
918 qemu_chr_parse_common(opts
, ccom
);
919 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
922 chr
= qemu_chardev_new(bid
? bid
: id
,
923 object_class_get_name(OBJECT_CLASS(cc
)),
932 qapi_free_ChardevBackend(backend
);
933 backend
= g_new0(ChardevBackend
, 1);
934 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
935 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
936 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
937 mux
= qemu_chardev_new(id
, TYPE_CHARDEV_MUX
, backend
, errp
);
939 object_unparent(OBJECT(chr
));
947 qapi_free_ChardevBackend(backend
);
952 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
)
959 if (strstart(filename
, "chardev:", &p
)) {
960 return qemu_chr_find(p
);
963 opts
= qemu_chr_parse_compat(label
, filename
);
967 chr
= qemu_chr_new_from_opts(opts
, &err
);
969 error_report_err(err
);
971 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
972 monitor_init(chr
, MONITOR_USE_READLINE
);
978 Chardev
*qemu_chr_new(const char *label
, const char *filename
)
981 chr
= qemu_chr_new_noreplay(label
, filename
);
983 if (replay_mode
!= REPLAY_MODE_NONE
) {
984 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
986 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
987 error_report("Replay: ioctl is not supported "
988 "for serial devices yet");
990 replay_register_char_driver(chr
);
995 void qemu_chr_fe_set_echo(CharBackend
*be
, bool echo
)
997 Chardev
*chr
= be
->chr
;
999 if (chr
&& CHARDEV_GET_CLASS(chr
)->chr_set_echo
) {
1000 CHARDEV_GET_CLASS(chr
)->chr_set_echo(chr
, echo
);
1004 void qemu_chr_fe_set_open(CharBackend
*be
, int fe_open
)
1006 Chardev
*chr
= be
->chr
;
1012 if (be
->fe_open
== fe_open
) {
1015 be
->fe_open
= fe_open
;
1016 if (CHARDEV_GET_CLASS(chr
)->chr_set_fe_open
) {
1017 CHARDEV_GET_CLASS(chr
)->chr_set_fe_open(chr
, fe_open
);
1021 guint
qemu_chr_fe_add_watch(CharBackend
*be
, GIOCondition cond
,
1022 GIOFunc func
, void *user_data
)
1024 Chardev
*s
= be
->chr
;
1028 if (!s
|| CHARDEV_GET_CLASS(s
)->chr_add_watch
== NULL
) {
1032 src
= CHARDEV_GET_CLASS(s
)->chr_add_watch(s
, cond
);
1037 g_source_set_callback(src
, (GSourceFunc
)func
, user_data
, NULL
);
1038 tag
= g_source_attach(src
, NULL
);
1039 g_source_unref(src
);
1044 void qemu_chr_fe_disconnect(CharBackend
*be
)
1046 Chardev
*chr
= be
->chr
;
1048 if (chr
&& CHARDEV_GET_CLASS(chr
)->chr_disconnect
) {
1049 CHARDEV_GET_CLASS(chr
)->chr_disconnect(chr
);
1053 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
1055 Chardev
*chr
= CHARDEV(obj
);
1056 ChardevInfoList
**list
= data
;
1057 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
1059 info
->value
= g_malloc0(sizeof(*info
->value
));
1060 info
->value
->label
= g_strdup(chr
->label
);
1061 info
->value
->filename
= g_strdup(chr
->filename
);
1062 info
->value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
1070 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
1072 ChardevInfoList
*chr_list
= NULL
;
1074 object_child_foreach(get_chardevs_root(),
1075 qmp_query_chardev_foreach
, &chr_list
);
1081 qmp_prepend_backend(const char *name
, void *opaque
)
1083 ChardevBackendInfoList
**list
= opaque
;
1084 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
1086 info
->value
= g_malloc0(sizeof(*info
->value
));
1087 info
->value
->name
= g_strdup(name
);
1092 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
1094 ChardevBackendInfoList
*backend_list
= NULL
;
1096 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
1098 return backend_list
;
1101 Chardev
*qemu_chr_find(const char *name
)
1103 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
1105 return obj
? CHARDEV(obj
) : NULL
;
1108 QemuOptsList qemu_chardev_opts
= {
1110 .implied_opt_name
= "backend",
1111 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
1115 .type
= QEMU_OPT_STRING
,
1118 .type
= QEMU_OPT_STRING
,
1121 .type
= QEMU_OPT_STRING
,
1124 .type
= QEMU_OPT_STRING
,
1126 .name
= "localaddr",
1127 .type
= QEMU_OPT_STRING
,
1129 .name
= "localport",
1130 .type
= QEMU_OPT_STRING
,
1133 .type
= QEMU_OPT_NUMBER
,
1136 .type
= QEMU_OPT_BOOL
,
1139 .type
= QEMU_OPT_BOOL
,
1142 .type
= QEMU_OPT_BOOL
,
1145 .type
= QEMU_OPT_BOOL
,
1148 .type
= QEMU_OPT_BOOL
,
1150 .name
= "reconnect",
1151 .type
= QEMU_OPT_NUMBER
,
1154 .type
= QEMU_OPT_BOOL
,
1157 .type
= QEMU_OPT_BOOL
,
1159 .name
= "tls-creds",
1160 .type
= QEMU_OPT_STRING
,
1163 .type
= QEMU_OPT_NUMBER
,
1166 .type
= QEMU_OPT_NUMBER
,
1169 .type
= QEMU_OPT_NUMBER
,
1172 .type
= QEMU_OPT_NUMBER
,
1175 .type
= QEMU_OPT_BOOL
,
1178 .type
= QEMU_OPT_BOOL
,
1181 .type
= QEMU_OPT_STRING
,
1184 .type
= QEMU_OPT_NUMBER
,
1187 .type
= QEMU_OPT_SIZE
,
1190 .type
= QEMU_OPT_STRING
,
1193 .type
= QEMU_OPT_BOOL
,
1196 .type
= QEMU_OPT_STRING
,
1198 .name
= "logappend",
1199 .type
= QEMU_OPT_BOOL
,
1201 { /* end of list */ }
1205 bool qemu_chr_has_feature(Chardev
*chr
,
1206 ChardevFeature feature
)
1208 return test_bit(feature
, chr
->features
);
1211 void qemu_chr_set_feature(Chardev
*chr
,
1212 ChardevFeature feature
)
1214 return set_bit(feature
, chr
->features
);
1217 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
1218 ChardevBackend
*backend
,
1222 Chardev
*chr
= NULL
;
1223 Error
*local_err
= NULL
;
1224 bool be_opened
= true;
1226 assert(g_str_has_prefix(typename
, "chardev-"));
1228 obj
= object_new(typename
);
1230 chr
->label
= g_strdup(id
);
1232 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
1237 if (!chr
->filename
) {
1238 chr
->filename
= g_strdup(typename
+ 8);
1241 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1245 object_property_add_child(get_chardevs_root(), id
, obj
, &local_err
);
1254 error_propagate(errp
, local_err
);
1262 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
1265 const ChardevClass
*cc
;
1269 cc
= char_get_class(ChardevBackendKind_lookup
[backend
->type
], errp
);
1274 chr
= qemu_chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
1280 ret
= g_new0(ChardevReturn
, 1);
1281 if (CHARDEV_IS_PTY(chr
)) {
1282 ret
->pty
= g_strdup(chr
->filename
+ 4);
1283 ret
->has_pty
= true;
1289 void qmp_chardev_remove(const char *id
, Error
**errp
)
1293 chr
= qemu_chr_find(id
);
1295 error_setg(errp
, "Chardev '%s' not found", id
);
1298 if (qemu_chr_is_busy(chr
)) {
1299 error_setg(errp
, "Chardev '%s' is busy", id
);
1302 if (qemu_chr_replay(chr
)) {
1304 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1307 object_unparent(OBJECT(chr
));
1310 void qemu_chr_cleanup(void)
1312 object_unparent(get_chardevs_root());
1315 static void register_types(void)
1317 type_register_static(&char_type_info
);
1319 /* this must be done after machine init, since we register FEs with muxes
1320 * as part of realize functions like serial_isa_realizefn when -nographic
1323 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
1326 type_init(register_types
);