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/cutils.h"
26 #include "monitor/monitor.h"
27 #include "sysemu/sysemu.h"
28 #include "qemu/config-file.h"
29 #include "qemu/error-report.h"
30 #include "chardev/char.h"
31 #include "qmp-commands.h"
32 #include "qapi-visit.h"
33 #include "sysemu/replay.h"
34 #include "qemu/help_option.h"
36 #include "chardev/char-mux.h"
38 /***********************************************************/
39 /* character device */
41 static Object
*get_chardevs_root(void)
43 return container_get(object_get_root(), "/chardevs");
46 void qemu_chr_be_event(Chardev
*s
, int event
)
48 CharBackend
*be
= s
->be
;
50 /* Keep track if the char device is open */
52 case CHR_EVENT_OPENED
:
55 case CHR_EVENT_CLOSED
:
60 if (!be
|| !be
->chr_event
) {
64 be
->chr_event(be
->opaque
, event
);
67 /* Not reporting errors from writing to logfile, as logs are
68 * defined to be "best effort" only */
69 static void qemu_chr_write_log(Chardev
*s
, const uint8_t *buf
, size_t len
)
80 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
81 if (ret
== -1 && errno
== EAGAIN
) {
93 static int qemu_chr_write_buffer(Chardev
*s
,
94 const uint8_t *buf
, int len
,
95 int *offset
, bool write_all
)
97 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
101 qemu_mutex_lock(&s
->chr_write_lock
);
102 while (*offset
< len
) {
104 res
= cc
->chr_write(s
, buf
+ *offset
, len
- *offset
);
105 if (res
< 0 && errno
== EAGAIN
&& write_all
) {
120 qemu_chr_write_log(s
, buf
, *offset
);
122 qemu_mutex_unlock(&s
->chr_write_lock
);
127 int qemu_chr_write(Chardev
*s
, const uint8_t *buf
, int len
, bool write_all
)
132 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
133 replay_char_write_event_load(&res
, &offset
);
134 assert(offset
<= len
);
135 qemu_chr_write_buffer(s
, buf
, offset
, &offset
, true);
139 res
= qemu_chr_write_buffer(s
, buf
, len
, &offset
, write_all
);
141 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
142 replay_char_write_event_save(res
, offset
);
151 int qemu_chr_be_can_write(Chardev
*s
)
153 CharBackend
*be
= s
->be
;
155 if (!be
|| !be
->chr_can_read
) {
159 return be
->chr_can_read(be
->opaque
);
162 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
)
164 CharBackend
*be
= s
->be
;
166 if (be
&& be
->chr_read
) {
167 be
->chr_read(be
->opaque
, buf
, len
);
171 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
)
173 if (qemu_chr_replay(s
)) {
174 if (replay_mode
== REPLAY_MODE_PLAY
) {
177 replay_chr_be_write(s
, buf
, len
);
179 qemu_chr_be_write_impl(s
, buf
, len
);
183 void qemu_chr_be_update_read_handlers(Chardev
*s
,
184 GMainContext
*context
)
186 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
188 s
->gcontext
= context
;
189 if (cc
->chr_update_read_handler
) {
190 cc
->chr_update_read_handler(s
);
194 int qemu_chr_add_client(Chardev
*s
, int fd
)
196 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
197 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
200 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
201 bool *be_opened
, Error
**errp
)
203 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
204 /* Any ChardevCommon member would work */
205 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
207 if (common
&& common
->has_logfile
) {
208 int flags
= O_WRONLY
| O_CREAT
;
209 if (common
->has_logappend
&&
215 chr
->logfd
= qemu_open(common
->logfile
, flags
, 0666);
216 if (chr
->logfd
< 0) {
217 error_setg_errno(errp
, errno
,
218 "Unable to open logfile %s",
225 cc
->open(chr
, backend
, be_opened
, errp
);
229 static void char_init(Object
*obj
)
231 Chardev
*chr
= CHARDEV(obj
);
234 qemu_mutex_init(&chr
->chr_write_lock
);
237 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
242 static void char_class_init(ObjectClass
*oc
, void *data
)
244 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
246 cc
->chr_write
= null_chr_write
;
249 static void char_finalize(Object
*obj
)
251 Chardev
*chr
= CHARDEV(obj
);
256 g_free(chr
->filename
);
258 if (chr
->logfd
!= -1) {
261 qemu_mutex_destroy(&chr
->chr_write_lock
);
264 static const TypeInfo char_type_info
= {
265 .name
= TYPE_CHARDEV
,
266 .parent
= TYPE_OBJECT
,
267 .instance_size
= sizeof(Chardev
),
268 .instance_init
= char_init
,
269 .instance_finalize
= char_finalize
,
271 .class_size
= sizeof(ChardevClass
),
272 .class_init
= char_class_init
,
276 * Called after processing of default and command-line-specified
277 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
278 * to a mux chardev. This is done here to ensure that
279 * output/prompts/banners are only displayed for the FE that has
280 * focus when initial command-line processing/machine init is
283 * After this point, any new FE attached to any new or existing
284 * mux will receive CHR_EVENT_OPENED notifications for the BE
287 static int open_muxes(Object
*child
, void *opaque
)
289 if (CHARDEV_IS_MUX(child
)) {
290 /* send OPENED to all already-attached FEs */
291 mux_chr_send_all_event(CHARDEV(child
), CHR_EVENT_OPENED
);
292 /* mark mux as OPENED so any new FEs will immediately receive
295 qemu_chr_be_event(CHARDEV(child
), CHR_EVENT_OPENED
);
301 static void muxes_realize_done(Notifier
*notifier
, void *unused
)
303 muxes_realized
= true;
304 object_child_foreach(get_chardevs_root(), open_muxes
, NULL
);
307 static Notifier muxes_realize_notify
= {
308 .notify
= muxes_realize_done
,
311 static bool qemu_chr_is_busy(Chardev
*s
)
313 if (CHARDEV_IS_MUX(s
)) {
314 MuxChardev
*d
= MUX_CHARDEV(s
);
315 return d
->mux_cnt
>= 0;
317 return s
->be
!= NULL
;
321 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
323 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
325 if (cc
->chr_wait_connected
) {
326 return cc
->chr_wait_connected(chr
, errp
);
332 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
334 char host
[65], port
[33], width
[8], height
[8];
338 Error
*local_err
= NULL
;
340 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
342 error_report_err(local_err
);
346 if (strstart(filename
, "mon:", &p
)) {
348 qemu_opt_set(opts
, "mux", "on", &error_abort
);
349 if (strcmp(filename
, "stdio") == 0) {
350 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
351 * but pass it to the guest. Handle this only for compat syntax,
352 * for -chardev syntax we have special option for this.
353 * This is what -nographic did, redirecting+muxing serial+monitor
354 * to stdio causing Ctrl+C to be passed to guest. */
355 qemu_opt_set(opts
, "signal", "off", &error_abort
);
359 if (strcmp(filename
, "null") == 0 ||
360 strcmp(filename
, "pty") == 0 ||
361 strcmp(filename
, "msmouse") == 0 ||
362 strcmp(filename
, "wctablet") == 0 ||
363 strcmp(filename
, "braille") == 0 ||
364 strcmp(filename
, "testdev") == 0 ||
365 strcmp(filename
, "stdio") == 0) {
366 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
369 if (strstart(filename
, "vc", &p
)) {
370 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
372 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
374 qemu_opt_set(opts
, "width", width
, &error_abort
);
375 qemu_opt_set(opts
, "height", height
, &error_abort
);
376 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
378 qemu_opt_set(opts
, "cols", width
, &error_abort
);
379 qemu_opt_set(opts
, "rows", height
, &error_abort
);
386 if (strcmp(filename
, "con:") == 0) {
387 qemu_opt_set(opts
, "backend", "console", &error_abort
);
390 if (strstart(filename
, "COM", NULL
)) {
391 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
392 qemu_opt_set(opts
, "path", filename
, &error_abort
);
395 if (strstart(filename
, "file:", &p
)) {
396 qemu_opt_set(opts
, "backend", "file", &error_abort
);
397 qemu_opt_set(opts
, "path", p
, &error_abort
);
400 if (strstart(filename
, "pipe:", &p
)) {
401 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
402 qemu_opt_set(opts
, "path", p
, &error_abort
);
405 if (strstart(filename
, "tcp:", &p
) ||
406 strstart(filename
, "telnet:", &p
) ||
407 strstart(filename
, "tn3270:", &p
)) {
408 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
410 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
413 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
414 qemu_opt_set(opts
, "host", host
, &error_abort
);
415 qemu_opt_set(opts
, "port", port
, &error_abort
);
417 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
419 error_report_err(local_err
);
423 if (strstart(filename
, "telnet:", &p
)) {
424 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
425 } else if (strstart(filename
, "tn3270:", &p
)) {
426 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
430 if (strstart(filename
, "udp:", &p
)) {
431 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
432 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
434 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
438 qemu_opt_set(opts
, "host", host
, &error_abort
);
439 qemu_opt_set(opts
, "port", port
, &error_abort
);
442 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
444 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
448 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
449 qemu_opt_set(opts
, "localport", port
, &error_abort
);
453 if (strstart(filename
, "unix:", &p
)) {
454 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
455 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
457 error_report_err(local_err
);
462 if (strstart(filename
, "/dev/parport", NULL
) ||
463 strstart(filename
, "/dev/ppi", NULL
)) {
464 qemu_opt_set(opts
, "backend", "parallel", &error_abort
);
465 qemu_opt_set(opts
, "path", filename
, &error_abort
);
468 if (strstart(filename
, "/dev/", NULL
)) {
469 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
470 qemu_opt_set(opts
, "path", filename
, &error_abort
);
479 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
481 const char *logfile
= qemu_opt_get(opts
, "logfile");
483 backend
->has_logfile
= logfile
!= NULL
;
484 backend
->logfile
= g_strdup(logfile
);
486 backend
->has_logappend
= true;
487 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
490 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
493 const ChardevClass
*cc
;
494 char *typename
= g_strdup_printf("chardev-%s", driver
);
496 oc
= object_class_by_name(typename
);
499 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
500 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
504 if (object_class_is_abstract(oc
)) {
505 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
506 "abstract device type");
510 cc
= CHARDEV_CLASS(oc
);
512 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
519 static const struct ChardevAlias
{
520 const char *typename
;
522 } chardev_alias_table
[] = {
523 #ifdef HAVE_CHARDEV_PARPORT
524 { "parallel", "parport" },
526 #ifdef HAVE_CHARDEV_SERIAL
531 typedef struct ChadevClassFE
{
532 void (*fn
)(const char *name
, void *opaque
);
537 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
539 ChadevClassFE
*fe
= opaque
;
541 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
542 if (CHARDEV_CLASS(klass
)->internal
) {
546 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
550 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
), void *opaque
)
552 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
555 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
557 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
558 fn(chardev_alias_table
[i
].alias
, opaque
);
563 help_string_append(const char *name
, void *opaque
)
565 GString
*str
= opaque
;
567 g_string_append_printf(str
, "\n%s", name
);
570 static const char *chardev_alias_translate(const char *name
)
573 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
574 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
575 return chardev_alias_table
[i
].typename
;
581 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
, Error
**errp
)
583 Error
*local_err
= NULL
;
584 const ChardevClass
*cc
;
585 ChardevBackend
*backend
= NULL
;
586 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
589 error_setg(errp
, "chardev: \"%s\" missing backend",
594 cc
= char_get_class(name
, errp
);
599 backend
= g_new0(ChardevBackend
, 1);
600 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
603 cc
->parse(opts
, backend
, &local_err
);
605 error_propagate(errp
, local_err
);
606 qapi_free_ChardevBackend(backend
);
610 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
611 qemu_chr_parse_common(opts
, ccom
);
612 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
618 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
, Error
**errp
)
620 const ChardevClass
*cc
;
622 ChardevBackend
*backend
= NULL
;
623 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
624 const char *id
= qemu_opts_id(opts
);
627 if (name
&& is_help_option(name
)) {
628 GString
*str
= g_string_new("");
630 chardev_name_foreach(help_string_append
, str
);
632 error_report("Available chardev backend types: %s", str
->str
);
633 g_string_free(str
, true);
638 error_setg(errp
, "chardev: no id specified");
642 backend
= qemu_chr_parse_opts(opts
, errp
);
643 if (backend
== NULL
) {
647 cc
= char_get_class(name
, errp
);
652 if (qemu_opt_get_bool(opts
, "mux", 0)) {
653 bid
= g_strdup_printf("%s-base", id
);
656 chr
= qemu_chardev_new(bid
? bid
: id
,
657 object_class_get_name(OBJECT_CLASS(cc
)),
666 qapi_free_ChardevBackend(backend
);
667 backend
= g_new0(ChardevBackend
, 1);
668 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
669 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
670 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
671 mux
= qemu_chardev_new(id
, TYPE_CHARDEV_MUX
, backend
, errp
);
673 object_unparent(OBJECT(chr
));
681 qapi_free_ChardevBackend(backend
);
686 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
)
693 if (strstart(filename
, "chardev:", &p
)) {
694 return qemu_chr_find(p
);
697 opts
= qemu_chr_parse_compat(label
, filename
);
701 chr
= qemu_chr_new_from_opts(opts
, &err
);
703 error_report_err(err
);
705 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
706 monitor_init(chr
, MONITOR_USE_READLINE
);
712 Chardev
*qemu_chr_new(const char *label
, const char *filename
)
715 chr
= qemu_chr_new_noreplay(label
, filename
);
717 if (replay_mode
!= REPLAY_MODE_NONE
) {
718 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
720 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
721 error_report("Replay: ioctl is not supported "
722 "for serial devices yet");
724 replay_register_char_driver(chr
);
729 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
731 Chardev
*chr
= CHARDEV(obj
);
732 ChardevInfoList
**list
= data
;
733 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
735 info
->value
= g_malloc0(sizeof(*info
->value
));
736 info
->value
->label
= g_strdup(chr
->label
);
737 info
->value
->filename
= g_strdup(chr
->filename
);
738 info
->value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
746 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
748 ChardevInfoList
*chr_list
= NULL
;
750 object_child_foreach(get_chardevs_root(),
751 qmp_query_chardev_foreach
, &chr_list
);
757 qmp_prepend_backend(const char *name
, void *opaque
)
759 ChardevBackendInfoList
**list
= opaque
;
760 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
762 info
->value
= g_malloc0(sizeof(*info
->value
));
763 info
->value
->name
= g_strdup(name
);
768 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
770 ChardevBackendInfoList
*backend_list
= NULL
;
772 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
777 Chardev
*qemu_chr_find(const char *name
)
779 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
781 return obj
? CHARDEV(obj
) : NULL
;
784 QemuOptsList qemu_chardev_opts
= {
786 .implied_opt_name
= "backend",
787 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
791 .type
= QEMU_OPT_STRING
,
794 .type
= QEMU_OPT_STRING
,
797 .type
= QEMU_OPT_STRING
,
800 .type
= QEMU_OPT_STRING
,
803 .type
= QEMU_OPT_STRING
,
806 .type
= QEMU_OPT_STRING
,
809 .type
= QEMU_OPT_NUMBER
,
812 .type
= QEMU_OPT_BOOL
,
815 .type
= QEMU_OPT_BOOL
,
818 .type
= QEMU_OPT_BOOL
,
821 .type
= QEMU_OPT_BOOL
,
824 .type
= QEMU_OPT_BOOL
,
827 .type
= QEMU_OPT_NUMBER
,
830 .type
= QEMU_OPT_BOOL
,
833 .type
= QEMU_OPT_BOOL
,
836 .type
= QEMU_OPT_STRING
,
839 .type
= QEMU_OPT_NUMBER
,
842 .type
= QEMU_OPT_NUMBER
,
845 .type
= QEMU_OPT_NUMBER
,
848 .type
= QEMU_OPT_NUMBER
,
851 .type
= QEMU_OPT_BOOL
,
854 .type
= QEMU_OPT_BOOL
,
857 .type
= QEMU_OPT_STRING
,
860 .type
= QEMU_OPT_NUMBER
,
863 .type
= QEMU_OPT_SIZE
,
866 .type
= QEMU_OPT_STRING
,
869 .type
= QEMU_OPT_BOOL
,
872 .type
= QEMU_OPT_STRING
,
875 .type
= QEMU_OPT_BOOL
,
877 { /* end of list */ }
881 bool qemu_chr_has_feature(Chardev
*chr
,
882 ChardevFeature feature
)
884 return test_bit(feature
, chr
->features
);
887 void qemu_chr_set_feature(Chardev
*chr
,
888 ChardevFeature feature
)
890 return set_bit(feature
, chr
->features
);
893 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
894 ChardevBackend
*backend
,
899 Error
*local_err
= NULL
;
900 bool be_opened
= true;
902 assert(g_str_has_prefix(typename
, "chardev-"));
904 obj
= object_new(typename
);
906 chr
->label
= g_strdup(id
);
908 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
913 if (!chr
->filename
) {
914 chr
->filename
= g_strdup(typename
+ 8);
917 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
921 object_property_add_child(get_chardevs_root(), id
, obj
, &local_err
);
930 error_propagate(errp
, local_err
);
938 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
941 const ChardevClass
*cc
;
945 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
950 chr
= qemu_chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
956 ret
= g_new0(ChardevReturn
, 1);
957 if (CHARDEV_IS_PTY(chr
)) {
958 ret
->pty
= g_strdup(chr
->filename
+ 4);
965 ChardevReturn
*qmp_chardev_change(const char *id
, ChardevBackend
*backend
,
969 const ChardevClass
*cc
;
970 Chardev
*chr
, *chr_new
;
971 bool closed_sent
= false;
974 chr
= qemu_chr_find(id
);
976 error_setg(errp
, "Chardev '%s' does not exist", id
);
980 if (CHARDEV_IS_MUX(chr
)) {
981 error_setg(errp
, "Mux device hotswap not supported yet");
985 if (qemu_chr_replay(chr
)) {
987 "Chardev '%s' cannot be changed in record/replay mode", id
);
994 object_unparent(OBJECT(chr
));
995 return qmp_chardev_add(id
, backend
, errp
);
998 if (!be
->chr_be_change
) {
999 error_setg(errp
, "Chardev user does not support chardev hotswap");
1003 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1008 chr_new
= qemu_chardev_new(NULL
, object_class_get_name(OBJECT_CLASS(cc
)),
1013 chr_new
->label
= g_strdup(id
);
1015 if (chr
->be_open
&& !chr_new
->be_open
) {
1016 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1021 qemu_chr_fe_init(be
, chr_new
, &error_abort
);
1023 if (be
->chr_be_change(be
->opaque
) < 0) {
1024 error_setg(errp
, "Chardev '%s' change failed", chr_new
->label
);
1026 qemu_chr_fe_init(be
, chr
, &error_abort
);
1028 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1030 object_unref(OBJECT(chr_new
));
1034 object_unparent(OBJECT(chr
));
1035 object_property_add_child(get_chardevs_root(), chr_new
->label
,
1036 OBJECT(chr_new
), &error_abort
);
1037 object_unref(OBJECT(chr_new
));
1039 ret
= g_new0(ChardevReturn
, 1);
1040 if (CHARDEV_IS_PTY(chr_new
)) {
1041 ret
->pty
= g_strdup(chr_new
->filename
+ 4);
1042 ret
->has_pty
= true;
1048 void qmp_chardev_remove(const char *id
, Error
**errp
)
1052 chr
= qemu_chr_find(id
);
1054 error_setg(errp
, "Chardev '%s' not found", id
);
1057 if (qemu_chr_is_busy(chr
)) {
1058 error_setg(errp
, "Chardev '%s' is busy", id
);
1061 if (qemu_chr_replay(chr
)) {
1063 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1066 object_unparent(OBJECT(chr
));
1069 void qmp_chardev_send_break(const char *id
, Error
**errp
)
1073 chr
= qemu_chr_find(id
);
1075 error_setg(errp
, "Chardev '%s' not found", id
);
1078 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
1081 void qemu_chr_cleanup(void)
1083 object_unparent(get_chardevs_root());
1086 static void register_types(void)
1088 type_register_static(&char_type_info
);
1090 /* this must be done after machine init, since we register FEs with muxes
1091 * as part of realize functions like serial_isa_realizefn when -nographic
1094 qemu_add_machine_init_done_notifier(&muxes_realize_notify
);
1097 type_init(register_types
);