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 "qemu/qemu-print.h"
32 #include "chardev/char.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-commands-char.h"
35 #include "qapi/qmp/qerror.h"
36 #include "sysemu/replay.h"
37 #include "qemu/help_option.h"
38 #include "qemu/module.h"
39 #include "qemu/option.h"
42 #include "chardev/char-mux.h"
44 /***********************************************************/
45 /* character device */
47 static Object
*get_chardevs_root(void)
49 return container_get(object_get_root(), "/chardevs");
52 static void chr_be_event(Chardev
*s
, QEMUChrEvent event
)
54 CharBackend
*be
= s
->be
;
56 if (!be
|| !be
->chr_event
) {
60 be
->chr_event(be
->opaque
, event
);
63 void qemu_chr_be_event(Chardev
*s
, QEMUChrEvent event
)
65 /* Keep track if the char device is open */
67 case CHR_EVENT_OPENED
:
70 case CHR_EVENT_CLOSED
:
74 case CHR_EVENT_MUX_IN
:
75 case CHR_EVENT_MUX_OUT
:
80 CHARDEV_GET_CLASS(s
)->chr_be_event(s
, event
);
83 /* Not reporting errors from writing to logfile, as logs are
84 * defined to be "best effort" only */
85 static void qemu_chr_write_log(Chardev
*s
, const uint8_t *buf
, size_t len
)
96 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
97 if (ret
== -1 && errno
== EAGAIN
) {
109 static int qemu_chr_write_buffer(Chardev
*s
,
110 const uint8_t *buf
, int len
,
111 int *offset
, bool write_all
)
113 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
117 qemu_mutex_lock(&s
->chr_write_lock
);
118 while (*offset
< len
) {
120 res
= cc
->chr_write(s
, buf
+ *offset
, len
- *offset
);
121 if (res
< 0 && errno
== EAGAIN
&& write_all
) {
136 qemu_chr_write_log(s
, buf
, *offset
);
138 qemu_mutex_unlock(&s
->chr_write_lock
);
143 int qemu_chr_write(Chardev
*s
, const uint8_t *buf
, int len
, bool write_all
)
148 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
149 replay_char_write_event_load(&res
, &offset
);
150 assert(offset
<= len
);
151 qemu_chr_write_buffer(s
, buf
, offset
, &offset
, true);
155 res
= qemu_chr_write_buffer(s
, buf
, len
, &offset
, write_all
);
157 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
158 replay_char_write_event_save(res
, offset
);
167 int qemu_chr_be_can_write(Chardev
*s
)
169 CharBackend
*be
= s
->be
;
171 if (!be
|| !be
->chr_can_read
) {
175 return be
->chr_can_read(be
->opaque
);
178 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
)
180 CharBackend
*be
= s
->be
;
182 if (be
&& be
->chr_read
) {
183 be
->chr_read(be
->opaque
, buf
, len
);
187 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
)
189 if (qemu_chr_replay(s
)) {
190 if (replay_mode
== REPLAY_MODE_PLAY
) {
193 replay_chr_be_write(s
, buf
, len
);
195 qemu_chr_be_write_impl(s
, buf
, len
);
199 void qemu_chr_be_update_read_handlers(Chardev
*s
,
200 GMainContext
*context
)
202 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
204 assert(qemu_chr_has_feature(s
, QEMU_CHAR_FEATURE_GCONTEXT
)
206 s
->gcontext
= context
;
207 if (cc
->chr_update_read_handler
) {
208 cc
->chr_update_read_handler(s
);
212 int qemu_chr_add_client(Chardev
*s
, int fd
)
214 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
215 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
218 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
219 bool *be_opened
, Error
**errp
)
221 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
222 /* Any ChardevCommon member would work */
223 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
225 if (common
&& common
->has_logfile
) {
226 int flags
= O_WRONLY
| O_CREAT
;
227 if (common
->has_logappend
&&
233 chr
->logfd
= qemu_open(common
->logfile
, flags
, 0666);
234 if (chr
->logfd
< 0) {
235 error_setg_errno(errp
, errno
,
236 "Unable to open logfile %s",
243 cc
->open(chr
, backend
, be_opened
, errp
);
247 static void char_init(Object
*obj
)
249 Chardev
*chr
= CHARDEV(obj
);
252 qemu_mutex_init(&chr
->chr_write_lock
);
255 * Assume if chr_update_read_handler is implemented it will
256 * take the updated gcontext into account.
258 if (CHARDEV_GET_CLASS(chr
)->chr_update_read_handler
) {
259 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
);
264 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
269 static void char_class_init(ObjectClass
*oc
, void *data
)
271 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
273 cc
->chr_write
= null_chr_write
;
274 cc
->chr_be_event
= chr_be_event
;
277 static void char_finalize(Object
*obj
)
279 Chardev
*chr
= CHARDEV(obj
);
284 g_free(chr
->filename
);
286 if (chr
->logfd
!= -1) {
289 qemu_mutex_destroy(&chr
->chr_write_lock
);
292 static const TypeInfo char_type_info
= {
293 .name
= TYPE_CHARDEV
,
294 .parent
= TYPE_OBJECT
,
295 .instance_size
= sizeof(Chardev
),
296 .instance_init
= char_init
,
297 .instance_finalize
= char_finalize
,
299 .class_size
= sizeof(ChardevClass
),
300 .class_init
= char_class_init
,
303 static int chardev_machine_done_notify_one(Object
*child
, void *opaque
)
305 Chardev
*chr
= (Chardev
*)child
;
306 ChardevClass
*class = CHARDEV_GET_CLASS(chr
);
308 if (class->chr_machine_done
) {
309 return class->chr_machine_done(chr
);
315 static void chardev_machine_done_hook(Notifier
*notifier
, void *unused
)
317 int ret
= object_child_foreach(get_chardevs_root(),
318 chardev_machine_done_notify_one
, NULL
);
321 error_report("Failed to call chardev machine_done hooks");
326 static Notifier chardev_machine_done_notify
= {
327 .notify
= chardev_machine_done_hook
,
330 static bool qemu_chr_is_busy(Chardev
*s
)
332 if (CHARDEV_IS_MUX(s
)) {
333 MuxChardev
*d
= MUX_CHARDEV(s
);
334 return d
->mux_cnt
>= 0;
336 return s
->be
!= NULL
;
340 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
342 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
344 if (cc
->chr_wait_connected
) {
345 return cc
->chr_wait_connected(chr
, errp
);
351 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
,
354 char host
[65], port
[33], width
[8], height
[8];
358 Error
*local_err
= NULL
;
360 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
362 error_report_err(local_err
);
366 if (strstart(filename
, "mon:", &p
)) {
367 if (!permit_mux_mon
) {
368 error_report("mon: isn't supported in this context");
372 qemu_opt_set(opts
, "mux", "on", &error_abort
);
373 if (strcmp(filename
, "stdio") == 0) {
374 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
375 * but pass it to the guest. Handle this only for compat syntax,
376 * for -chardev syntax we have special option for this.
377 * This is what -nographic did, redirecting+muxing serial+monitor
378 * to stdio causing Ctrl+C to be passed to guest. */
379 qemu_opt_set(opts
, "signal", "off", &error_abort
);
383 if (strcmp(filename
, "null") == 0 ||
384 strcmp(filename
, "pty") == 0 ||
385 strcmp(filename
, "msmouse") == 0 ||
386 strcmp(filename
, "wctablet") == 0 ||
387 strcmp(filename
, "braille") == 0 ||
388 strcmp(filename
, "testdev") == 0 ||
389 strcmp(filename
, "stdio") == 0) {
390 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
393 if (strstart(filename
, "vc", &p
)) {
394 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
396 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
398 qemu_opt_set(opts
, "width", width
, &error_abort
);
399 qemu_opt_set(opts
, "height", height
, &error_abort
);
400 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
402 qemu_opt_set(opts
, "cols", width
, &error_abort
);
403 qemu_opt_set(opts
, "rows", height
, &error_abort
);
410 if (strcmp(filename
, "con:") == 0) {
411 qemu_opt_set(opts
, "backend", "console", &error_abort
);
414 if (strstart(filename
, "COM", NULL
)) {
415 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
416 qemu_opt_set(opts
, "path", filename
, &error_abort
);
419 if (strstart(filename
, "file:", &p
)) {
420 qemu_opt_set(opts
, "backend", "file", &error_abort
);
421 qemu_opt_set(opts
, "path", p
, &error_abort
);
424 if (strstart(filename
, "pipe:", &p
)) {
425 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
426 qemu_opt_set(opts
, "path", p
, &error_abort
);
429 if (strstart(filename
, "tcp:", &p
) ||
430 strstart(filename
, "telnet:", &p
) ||
431 strstart(filename
, "tn3270:", &p
) ||
432 strstart(filename
, "websocket:", &p
)) {
433 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
435 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
438 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
439 qemu_opt_set(opts
, "host", host
, &error_abort
);
440 qemu_opt_set(opts
, "port", port
, &error_abort
);
442 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
444 error_report_err(local_err
);
448 if (strstart(filename
, "telnet:", &p
)) {
449 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
450 } else if (strstart(filename
, "tn3270:", &p
)) {
451 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
452 } else if (strstart(filename
, "websocket:", &p
)) {
453 qemu_opt_set(opts
, "websocket", "on", &error_abort
);
457 if (strstart(filename
, "udp:", &p
)) {
458 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
459 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
461 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
465 qemu_opt_set(opts
, "host", host
, &error_abort
);
466 qemu_opt_set(opts
, "port", port
, &error_abort
);
469 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
471 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
475 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
476 qemu_opt_set(opts
, "localport", port
, &error_abort
);
480 if (strstart(filename
, "unix:", &p
)) {
481 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
482 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
484 error_report_err(local_err
);
489 if (strstart(filename
, "/dev/parport", NULL
) ||
490 strstart(filename
, "/dev/ppi", NULL
)) {
491 qemu_opt_set(opts
, "backend", "parallel", &error_abort
);
492 qemu_opt_set(opts
, "path", filename
, &error_abort
);
495 if (strstart(filename
, "/dev/", NULL
)) {
496 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
497 qemu_opt_set(opts
, "path", filename
, &error_abort
);
501 error_report("'%s' is not a valid char driver", filename
);
508 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
510 const char *logfile
= qemu_opt_get(opts
, "logfile");
512 backend
->has_logfile
= logfile
!= NULL
;
513 backend
->logfile
= g_strdup(logfile
);
515 backend
->has_logappend
= true;
516 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
519 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
522 const ChardevClass
*cc
;
523 char *typename
= g_strdup_printf("chardev-%s", driver
);
525 oc
= object_class_by_name(typename
);
528 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
529 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
533 if (object_class_is_abstract(oc
)) {
534 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
535 "abstract device type");
539 cc
= CHARDEV_CLASS(oc
);
541 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
548 static const struct ChardevAlias
{
549 const char *typename
;
551 } chardev_alias_table
[] = {
552 #ifdef HAVE_CHARDEV_PARPORT
553 { "parallel", "parport" },
555 #ifdef HAVE_CHARDEV_SERIAL
560 typedef struct ChadevClassFE
{
561 void (*fn
)(const char *name
, void *opaque
);
566 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
568 ChadevClassFE
*fe
= opaque
;
570 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
571 if (CHARDEV_CLASS(klass
)->internal
) {
575 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
579 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
), void *opaque
)
581 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
584 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
586 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
587 fn(chardev_alias_table
[i
].alias
, opaque
);
592 help_string_append(const char *name
, void *opaque
)
594 GString
*str
= opaque
;
596 g_string_append_printf(str
, "\n %s", name
);
599 static const char *chardev_alias_translate(const char *name
)
602 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
603 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
604 return chardev_alias_table
[i
].typename
;
610 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
, Error
**errp
)
612 Error
*local_err
= NULL
;
613 const ChardevClass
*cc
;
614 ChardevBackend
*backend
= NULL
;
615 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
618 error_setg(errp
, "chardev: \"%s\" missing backend",
623 cc
= char_get_class(name
, errp
);
628 backend
= g_new0(ChardevBackend
, 1);
629 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
632 cc
->parse(opts
, backend
, &local_err
);
634 error_propagate(errp
, local_err
);
635 qapi_free_ChardevBackend(backend
);
639 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
640 qemu_chr_parse_common(opts
, ccom
);
641 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
647 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
, GMainContext
*context
,
650 const ChardevClass
*cc
;
652 ChardevBackend
*backend
= NULL
;
653 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
654 const char *id
= qemu_opts_id(opts
);
657 if (name
&& is_help_option(name
)) {
658 GString
*str
= g_string_new("");
660 chardev_name_foreach(help_string_append
, str
);
662 qemu_printf("Available chardev backend types: %s\n", str
->str
);
663 g_string_free(str
, true);
668 error_setg(errp
, "chardev: no id specified");
672 backend
= qemu_chr_parse_opts(opts
, errp
);
673 if (backend
== NULL
) {
677 cc
= char_get_class(name
, errp
);
682 if (qemu_opt_get_bool(opts
, "mux", 0)) {
683 bid
= g_strdup_printf("%s-base", id
);
686 chr
= qemu_chardev_new(bid
? bid
: id
,
687 object_class_get_name(OBJECT_CLASS(cc
)),
688 backend
, context
, errp
);
696 qapi_free_ChardevBackend(backend
);
697 backend
= g_new0(ChardevBackend
, 1);
698 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
699 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
700 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
701 mux
= qemu_chardev_new(id
, TYPE_CHARDEV_MUX
, backend
, context
, errp
);
703 object_unparent(OBJECT(chr
));
711 qapi_free_ChardevBackend(backend
);
716 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
717 bool permit_mux_mon
, GMainContext
*context
)
724 if (strstart(filename
, "chardev:", &p
)) {
725 return qemu_chr_find(p
);
728 opts
= qemu_chr_parse_compat(label
, filename
, permit_mux_mon
);
732 chr
= qemu_chr_new_from_opts(opts
, context
, &err
);
734 error_report_err(err
);
738 if (qemu_opt_get_bool(opts
, "mux", 0)) {
739 assert(permit_mux_mon
);
740 monitor_init_hmp(chr
, true);
748 static Chardev
*qemu_chr_new_permit_mux_mon(const char *label
,
749 const char *filename
,
751 GMainContext
*context
)
754 chr
= qemu_chr_new_noreplay(label
, filename
, permit_mux_mon
, context
);
756 if (replay_mode
!= REPLAY_MODE_NONE
) {
757 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
759 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
760 error_report("Replay: ioctl is not supported "
761 "for serial devices yet");
763 replay_register_char_driver(chr
);
768 Chardev
*qemu_chr_new(const char *label
, const char *filename
,
769 GMainContext
*context
)
771 return qemu_chr_new_permit_mux_mon(label
, filename
, false, context
);
774 Chardev
*qemu_chr_new_mux_mon(const char *label
, const char *filename
,
775 GMainContext
*context
)
777 return qemu_chr_new_permit_mux_mon(label
, filename
, true, context
);
780 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
782 Chardev
*chr
= CHARDEV(obj
);
783 ChardevInfoList
**list
= data
;
784 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
786 info
->value
= g_malloc0(sizeof(*info
->value
));
787 info
->value
->label
= g_strdup(chr
->label
);
788 info
->value
->filename
= g_strdup(chr
->filename
);
789 info
->value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
797 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
799 ChardevInfoList
*chr_list
= NULL
;
801 object_child_foreach(get_chardevs_root(),
802 qmp_query_chardev_foreach
, &chr_list
);
808 qmp_prepend_backend(const char *name
, void *opaque
)
810 ChardevBackendInfoList
**list
= opaque
;
811 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
813 info
->value
= g_malloc0(sizeof(*info
->value
));
814 info
->value
->name
= g_strdup(name
);
819 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
821 ChardevBackendInfoList
*backend_list
= NULL
;
823 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
828 Chardev
*qemu_chr_find(const char *name
)
830 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
832 return obj
? CHARDEV(obj
) : NULL
;
835 QemuOptsList qemu_chardev_opts
= {
837 .implied_opt_name
= "backend",
838 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
842 .type
= QEMU_OPT_STRING
,
845 .type
= QEMU_OPT_STRING
,
848 .type
= QEMU_OPT_STRING
,
851 .type
= QEMU_OPT_STRING
,
854 .type
= QEMU_OPT_STRING
,
857 .type
= QEMU_OPT_STRING
,
860 .type
= QEMU_OPT_STRING
,
863 .type
= QEMU_OPT_NUMBER
,
866 .type
= QEMU_OPT_BOOL
,
869 .type
= QEMU_OPT_BOOL
,
872 .type
= QEMU_OPT_BOOL
,
875 .type
= QEMU_OPT_BOOL
,
878 .type
= QEMU_OPT_BOOL
,
881 .type
= QEMU_OPT_NUMBER
,
884 .type
= QEMU_OPT_BOOL
,
887 .type
= QEMU_OPT_BOOL
,
890 .type
= QEMU_OPT_STRING
,
893 .type
= QEMU_OPT_STRING
,
896 .type
= QEMU_OPT_BOOL
,
899 .type
= QEMU_OPT_NUMBER
,
902 .type
= QEMU_OPT_NUMBER
,
905 .type
= QEMU_OPT_NUMBER
,
908 .type
= QEMU_OPT_NUMBER
,
911 .type
= QEMU_OPT_BOOL
,
914 .type
= QEMU_OPT_BOOL
,
917 .type
= QEMU_OPT_STRING
,
920 .type
= QEMU_OPT_NUMBER
,
923 .type
= QEMU_OPT_SIZE
,
926 .type
= QEMU_OPT_STRING
,
929 .type
= QEMU_OPT_BOOL
,
932 .type
= QEMU_OPT_STRING
,
935 .type
= QEMU_OPT_BOOL
,
937 { /* end of list */ }
941 bool qemu_chr_has_feature(Chardev
*chr
,
942 ChardevFeature feature
)
944 return test_bit(feature
, chr
->features
);
947 void qemu_chr_set_feature(Chardev
*chr
,
948 ChardevFeature feature
)
950 return set_bit(feature
, chr
->features
);
953 static Chardev
*chardev_new(const char *id
, const char *typename
,
954 ChardevBackend
*backend
,
955 GMainContext
*gcontext
,
960 Error
*local_err
= NULL
;
961 bool be_opened
= true;
963 assert(g_str_has_prefix(typename
, "chardev-"));
965 obj
= object_new(typename
);
967 chr
->label
= g_strdup(id
);
968 chr
->gcontext
= gcontext
;
970 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
975 if (!chr
->filename
) {
976 chr
->filename
= g_strdup(typename
+ 8);
979 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
983 object_property_add_child(get_chardevs_root(), id
, obj
, &local_err
);
992 error_propagate(errp
, local_err
);
1000 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
1001 ChardevBackend
*backend
,
1002 GMainContext
*gcontext
,
1005 g_autofree
char *genid
= NULL
;
1008 genid
= id_generate(ID_CHR
);
1012 return chardev_new(id
, typename
, backend
, gcontext
, errp
);
1015 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
1018 const ChardevClass
*cc
;
1022 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1027 chr
= chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
1028 backend
, NULL
, errp
);
1033 ret
= g_new0(ChardevReturn
, 1);
1034 if (CHARDEV_IS_PTY(chr
)) {
1035 ret
->pty
= g_strdup(chr
->filename
+ 4);
1036 ret
->has_pty
= true;
1042 ChardevReturn
*qmp_chardev_change(const char *id
, ChardevBackend
*backend
,
1046 const ChardevClass
*cc
;
1047 Chardev
*chr
, *chr_new
;
1048 bool closed_sent
= false;
1051 chr
= qemu_chr_find(id
);
1053 error_setg(errp
, "Chardev '%s' does not exist", id
);
1057 if (CHARDEV_IS_MUX(chr
)) {
1058 error_setg(errp
, "Mux device hotswap not supported yet");
1062 if (qemu_chr_replay(chr
)) {
1064 "Chardev '%s' cannot be changed in record/replay mode", id
);
1071 object_unparent(OBJECT(chr
));
1072 return qmp_chardev_add(id
, backend
, errp
);
1075 if (!be
->chr_be_change
) {
1076 error_setg(errp
, "Chardev user does not support chardev hotswap");
1080 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1085 chr_new
= chardev_new(NULL
, object_class_get_name(OBJECT_CLASS(cc
)),
1086 backend
, chr
->gcontext
, errp
);
1090 chr_new
->label
= g_strdup(id
);
1092 if (chr
->be_open
&& !chr_new
->be_open
) {
1093 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1098 qemu_chr_fe_init(be
, chr_new
, &error_abort
);
1100 if (be
->chr_be_change(be
->opaque
) < 0) {
1101 error_setg(errp
, "Chardev '%s' change failed", chr_new
->label
);
1103 qemu_chr_fe_init(be
, chr
, &error_abort
);
1105 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1107 object_unref(OBJECT(chr_new
));
1111 object_unparent(OBJECT(chr
));
1112 object_property_add_child(get_chardevs_root(), chr_new
->label
,
1113 OBJECT(chr_new
), &error_abort
);
1114 object_unref(OBJECT(chr_new
));
1116 ret
= g_new0(ChardevReturn
, 1);
1117 if (CHARDEV_IS_PTY(chr_new
)) {
1118 ret
->pty
= g_strdup(chr_new
->filename
+ 4);
1119 ret
->has_pty
= true;
1125 void qmp_chardev_remove(const char *id
, Error
**errp
)
1129 chr
= qemu_chr_find(id
);
1131 error_setg(errp
, "Chardev '%s' not found", id
);
1134 if (qemu_chr_is_busy(chr
)) {
1135 error_setg(errp
, "Chardev '%s' is busy", id
);
1138 if (qemu_chr_replay(chr
)) {
1140 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1143 object_unparent(OBJECT(chr
));
1146 void qmp_chardev_send_break(const char *id
, Error
**errp
)
1150 chr
= qemu_chr_find(id
);
1152 error_setg(errp
, "Chardev '%s' not found", id
);
1155 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
1159 * Add a timeout callback for the chardev (in milliseconds), return
1160 * the GSource object created. Please use this to add timeout hook for
1161 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1162 * make sure the gcontext that the task bound to is correct.
1164 GSource
*qemu_chr_timeout_add_ms(Chardev
*chr
, guint ms
,
1165 GSourceFunc func
, void *private)
1167 GSource
*source
= g_timeout_source_new(ms
);
1170 g_source_set_callback(source
, func
, private, NULL
);
1171 g_source_attach(source
, chr
->gcontext
);
1176 void qemu_chr_cleanup(void)
1178 object_unparent(get_chardevs_root());
1181 static void register_types(void)
1183 type_register_static(&char_type_info
);
1185 /* this must be done after machine init, since we register FEs with muxes
1186 * as part of realize functions like serial_isa_realizefn when -nographic
1189 qemu_add_machine_init_done_notifier(&chardev_machine_done_notify
);
1192 type_init(register_types
);