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
25 #include "qemu/osdep.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 "chardev/char.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-char.h"
34 #include "qapi/qmp/qerror.h"
35 #include "sysemu/replay.h"
36 #include "qemu/help_option.h"
37 #include "qemu/option.h"
39 #include "chardev/char-mux.h"
41 /***********************************************************/
42 /* character device */
44 static Object
*get_chardevs_root(void)
46 return container_get(object_get_root(), "/chardevs");
49 static void chr_be_event(Chardev
*s
, int event
)
51 CharBackend
*be
= s
->be
;
53 if (!be
|| !be
->chr_event
) {
57 be
->chr_event(be
->opaque
, event
);
60 void qemu_chr_be_event(Chardev
*s
, int event
)
62 /* Keep track if the char device is open */
64 case CHR_EVENT_OPENED
:
67 case CHR_EVENT_CLOSED
:
72 CHARDEV_GET_CLASS(s
)->chr_be_event(s
, event
);
75 /* Not reporting errors from writing to logfile, as logs are
76 * defined to be "best effort" only */
77 static void qemu_chr_write_log(Chardev
*s
, const uint8_t *buf
, size_t len
)
88 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
89 if (ret
== -1 && errno
== EAGAIN
) {
101 static int qemu_chr_write_buffer(Chardev
*s
,
102 const uint8_t *buf
, int len
,
103 int *offset
, bool write_all
)
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
&& write_all
) {
128 qemu_chr_write_log(s
, buf
, *offset
);
130 qemu_mutex_unlock(&s
->chr_write_lock
);
135 int qemu_chr_write(Chardev
*s
, const uint8_t *buf
, int len
, bool write_all
)
140 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
141 replay_char_write_event_load(&res
, &offset
);
142 assert(offset
<= len
);
143 qemu_chr_write_buffer(s
, buf
, offset
, &offset
, true);
147 res
= qemu_chr_write_buffer(s
, buf
, len
, &offset
, write_all
);
149 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
150 replay_char_write_event_save(res
, offset
);
159 int qemu_chr_be_can_write(Chardev
*s
)
161 CharBackend
*be
= s
->be
;
163 if (!be
|| !be
->chr_can_read
) {
167 return be
->chr_can_read(be
->opaque
);
170 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
)
172 CharBackend
*be
= s
->be
;
174 if (be
&& be
->chr_read
) {
175 be
->chr_read(be
->opaque
, buf
, len
);
179 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
)
181 if (qemu_chr_replay(s
)) {
182 if (replay_mode
== REPLAY_MODE_PLAY
) {
185 replay_chr_be_write(s
, buf
, len
);
187 qemu_chr_be_write_impl(s
, buf
, len
);
191 void qemu_chr_be_update_read_handlers(Chardev
*s
,
192 GMainContext
*context
)
194 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
196 assert(qemu_chr_has_feature(s
, QEMU_CHAR_FEATURE_GCONTEXT
)
198 s
->gcontext
= context
;
199 if (cc
->chr_update_read_handler
) {
200 cc
->chr_update_read_handler(s
);
204 int qemu_chr_add_client(Chardev
*s
, int fd
)
206 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
207 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
210 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
211 bool *be_opened
, Error
**errp
)
213 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
214 /* Any ChardevCommon member would work */
215 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
217 if (common
&& common
->has_logfile
) {
218 int flags
= O_WRONLY
| O_CREAT
;
219 if (common
->has_logappend
&&
225 chr
->logfd
= qemu_open(common
->logfile
, flags
, 0666);
226 if (chr
->logfd
< 0) {
227 error_setg_errno(errp
, errno
,
228 "Unable to open logfile %s",
235 cc
->open(chr
, backend
, be_opened
, errp
);
239 static void char_init(Object
*obj
)
241 Chardev
*chr
= CHARDEV(obj
);
244 qemu_mutex_init(&chr
->chr_write_lock
);
247 * Assume if chr_update_read_handler is implemented it will
248 * take the updated gcontext into account.
250 if (CHARDEV_GET_CLASS(chr
)->chr_update_read_handler
) {
251 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
);
256 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
261 static void char_class_init(ObjectClass
*oc
, void *data
)
263 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
265 cc
->chr_write
= null_chr_write
;
266 cc
->chr_be_event
= chr_be_event
;
269 static void char_finalize(Object
*obj
)
271 Chardev
*chr
= CHARDEV(obj
);
276 g_free(chr
->filename
);
278 if (chr
->logfd
!= -1) {
281 qemu_mutex_destroy(&chr
->chr_write_lock
);
284 static const TypeInfo char_type_info
= {
285 .name
= TYPE_CHARDEV
,
286 .parent
= TYPE_OBJECT
,
287 .instance_size
= sizeof(Chardev
),
288 .instance_init
= char_init
,
289 .instance_finalize
= char_finalize
,
291 .class_size
= sizeof(ChardevClass
),
292 .class_init
= char_class_init
,
295 static int chardev_machine_done_notify_one(Object
*child
, void *opaque
)
297 Chardev
*chr
= (Chardev
*)child
;
298 ChardevClass
*class = CHARDEV_GET_CLASS(chr
);
300 if (class->chr_machine_done
) {
301 return class->chr_machine_done(chr
);
307 static void chardev_machine_done_hook(Notifier
*notifier
, void *unused
)
309 int ret
= object_child_foreach(get_chardevs_root(),
310 chardev_machine_done_notify_one
, NULL
);
313 error_report("Failed to call chardev machine_done hooks");
318 static Notifier chardev_machine_done_notify
= {
319 .notify
= chardev_machine_done_hook
,
322 static bool qemu_chr_is_busy(Chardev
*s
)
324 if (CHARDEV_IS_MUX(s
)) {
325 MuxChardev
*d
= MUX_CHARDEV(s
);
326 return d
->mux_cnt
>= 0;
328 return s
->be
!= NULL
;
332 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
334 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
336 if (cc
->chr_wait_connected
) {
337 return cc
->chr_wait_connected(chr
, errp
);
343 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
,
346 char host
[65], port
[33], width
[8], height
[8];
350 Error
*local_err
= NULL
;
352 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
354 error_report_err(local_err
);
358 if (strstart(filename
, "mon:", &p
)) {
359 if (!permit_mux_mon
) {
360 error_report("mon: isn't supported in this context");
364 qemu_opt_set(opts
, "mux", "on", &error_abort
);
365 if (strcmp(filename
, "stdio") == 0) {
366 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
367 * but pass it to the guest. Handle this only for compat syntax,
368 * for -chardev syntax we have special option for this.
369 * This is what -nographic did, redirecting+muxing serial+monitor
370 * to stdio causing Ctrl+C to be passed to guest. */
371 qemu_opt_set(opts
, "signal", "off", &error_abort
);
375 if (strcmp(filename
, "null") == 0 ||
376 strcmp(filename
, "pty") == 0 ||
377 strcmp(filename
, "msmouse") == 0 ||
378 strcmp(filename
, "wctablet") == 0 ||
379 strcmp(filename
, "braille") == 0 ||
380 strcmp(filename
, "testdev") == 0 ||
381 strcmp(filename
, "stdio") == 0) {
382 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
385 if (strstart(filename
, "vc", &p
)) {
386 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
388 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
390 qemu_opt_set(opts
, "width", width
, &error_abort
);
391 qemu_opt_set(opts
, "height", height
, &error_abort
);
392 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
394 qemu_opt_set(opts
, "cols", width
, &error_abort
);
395 qemu_opt_set(opts
, "rows", height
, &error_abort
);
402 if (strcmp(filename
, "con:") == 0) {
403 qemu_opt_set(opts
, "backend", "console", &error_abort
);
406 if (strstart(filename
, "COM", NULL
)) {
407 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
408 qemu_opt_set(opts
, "path", filename
, &error_abort
);
411 if (strstart(filename
, "file:", &p
)) {
412 qemu_opt_set(opts
, "backend", "file", &error_abort
);
413 qemu_opt_set(opts
, "path", p
, &error_abort
);
416 if (strstart(filename
, "pipe:", &p
)) {
417 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
418 qemu_opt_set(opts
, "path", p
, &error_abort
);
421 if (strstart(filename
, "tcp:", &p
) ||
422 strstart(filename
, "telnet:", &p
) ||
423 strstart(filename
, "tn3270:", &p
) ||
424 strstart(filename
, "websocket:", &p
)) {
425 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
427 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
430 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
431 qemu_opt_set(opts
, "host", host
, &error_abort
);
432 qemu_opt_set(opts
, "port", port
, &error_abort
);
434 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
436 error_report_err(local_err
);
440 if (strstart(filename
, "telnet:", &p
)) {
441 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
442 } else if (strstart(filename
, "tn3270:", &p
)) {
443 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
444 } else if (strstart(filename
, "websocket:", &p
)) {
445 qemu_opt_set(opts
, "websocket", "on", &error_abort
);
449 if (strstart(filename
, "udp:", &p
)) {
450 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
451 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
453 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
457 qemu_opt_set(opts
, "host", host
, &error_abort
);
458 qemu_opt_set(opts
, "port", port
, &error_abort
);
461 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
463 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
467 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
468 qemu_opt_set(opts
, "localport", port
, &error_abort
);
472 if (strstart(filename
, "unix:", &p
)) {
473 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
474 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
476 error_report_err(local_err
);
481 if (strstart(filename
, "/dev/parport", NULL
) ||
482 strstart(filename
, "/dev/ppi", NULL
)) {
483 qemu_opt_set(opts
, "backend", "parallel", &error_abort
);
484 qemu_opt_set(opts
, "path", filename
, &error_abort
);
487 if (strstart(filename
, "/dev/", NULL
)) {
488 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
489 qemu_opt_set(opts
, "path", filename
, &error_abort
);
498 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
500 const char *logfile
= qemu_opt_get(opts
, "logfile");
502 backend
->has_logfile
= logfile
!= NULL
;
503 backend
->logfile
= g_strdup(logfile
);
505 backend
->has_logappend
= true;
506 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
509 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
512 const ChardevClass
*cc
;
513 char *typename
= g_strdup_printf("chardev-%s", driver
);
515 oc
= object_class_by_name(typename
);
518 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
519 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
523 if (object_class_is_abstract(oc
)) {
524 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
525 "abstract device type");
529 cc
= CHARDEV_CLASS(oc
);
531 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
538 static const struct ChardevAlias
{
539 const char *typename
;
541 } chardev_alias_table
[] = {
542 #ifdef HAVE_CHARDEV_PARPORT
543 { "parallel", "parport" },
545 #ifdef HAVE_CHARDEV_SERIAL
550 typedef struct ChadevClassFE
{
551 void (*fn
)(const char *name
, void *opaque
);
556 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
558 ChadevClassFE
*fe
= opaque
;
560 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
561 if (CHARDEV_CLASS(klass
)->internal
) {
565 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
569 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
), void *opaque
)
571 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
574 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
576 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
577 fn(chardev_alias_table
[i
].alias
, opaque
);
582 help_string_append(const char *name
, void *opaque
)
584 GString
*str
= opaque
;
586 g_string_append_printf(str
, "\n %s", name
);
589 static const char *chardev_alias_translate(const char *name
)
592 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
593 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
594 return chardev_alias_table
[i
].typename
;
600 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
, Error
**errp
)
602 Error
*local_err
= NULL
;
603 const ChardevClass
*cc
;
604 ChardevBackend
*backend
= NULL
;
605 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
608 error_setg(errp
, "chardev: \"%s\" missing backend",
613 cc
= char_get_class(name
, errp
);
618 backend
= g_new0(ChardevBackend
, 1);
619 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
622 cc
->parse(opts
, backend
, &local_err
);
624 error_propagate(errp
, local_err
);
625 qapi_free_ChardevBackend(backend
);
629 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
630 qemu_chr_parse_common(opts
, ccom
);
631 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
637 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
, Error
**errp
)
639 const ChardevClass
*cc
;
641 ChardevBackend
*backend
= NULL
;
642 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
643 const char *id
= qemu_opts_id(opts
);
646 if (name
&& is_help_option(name
)) {
647 GString
*str
= g_string_new("");
649 chardev_name_foreach(help_string_append
, str
);
651 error_printf("Available chardev backend types: %s\n", str
->str
);
652 g_string_free(str
, true);
657 error_setg(errp
, "chardev: no id specified");
661 backend
= qemu_chr_parse_opts(opts
, errp
);
662 if (backend
== NULL
) {
666 cc
= char_get_class(name
, errp
);
671 if (qemu_opt_get_bool(opts
, "mux", 0)) {
672 bid
= g_strdup_printf("%s-base", id
);
675 chr
= qemu_chardev_new(bid
? bid
: id
,
676 object_class_get_name(OBJECT_CLASS(cc
)),
685 qapi_free_ChardevBackend(backend
);
686 backend
= g_new0(ChardevBackend
, 1);
687 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
688 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
689 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
690 mux
= qemu_chardev_new(id
, TYPE_CHARDEV_MUX
, backend
, errp
);
692 object_unparent(OBJECT(chr
));
700 qapi_free_ChardevBackend(backend
);
705 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
713 if (strstart(filename
, "chardev:", &p
)) {
714 return qemu_chr_find(p
);
717 opts
= qemu_chr_parse_compat(label
, filename
, permit_mux_mon
);
721 chr
= qemu_chr_new_from_opts(opts
, &err
);
723 error_report_err(err
);
727 if (qemu_opt_get_bool(opts
, "mux", 0)) {
728 assert(permit_mux_mon
);
729 monitor_init(chr
, MONITOR_USE_READLINE
);
737 static Chardev
*qemu_chr_new_permit_mux_mon(const char *label
,
738 const char *filename
,
742 chr
= qemu_chr_new_noreplay(label
, filename
, permit_mux_mon
);
744 if (replay_mode
!= REPLAY_MODE_NONE
) {
745 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
747 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
748 error_report("Replay: ioctl is not supported "
749 "for serial devices yet");
751 replay_register_char_driver(chr
);
756 Chardev
*qemu_chr_new(const char *label
, const char *filename
)
758 return qemu_chr_new_permit_mux_mon(label
, filename
, false);
761 Chardev
*qemu_chr_new_mux_mon(const char *label
, const char *filename
)
763 return qemu_chr_new_permit_mux_mon(label
, filename
, true);
766 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
768 Chardev
*chr
= CHARDEV(obj
);
769 ChardevInfoList
**list
= data
;
770 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
772 info
->value
= g_malloc0(sizeof(*info
->value
));
773 info
->value
->label
= g_strdup(chr
->label
);
774 info
->value
->filename
= g_strdup(chr
->filename
);
775 info
->value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
783 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
785 ChardevInfoList
*chr_list
= NULL
;
787 object_child_foreach(get_chardevs_root(),
788 qmp_query_chardev_foreach
, &chr_list
);
794 qmp_prepend_backend(const char *name
, void *opaque
)
796 ChardevBackendInfoList
**list
= opaque
;
797 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
799 info
->value
= g_malloc0(sizeof(*info
->value
));
800 info
->value
->name
= g_strdup(name
);
805 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
807 ChardevBackendInfoList
*backend_list
= NULL
;
809 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
814 Chardev
*qemu_chr_find(const char *name
)
816 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
818 return obj
? CHARDEV(obj
) : NULL
;
821 QemuOptsList qemu_chardev_opts
= {
823 .implied_opt_name
= "backend",
824 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
828 .type
= QEMU_OPT_STRING
,
831 .type
= QEMU_OPT_STRING
,
834 .type
= QEMU_OPT_STRING
,
837 .type
= QEMU_OPT_STRING
,
840 .type
= QEMU_OPT_STRING
,
843 .type
= QEMU_OPT_STRING
,
846 .type
= QEMU_OPT_STRING
,
849 .type
= QEMU_OPT_NUMBER
,
852 .type
= QEMU_OPT_BOOL
,
855 .type
= QEMU_OPT_BOOL
,
858 .type
= QEMU_OPT_BOOL
,
861 .type
= QEMU_OPT_BOOL
,
864 .type
= QEMU_OPT_BOOL
,
867 .type
= QEMU_OPT_NUMBER
,
870 .type
= QEMU_OPT_BOOL
,
873 .type
= QEMU_OPT_BOOL
,
876 .type
= QEMU_OPT_STRING
,
879 .type
= QEMU_OPT_BOOL
,
882 .type
= QEMU_OPT_NUMBER
,
885 .type
= QEMU_OPT_NUMBER
,
888 .type
= QEMU_OPT_NUMBER
,
891 .type
= QEMU_OPT_NUMBER
,
894 .type
= QEMU_OPT_BOOL
,
897 .type
= QEMU_OPT_BOOL
,
900 .type
= QEMU_OPT_STRING
,
903 .type
= QEMU_OPT_NUMBER
,
906 .type
= QEMU_OPT_SIZE
,
909 .type
= QEMU_OPT_STRING
,
912 .type
= QEMU_OPT_BOOL
,
915 .type
= QEMU_OPT_STRING
,
918 .type
= QEMU_OPT_BOOL
,
920 { /* end of list */ }
924 bool qemu_chr_has_feature(Chardev
*chr
,
925 ChardevFeature feature
)
927 return test_bit(feature
, chr
->features
);
930 void qemu_chr_set_feature(Chardev
*chr
,
931 ChardevFeature feature
)
933 return set_bit(feature
, chr
->features
);
936 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
937 ChardevBackend
*backend
,
942 Error
*local_err
= NULL
;
943 bool be_opened
= true;
945 assert(g_str_has_prefix(typename
, "chardev-"));
947 obj
= object_new(typename
);
949 chr
->label
= g_strdup(id
);
951 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
956 if (!chr
->filename
) {
957 chr
->filename
= g_strdup(typename
+ 8);
960 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
964 object_property_add_child(get_chardevs_root(), id
, obj
, &local_err
);
973 error_propagate(errp
, local_err
);
981 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
984 const ChardevClass
*cc
;
988 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
993 chr
= qemu_chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
999 ret
= g_new0(ChardevReturn
, 1);
1000 if (CHARDEV_IS_PTY(chr
)) {
1001 ret
->pty
= g_strdup(chr
->filename
+ 4);
1002 ret
->has_pty
= true;
1008 ChardevReturn
*qmp_chardev_change(const char *id
, ChardevBackend
*backend
,
1012 const ChardevClass
*cc
;
1013 Chardev
*chr
, *chr_new
;
1014 bool closed_sent
= false;
1017 chr
= qemu_chr_find(id
);
1019 error_setg(errp
, "Chardev '%s' does not exist", id
);
1023 if (CHARDEV_IS_MUX(chr
)) {
1024 error_setg(errp
, "Mux device hotswap not supported yet");
1028 if (qemu_chr_replay(chr
)) {
1030 "Chardev '%s' cannot be changed in record/replay mode", id
);
1037 object_unparent(OBJECT(chr
));
1038 return qmp_chardev_add(id
, backend
, errp
);
1041 if (!be
->chr_be_change
) {
1042 error_setg(errp
, "Chardev user does not support chardev hotswap");
1046 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1051 chr_new
= qemu_chardev_new(NULL
, object_class_get_name(OBJECT_CLASS(cc
)),
1056 chr_new
->label
= g_strdup(id
);
1058 if (chr
->be_open
&& !chr_new
->be_open
) {
1059 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1064 qemu_chr_fe_init(be
, chr_new
, &error_abort
);
1066 if (be
->chr_be_change(be
->opaque
) < 0) {
1067 error_setg(errp
, "Chardev '%s' change failed", chr_new
->label
);
1069 qemu_chr_fe_init(be
, chr
, &error_abort
);
1071 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1073 object_unref(OBJECT(chr_new
));
1077 object_unparent(OBJECT(chr
));
1078 object_property_add_child(get_chardevs_root(), chr_new
->label
,
1079 OBJECT(chr_new
), &error_abort
);
1080 object_unref(OBJECT(chr_new
));
1082 ret
= g_new0(ChardevReturn
, 1);
1083 if (CHARDEV_IS_PTY(chr_new
)) {
1084 ret
->pty
= g_strdup(chr_new
->filename
+ 4);
1085 ret
->has_pty
= true;
1091 void qmp_chardev_remove(const char *id
, Error
**errp
)
1095 chr
= qemu_chr_find(id
);
1097 error_setg(errp
, "Chardev '%s' not found", id
);
1100 if (qemu_chr_is_busy(chr
)) {
1101 error_setg(errp
, "Chardev '%s' is busy", id
);
1104 if (qemu_chr_replay(chr
)) {
1106 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1109 object_unparent(OBJECT(chr
));
1112 void qmp_chardev_send_break(const char *id
, Error
**errp
)
1116 chr
= qemu_chr_find(id
);
1118 error_setg(errp
, "Chardev '%s' not found", id
);
1121 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
1125 * Add a timeout callback for the chardev (in milliseconds), return
1126 * the GSource object created. Please use this to add timeout hook for
1127 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1128 * make sure the gcontext that the task bound to is correct.
1130 GSource
*qemu_chr_timeout_add_ms(Chardev
*chr
, guint ms
,
1131 GSourceFunc func
, void *private)
1133 GSource
*source
= g_timeout_source_new(ms
);
1136 g_source_set_callback(source
, func
, private, NULL
);
1137 g_source_attach(source
, chr
->gcontext
);
1142 void qemu_chr_cleanup(void)
1144 object_unparent(get_chardevs_root());
1147 static void register_types(void)
1149 type_register_static(&char_type_info
);
1151 /* this must be done after machine init, since we register FEs with muxes
1152 * as part of realize functions like serial_isa_realizefn when -nographic
1155 qemu_add_machine_init_done_notifier(&chardev_machine_done_notify
);
1158 type_init(register_types
);