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 "qemu/config-file.h"
29 #include "qemu/error-report.h"
30 #include "qemu/qemu-print.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/module.h"
38 #include "qemu/option.h"
40 #include "qemu/coroutine.h"
41 #include "qemu/yank.h"
43 #include "chardev-internal.h"
45 /***********************************************************/
46 /* character device */
48 Object
*get_chardevs_root(void)
50 return container_get(object_get_root(), "/chardevs");
53 static void chr_be_event(Chardev
*s
, QEMUChrEvent event
)
55 CharBackend
*be
= s
->be
;
57 if (!be
|| !be
->chr_event
) {
61 be
->chr_event(be
->opaque
, event
);
64 void qemu_chr_be_event(Chardev
*s
, QEMUChrEvent event
)
66 /* Keep track if the char device is open */
68 case CHR_EVENT_OPENED
:
71 case CHR_EVENT_CLOSED
:
75 case CHR_EVENT_MUX_IN
:
76 case CHR_EVENT_MUX_OUT
:
81 CHARDEV_GET_CLASS(s
)->chr_be_event(s
, event
);
84 /* Not reporting errors from writing to logfile, as logs are
85 * defined to be "best effort" only */
86 static void qemu_chr_write_log(Chardev
*s
, const uint8_t *buf
, size_t len
)
97 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
98 if (ret
== -1 && errno
== EAGAIN
) {
110 static int qemu_chr_write_buffer(Chardev
*s
,
111 const uint8_t *buf
, int len
,
112 int *offset
, bool write_all
)
114 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
118 qemu_mutex_lock(&s
->chr_write_lock
);
119 while (*offset
< len
) {
121 res
= cc
->chr_write(s
, buf
+ *offset
, len
- *offset
);
122 if (res
< 0 && errno
== EAGAIN
&& write_all
) {
123 if (qemu_in_coroutine()) {
124 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
142 * If some data was written by backend, we should
143 * only log what was actually written. This method
144 * may be invoked again to write the remaining
145 * method, thus we'll log the remainder at that time.
147 qemu_chr_write_log(s
, buf
, *offset
);
148 } else if (res
< 0) {
150 * If a fatal error was reported by the backend,
151 * assume this method won't be invoked again with
152 * this buffer, so log it all right away.
154 qemu_chr_write_log(s
, buf
, len
);
156 qemu_mutex_unlock(&s
->chr_write_lock
);
161 int qemu_chr_write(Chardev
*s
, const uint8_t *buf
, int len
, bool write_all
)
166 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
167 replay_char_write_event_load(&res
, &offset
);
168 assert(offset
<= len
);
169 qemu_chr_write_buffer(s
, buf
, offset
, &offset
, true);
173 res
= qemu_chr_write_buffer(s
, buf
, len
, &offset
, write_all
);
175 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
176 replay_char_write_event_save(res
, offset
);
185 int qemu_chr_be_can_write(Chardev
*s
)
187 CharBackend
*be
= s
->be
;
189 if (!be
|| !be
->chr_can_read
) {
193 return be
->chr_can_read(be
->opaque
);
196 void qemu_chr_be_write_impl(Chardev
*s
, const uint8_t *buf
, int len
)
198 CharBackend
*be
= s
->be
;
200 if (be
&& be
->chr_read
) {
201 be
->chr_read(be
->opaque
, buf
, len
);
205 void qemu_chr_be_write(Chardev
*s
, const uint8_t *buf
, int len
)
207 if (qemu_chr_replay(s
)) {
208 if (replay_mode
== REPLAY_MODE_PLAY
) {
211 replay_chr_be_write(s
, buf
, len
);
213 qemu_chr_be_write_impl(s
, buf
, len
);
217 void qemu_chr_be_update_read_handlers(Chardev
*s
,
218 GMainContext
*context
)
220 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
222 assert(qemu_chr_has_feature(s
, QEMU_CHAR_FEATURE_GCONTEXT
)
224 s
->gcontext
= context
;
225 if (cc
->chr_update_read_handler
) {
226 cc
->chr_update_read_handler(s
);
230 int qemu_chr_add_client(Chardev
*s
, int fd
)
232 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
233 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
236 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
237 bool *be_opened
, Error
**errp
)
239 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
240 /* Any ChardevCommon member would work */
241 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
243 if (common
&& common
->has_logfile
) {
244 int flags
= O_WRONLY
;
245 if (common
->has_logappend
&&
251 chr
->logfd
= qemu_create(common
->logfile
, flags
, 0666, errp
);
252 if (chr
->logfd
< 0) {
258 cc
->open(chr
, backend
, be_opened
, errp
);
262 static void char_init(Object
*obj
)
264 Chardev
*chr
= CHARDEV(obj
);
266 chr
->handover_yank_instance
= false;
268 qemu_mutex_init(&chr
->chr_write_lock
);
271 * Assume if chr_update_read_handler is implemented it will
272 * take the updated gcontext into account.
274 if (CHARDEV_GET_CLASS(chr
)->chr_update_read_handler
) {
275 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
);
280 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
285 static void char_class_init(ObjectClass
*oc
, void *data
)
287 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
289 cc
->chr_write
= null_chr_write
;
290 cc
->chr_be_event
= chr_be_event
;
293 static void char_finalize(Object
*obj
)
295 Chardev
*chr
= CHARDEV(obj
);
300 g_free(chr
->filename
);
302 if (chr
->logfd
!= -1) {
305 qemu_mutex_destroy(&chr
->chr_write_lock
);
308 static const TypeInfo char_type_info
= {
309 .name
= TYPE_CHARDEV
,
310 .parent
= TYPE_OBJECT
,
311 .instance_size
= sizeof(Chardev
),
312 .instance_init
= char_init
,
313 .instance_finalize
= char_finalize
,
315 .class_size
= sizeof(ChardevClass
),
316 .class_init
= char_class_init
,
319 static bool qemu_chr_is_busy(Chardev
*s
)
321 if (CHARDEV_IS_MUX(s
)) {
322 MuxChardev
*d
= MUX_CHARDEV(s
);
323 return d
->mux_cnt
>= 0;
325 return s
->be
!= NULL
;
329 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
331 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
333 if (cc
->chr_wait_connected
) {
334 return cc
->chr_wait_connected(chr
, errp
);
340 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
,
343 char host
[65], port
[33], width
[8], height
[8];
347 Error
*local_err
= NULL
;
349 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
351 error_report_err(local_err
);
355 if (strstart(filename
, "mon:", &p
)) {
356 if (!permit_mux_mon
) {
357 error_report("mon: isn't supported in this context");
361 qemu_opt_set(opts
, "mux", "on", &error_abort
);
362 if (strcmp(filename
, "stdio") == 0) {
363 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
364 * but pass it to the guest. Handle this only for compat syntax,
365 * for -chardev syntax we have special option for this.
366 * This is what -nographic did, redirecting+muxing serial+monitor
367 * to stdio causing Ctrl+C to be passed to guest. */
368 qemu_opt_set(opts
, "signal", "off", &error_abort
);
372 if (strcmp(filename
, "null") == 0 ||
373 strcmp(filename
, "pty") == 0 ||
374 strcmp(filename
, "msmouse") == 0 ||
375 strcmp(filename
, "wctablet") == 0 ||
376 strcmp(filename
, "braille") == 0 ||
377 strcmp(filename
, "testdev") == 0 ||
378 strcmp(filename
, "stdio") == 0) {
379 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
382 if (strstart(filename
, "vc", &p
)) {
383 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
385 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
387 qemu_opt_set(opts
, "width", width
, &error_abort
);
388 qemu_opt_set(opts
, "height", height
, &error_abort
);
389 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
391 qemu_opt_set(opts
, "cols", width
, &error_abort
);
392 qemu_opt_set(opts
, "rows", height
, &error_abort
);
399 if (strcmp(filename
, "con:") == 0) {
400 qemu_opt_set(opts
, "backend", "console", &error_abort
);
403 if (strstart(filename
, "COM", NULL
)) {
404 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
405 qemu_opt_set(opts
, "path", filename
, &error_abort
);
408 if (strstart(filename
, "file:", &p
)) {
409 qemu_opt_set(opts
, "backend", "file", &error_abort
);
410 qemu_opt_set(opts
, "path", p
, &error_abort
);
413 if (strstart(filename
, "pipe:", &p
)) {
414 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
415 qemu_opt_set(opts
, "path", p
, &error_abort
);
418 if (strstart(filename
, "tcp:", &p
) ||
419 strstart(filename
, "telnet:", &p
) ||
420 strstart(filename
, "tn3270:", &p
) ||
421 strstart(filename
, "websocket:", &p
)) {
422 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
424 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
427 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
428 qemu_opt_set(opts
, "host", host
, &error_abort
);
429 qemu_opt_set(opts
, "port", port
, &error_abort
);
431 if (!qemu_opts_do_parse(opts
, p
+ pos
+ 1, NULL
, &local_err
)) {
432 error_report_err(local_err
);
436 if (strstart(filename
, "telnet:", &p
)) {
437 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
438 } else if (strstart(filename
, "tn3270:", &p
)) {
439 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
440 } else if (strstart(filename
, "websocket:", &p
)) {
441 qemu_opt_set(opts
, "websocket", "on", &error_abort
);
445 if (strstart(filename
, "udp:", &p
)) {
446 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
447 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
449 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
453 qemu_opt_set(opts
, "host", host
, &error_abort
);
454 qemu_opt_set(opts
, "port", port
, &error_abort
);
457 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
459 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
463 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
464 qemu_opt_set(opts
, "localport", port
, &error_abort
);
468 if (strstart(filename
, "unix:", &p
)) {
469 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
470 if (!qemu_opts_do_parse(opts
, p
, "path", &local_err
)) {
471 error_report_err(local_err
);
476 if (strstart(filename
, "/dev/parport", NULL
) ||
477 strstart(filename
, "/dev/ppi", NULL
)) {
478 qemu_opt_set(opts
, "backend", "parallel", &error_abort
);
479 qemu_opt_set(opts
, "path", filename
, &error_abort
);
482 if (strstart(filename
, "/dev/", NULL
)) {
483 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
484 qemu_opt_set(opts
, "path", filename
, &error_abort
);
488 error_report("'%s' is not a valid char driver", filename
);
495 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
497 const char *logfile
= qemu_opt_get(opts
, "logfile");
499 backend
->has_logfile
= logfile
!= NULL
;
500 backend
->logfile
= g_strdup(logfile
);
502 backend
->has_logappend
= true;
503 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
506 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
509 const ChardevClass
*cc
;
510 char *typename
= g_strdup_printf("chardev-%s", driver
);
512 oc
= module_object_class_by_name(typename
);
515 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
516 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
520 if (object_class_is_abstract(oc
)) {
521 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
522 "an abstract device type");
526 cc
= CHARDEV_CLASS(oc
);
528 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
535 static struct ChardevAlias
{
536 const char *typename
;
538 bool deprecation_warning_printed
;
539 } chardev_alias_table
[] = {
540 #ifdef HAVE_CHARDEV_PARPORT
541 { "parallel", "parport" },
543 #ifdef HAVE_CHARDEV_SERIAL
548 typedef struct ChadevClassFE
{
549 void (*fn
)(const char *name
, void *opaque
);
554 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
556 ChadevClassFE
*fe
= opaque
;
558 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
559 if (CHARDEV_CLASS(klass
)->internal
) {
563 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
567 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
),
570 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
572 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
576 help_string_append(const char *name
, void *opaque
)
578 GString
*str
= opaque
;
580 g_string_append_printf(str
, "\n %s", name
);
583 static const char *chardev_alias_translate(const char *name
)
586 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
587 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
588 if (!chardev_alias_table
[i
].deprecation_warning_printed
) {
589 warn_report("The alias '%s' is deprecated, use '%s' instead",
590 name
, chardev_alias_table
[i
].typename
);
591 chardev_alias_table
[i
].deprecation_warning_printed
= true;
593 return chardev_alias_table
[i
].typename
;
599 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
, Error
**errp
)
601 Error
*local_err
= NULL
;
602 const ChardevClass
*cc
;
603 ChardevBackend
*backend
= NULL
;
604 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
607 error_setg(errp
, "chardev: \"%s\" missing backend",
612 cc
= char_get_class(name
, errp
);
617 backend
= g_new0(ChardevBackend
, 1);
618 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
621 cc
->parse(opts
, backend
, &local_err
);
623 error_propagate(errp
, local_err
);
624 qapi_free_ChardevBackend(backend
);
628 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
629 qemu_chr_parse_common(opts
, ccom
);
630 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
636 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
, GMainContext
*context
,
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 qemu_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
)),
677 backend
, context
, errp
);
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
, context
, errp
);
692 object_unparent(OBJECT(chr
));
700 qapi_free_ChardevBackend(backend
);
705 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
706 bool permit_mux_mon
, GMainContext
*context
)
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
, context
, &err
);
723 error_report_err(err
);
727 if (qemu_opt_get_bool(opts
, "mux", 0)) {
728 assert(permit_mux_mon
);
729 monitor_init_hmp(chr
, true, &err
);
731 error_report_err(err
);
732 object_unparent(OBJECT(chr
));
743 static Chardev
*qemu_chr_new_permit_mux_mon(const char *label
,
744 const char *filename
,
746 GMainContext
*context
)
749 chr
= qemu_chr_new_noreplay(label
, filename
, permit_mux_mon
, context
);
751 if (replay_mode
!= REPLAY_MODE_NONE
) {
752 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
754 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
755 error_report("Replay: ioctl is not supported "
756 "for serial devices yet");
758 replay_register_char_driver(chr
);
763 Chardev
*qemu_chr_new(const char *label
, const char *filename
,
764 GMainContext
*context
)
766 return qemu_chr_new_permit_mux_mon(label
, filename
, false, context
);
769 Chardev
*qemu_chr_new_mux_mon(const char *label
, const char *filename
,
770 GMainContext
*context
)
772 return qemu_chr_new_permit_mux_mon(label
, filename
, true, context
);
775 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
777 Chardev
*chr
= CHARDEV(obj
);
778 ChardevInfoList
**list
= data
;
779 ChardevInfo
*value
= g_malloc0(sizeof(*value
));
781 value
->label
= g_strdup(chr
->label
);
782 value
->filename
= g_strdup(chr
->filename
);
783 value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
785 QAPI_LIST_PREPEND(*list
, value
);
790 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
792 ChardevInfoList
*chr_list
= NULL
;
794 object_child_foreach(get_chardevs_root(),
795 qmp_query_chardev_foreach
, &chr_list
);
801 qmp_prepend_backend(const char *name
, void *opaque
)
803 ChardevBackendInfoList
**list
= opaque
;
804 ChardevBackendInfo
*value
;
806 value
= g_new0(ChardevBackendInfo
, 1);
807 value
->name
= g_strdup(name
);
808 QAPI_LIST_PREPEND(*list
, value
);
811 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
813 ChardevBackendInfoList
*backend_list
= NULL
;
815 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
820 Chardev
*qemu_chr_find(const char *name
)
822 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
824 return obj
? CHARDEV(obj
) : NULL
;
827 QemuOptsList qemu_chardev_opts
= {
829 .implied_opt_name
= "backend",
830 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
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_STRING
,
852 .type
= QEMU_OPT_STRING
,
855 .type
= QEMU_OPT_NUMBER
,
858 .type
= QEMU_OPT_BOOL
,
861 .type
= QEMU_OPT_BOOL
,
864 .type
= QEMU_OPT_BOOL
,
867 .type
= QEMU_OPT_BOOL
,
870 .type
= QEMU_OPT_BOOL
,
873 .type
= QEMU_OPT_BOOL
,
876 .type
= QEMU_OPT_NUMBER
,
879 .type
= QEMU_OPT_BOOL
,
882 .type
= QEMU_OPT_BOOL
,
885 .type
= QEMU_OPT_STRING
,
888 .type
= QEMU_OPT_STRING
,
891 .type
= QEMU_OPT_BOOL
,
894 .type
= QEMU_OPT_NUMBER
,
897 .type
= QEMU_OPT_NUMBER
,
900 .type
= QEMU_OPT_NUMBER
,
903 .type
= QEMU_OPT_NUMBER
,
906 .type
= QEMU_OPT_BOOL
,
909 .type
= QEMU_OPT_BOOL
,
912 .type
= QEMU_OPT_STRING
,
915 .type
= QEMU_OPT_NUMBER
,
918 .type
= QEMU_OPT_SIZE
,
921 .type
= QEMU_OPT_STRING
,
924 .type
= QEMU_OPT_BOOL
,
927 .type
= QEMU_OPT_STRING
,
930 .type
= QEMU_OPT_BOOL
,
933 .type
= QEMU_OPT_BOOL
,
936 .type
= QEMU_OPT_BOOL
,
940 .type
= QEMU_OPT_BOOL
,
941 .def_value_str
= "on",
944 .type
= QEMU_OPT_BOOL
,
947 { /* end of list */ }
951 bool qemu_chr_has_feature(Chardev
*chr
,
952 ChardevFeature feature
)
954 return test_bit(feature
, chr
->features
);
957 void qemu_chr_set_feature(Chardev
*chr
,
958 ChardevFeature feature
)
960 return set_bit(feature
, chr
->features
);
963 static Chardev
*chardev_new(const char *id
, const char *typename
,
964 ChardevBackend
*backend
,
965 GMainContext
*gcontext
,
966 bool handover_yank_instance
,
971 Error
*local_err
= NULL
;
972 bool be_opened
= true;
974 assert(g_str_has_prefix(typename
, "chardev-"));
977 obj
= object_new(typename
);
979 chr
->handover_yank_instance
= handover_yank_instance
;
980 chr
->label
= g_strdup(id
);
981 chr
->gcontext
= gcontext
;
983 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
985 error_propagate(errp
, local_err
);
990 if (!chr
->filename
) {
991 chr
->filename
= g_strdup(typename
+ 8);
994 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1000 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
1001 ChardevBackend
*backend
,
1002 GMainContext
*gcontext
,
1006 g_autofree
char *genid
= NULL
;
1009 genid
= id_generate(ID_CHR
);
1013 chr
= chardev_new(id
, typename
, backend
, gcontext
, false, errp
);
1018 if (!object_property_try_add_child(get_chardevs_root(), id
, OBJECT(chr
),
1020 object_unref(OBJECT(chr
));
1023 object_unref(OBJECT(chr
));
1028 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
1032 const ChardevClass
*cc
;
1034 g_autoptr(Chardev
) chr
= NULL
;
1036 if (qemu_chr_find(id
)) {
1037 error_setg(errp
, "Chardev with id '%s' already exists", id
);
1041 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1046 chr
= chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
1047 backend
, NULL
, false, errp
);
1052 if (!object_property_try_add_child(get_chardevs_root(), id
, OBJECT(chr
),
1057 ret
= g_new0(ChardevReturn
, 1);
1058 if (CHARDEV_IS_PTY(chr
)) {
1059 ret
->pty
= g_strdup(chr
->filename
+ 4);
1060 ret
->has_pty
= true;
1066 error_prepend(errp
, "Failed to add chardev '%s': ", id
);
1070 ChardevReturn
*qmp_chardev_change(const char *id
, ChardevBackend
*backend
,
1074 const ChardevClass
*cc
, *cc_new
;
1075 Chardev
*chr
, *chr_new
;
1076 bool closed_sent
= false;
1077 bool handover_yank_instance
;
1080 chr
= qemu_chr_find(id
);
1082 error_setg(errp
, "Chardev '%s' does not exist", id
);
1086 if (CHARDEV_IS_MUX(chr
)) {
1087 error_setg(errp
, "Mux device hotswap not supported yet");
1091 if (qemu_chr_replay(chr
)) {
1093 "Chardev '%s' cannot be changed in record/replay mode", id
);
1100 object_unparent(OBJECT(chr
));
1101 return qmp_chardev_add(id
, backend
, errp
);
1104 if (!be
->chr_be_change
) {
1105 error_setg(errp
, "Chardev user does not support chardev hotswap");
1109 cc
= CHARDEV_GET_CLASS(chr
);
1110 cc_new
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1116 * The new chardev should not register a yank instance if the current
1117 * chardev has registered one already.
1119 handover_yank_instance
= cc
->supports_yank
&& cc_new
->supports_yank
;
1121 chr_new
= chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc_new
)),
1122 backend
, chr
->gcontext
, handover_yank_instance
, errp
);
1127 if (chr
->be_open
&& !chr_new
->be_open
) {
1128 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1133 qemu_chr_fe_init(be
, chr_new
, &error_abort
);
1135 if (be
->chr_be_change(be
->opaque
) < 0) {
1136 error_setg(errp
, "Chardev '%s' change failed", chr_new
->label
);
1138 qemu_chr_fe_init(be
, chr
, &error_abort
);
1140 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1142 object_unref(OBJECT(chr_new
));
1146 /* change successfull, clean up */
1147 chr_new
->handover_yank_instance
= false;
1150 * When the old chardev is freed, it should not unregister the yank
1151 * instance if the new chardev needs it.
1153 chr
->handover_yank_instance
= handover_yank_instance
;
1155 object_unparent(OBJECT(chr
));
1156 object_property_add_child(get_chardevs_root(), chr_new
->label
,
1158 object_unref(OBJECT(chr_new
));
1160 ret
= g_new0(ChardevReturn
, 1);
1161 if (CHARDEV_IS_PTY(chr_new
)) {
1162 ret
->pty
= g_strdup(chr_new
->filename
+ 4);
1163 ret
->has_pty
= true;
1169 void qmp_chardev_remove(const char *id
, Error
**errp
)
1173 chr
= qemu_chr_find(id
);
1175 error_setg(errp
, "Chardev '%s' not found", id
);
1178 if (qemu_chr_is_busy(chr
)) {
1179 error_setg(errp
, "Chardev '%s' is busy", id
);
1182 if (qemu_chr_replay(chr
)) {
1184 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1187 object_unparent(OBJECT(chr
));
1190 void qmp_chardev_send_break(const char *id
, Error
**errp
)
1194 chr
= qemu_chr_find(id
);
1196 error_setg(errp
, "Chardev '%s' not found", id
);
1199 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
1203 * Add a timeout callback for the chardev (in milliseconds), return
1204 * the GSource object created. Please use this to add timeout hook for
1205 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1206 * make sure the gcontext that the task bound to is correct.
1208 GSource
*qemu_chr_timeout_add_ms(Chardev
*chr
, guint ms
,
1209 GSourceFunc func
, void *private)
1211 GSource
*source
= g_timeout_source_new(ms
);
1214 g_source_set_callback(source
, func
, private, NULL
);
1215 g_source_attach(source
, chr
->gcontext
);
1220 void qemu_chr_cleanup(void)
1222 object_unparent(get_chardevs_root());
1225 static void register_types(void)
1227 type_register_static(&char_type_info
);
1230 type_init(register_types
);