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"
41 #include "qemu/coroutine.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
, 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
, 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
| O_CREAT
;
245 if (common
->has_logappend
&&
251 chr
->logfd
= qemu_open_old(common
->logfile
, flags
, 0666);
252 if (chr
->logfd
< 0) {
253 error_setg_errno(errp
, errno
,
254 "Unable to open logfile %s",
261 cc
->open(chr
, backend
, be_opened
, errp
);
265 static void char_init(Object
*obj
)
267 Chardev
*chr
= CHARDEV(obj
);
270 qemu_mutex_init(&chr
->chr_write_lock
);
273 * Assume if chr_update_read_handler is implemented it will
274 * take the updated gcontext into account.
276 if (CHARDEV_GET_CLASS(chr
)->chr_update_read_handler
) {
277 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
);
282 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
287 static void char_class_init(ObjectClass
*oc
, void *data
)
289 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
291 cc
->chr_write
= null_chr_write
;
292 cc
->chr_be_event
= chr_be_event
;
295 static void char_finalize(Object
*obj
)
297 Chardev
*chr
= CHARDEV(obj
);
302 g_free(chr
->filename
);
304 if (chr
->logfd
!= -1) {
307 qemu_mutex_destroy(&chr
->chr_write_lock
);
310 static const TypeInfo char_type_info
= {
311 .name
= TYPE_CHARDEV
,
312 .parent
= TYPE_OBJECT
,
313 .instance_size
= sizeof(Chardev
),
314 .instance_init
= char_init
,
315 .instance_finalize
= char_finalize
,
317 .class_size
= sizeof(ChardevClass
),
318 .class_init
= char_class_init
,
321 static bool qemu_chr_is_busy(Chardev
*s
)
323 if (CHARDEV_IS_MUX(s
)) {
324 MuxChardev
*d
= MUX_CHARDEV(s
);
325 return d
->mux_cnt
>= 0;
327 return s
->be
!= NULL
;
331 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
333 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
335 if (cc
->chr_wait_connected
) {
336 return cc
->chr_wait_connected(chr
, errp
);
342 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
,
345 char host
[65], port
[33], width
[8], height
[8];
349 Error
*local_err
= NULL
;
351 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
353 error_report_err(local_err
);
357 if (strstart(filename
, "mon:", &p
)) {
358 if (!permit_mux_mon
) {
359 error_report("mon: isn't supported in this context");
363 qemu_opt_set(opts
, "mux", "on", &error_abort
);
364 if (strcmp(filename
, "stdio") == 0) {
365 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
366 * but pass it to the guest. Handle this only for compat syntax,
367 * for -chardev syntax we have special option for this.
368 * This is what -nographic did, redirecting+muxing serial+monitor
369 * to stdio causing Ctrl+C to be passed to guest. */
370 qemu_opt_set(opts
, "signal", "off", &error_abort
);
374 if (strcmp(filename
, "null") == 0 ||
375 strcmp(filename
, "pty") == 0 ||
376 strcmp(filename
, "msmouse") == 0 ||
377 strcmp(filename
, "wctablet") == 0 ||
378 strcmp(filename
, "braille") == 0 ||
379 strcmp(filename
, "testdev") == 0 ||
380 strcmp(filename
, "stdio") == 0) {
381 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
384 if (strstart(filename
, "vc", &p
)) {
385 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
387 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
389 qemu_opt_set(opts
, "width", width
, &error_abort
);
390 qemu_opt_set(opts
, "height", height
, &error_abort
);
391 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
393 qemu_opt_set(opts
, "cols", width
, &error_abort
);
394 qemu_opt_set(opts
, "rows", height
, &error_abort
);
401 if (strcmp(filename
, "con:") == 0) {
402 qemu_opt_set(opts
, "backend", "console", &error_abort
);
405 if (strstart(filename
, "COM", NULL
)) {
406 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
407 qemu_opt_set(opts
, "path", filename
, &error_abort
);
410 if (strstart(filename
, "file:", &p
)) {
411 qemu_opt_set(opts
, "backend", "file", &error_abort
);
412 qemu_opt_set(opts
, "path", p
, &error_abort
);
415 if (strstart(filename
, "pipe:", &p
)) {
416 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
417 qemu_opt_set(opts
, "path", p
, &error_abort
);
420 if (strstart(filename
, "tcp:", &p
) ||
421 strstart(filename
, "telnet:", &p
) ||
422 strstart(filename
, "tn3270:", &p
) ||
423 strstart(filename
, "websocket:", &p
)) {
424 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
426 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
429 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
430 qemu_opt_set(opts
, "host", host
, &error_abort
);
431 qemu_opt_set(opts
, "port", port
, &error_abort
);
433 if (!qemu_opts_do_parse(opts
, p
+ pos
+ 1, NULL
, &local_err
)) {
434 error_report_err(local_err
);
438 if (strstart(filename
, "telnet:", &p
)) {
439 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
440 } else if (strstart(filename
, "tn3270:", &p
)) {
441 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
442 } else if (strstart(filename
, "websocket:", &p
)) {
443 qemu_opt_set(opts
, "websocket", "on", &error_abort
);
447 if (strstart(filename
, "udp:", &p
)) {
448 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
449 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
451 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
455 qemu_opt_set(opts
, "host", host
, &error_abort
);
456 qemu_opt_set(opts
, "port", port
, &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
, "localaddr", host
, &error_abort
);
466 qemu_opt_set(opts
, "localport", port
, &error_abort
);
470 if (strstart(filename
, "unix:", &p
)) {
471 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
472 if (!qemu_opts_do_parse(opts
, p
, "path", &local_err
)) {
473 error_report_err(local_err
);
478 if (strstart(filename
, "/dev/parport", NULL
) ||
479 strstart(filename
, "/dev/ppi", NULL
)) {
480 qemu_opt_set(opts
, "backend", "parallel", &error_abort
);
481 qemu_opt_set(opts
, "path", filename
, &error_abort
);
484 if (strstart(filename
, "/dev/", NULL
)) {
485 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
486 qemu_opt_set(opts
, "path", filename
, &error_abort
);
490 error_report("'%s' is not a valid char driver", filename
);
497 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
499 const char *logfile
= qemu_opt_get(opts
, "logfile");
501 backend
->has_logfile
= logfile
!= NULL
;
502 backend
->logfile
= g_strdup(logfile
);
504 backend
->has_logappend
= true;
505 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
508 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
511 const ChardevClass
*cc
;
512 char *typename
= g_strdup_printf("chardev-%s", driver
);
514 oc
= module_object_class_by_name(typename
);
517 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
518 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
522 if (object_class_is_abstract(oc
)) {
523 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
524 "an abstract device type");
528 cc
= CHARDEV_CLASS(oc
);
530 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
537 static struct ChardevAlias
{
538 const char *typename
;
540 bool deprecation_warning_printed
;
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
, bool is_cli_alias
, 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, false, fe
->opaque
);
569 chardev_name_foreach(void (*fn
)(const char *name
, bool is_cli_alias
,
573 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
576 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
578 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
579 fn(chardev_alias_table
[i
].alias
, true, opaque
);
584 help_string_append(const char *name
, bool is_cli_alias
, void *opaque
)
586 GString
*str
= opaque
;
592 g_string_append_printf(str
, "\n %s", name
);
595 static const char *chardev_alias_translate(const char *name
)
598 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
599 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
600 if (!chardev_alias_table
[i
].deprecation_warning_printed
) {
601 warn_report("The alias '%s' is deprecated, use '%s' instead",
602 name
, chardev_alias_table
[i
].typename
);
603 chardev_alias_table
[i
].deprecation_warning_printed
= true;
605 return chardev_alias_table
[i
].typename
;
611 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
, Error
**errp
)
613 Error
*local_err
= NULL
;
614 const ChardevClass
*cc
;
615 ChardevBackend
*backend
= NULL
;
616 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
619 error_setg(errp
, "chardev: \"%s\" missing backend",
624 cc
= char_get_class(name
, errp
);
629 backend
= g_new0(ChardevBackend
, 1);
630 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
633 cc
->parse(opts
, backend
, &local_err
);
635 error_propagate(errp
, local_err
);
636 qapi_free_ChardevBackend(backend
);
640 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
641 qemu_chr_parse_common(opts
, ccom
);
642 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
648 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
, GMainContext
*context
,
651 const ChardevClass
*cc
;
653 ChardevBackend
*backend
= NULL
;
654 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
655 const char *id
= qemu_opts_id(opts
);
658 if (name
&& is_help_option(name
)) {
659 GString
*str
= g_string_new("");
661 chardev_name_foreach(help_string_append
, str
);
663 qemu_printf("Available chardev backend types: %s\n", str
->str
);
664 g_string_free(str
, true);
669 error_setg(errp
, "chardev: no id specified");
673 backend
= qemu_chr_parse_opts(opts
, errp
);
674 if (backend
== NULL
) {
678 cc
= char_get_class(name
, errp
);
683 if (qemu_opt_get_bool(opts
, "mux", 0)) {
684 bid
= g_strdup_printf("%s-base", id
);
687 chr
= qemu_chardev_new(bid
? bid
: id
,
688 object_class_get_name(OBJECT_CLASS(cc
)),
689 backend
, context
, errp
);
697 qapi_free_ChardevBackend(backend
);
698 backend
= g_new0(ChardevBackend
, 1);
699 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
700 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
701 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
702 mux
= qemu_chardev_new(id
, TYPE_CHARDEV_MUX
, backend
, context
, errp
);
704 object_unparent(OBJECT(chr
));
712 qapi_free_ChardevBackend(backend
);
717 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
718 bool permit_mux_mon
, GMainContext
*context
)
725 if (strstart(filename
, "chardev:", &p
)) {
726 return qemu_chr_find(p
);
729 opts
= qemu_chr_parse_compat(label
, filename
, permit_mux_mon
);
733 chr
= qemu_chr_new_from_opts(opts
, context
, &err
);
735 error_report_err(err
);
739 if (qemu_opt_get_bool(opts
, "mux", 0)) {
740 assert(permit_mux_mon
);
741 monitor_init_hmp(chr
, true, &err
);
743 error_report_err(err
);
744 object_unparent(OBJECT(chr
));
755 static Chardev
*qemu_chr_new_permit_mux_mon(const char *label
,
756 const char *filename
,
758 GMainContext
*context
)
761 chr
= qemu_chr_new_noreplay(label
, filename
, permit_mux_mon
, context
);
763 if (replay_mode
!= REPLAY_MODE_NONE
) {
764 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
766 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
767 error_report("Replay: ioctl is not supported "
768 "for serial devices yet");
770 replay_register_char_driver(chr
);
775 Chardev
*qemu_chr_new(const char *label
, const char *filename
,
776 GMainContext
*context
)
778 return qemu_chr_new_permit_mux_mon(label
, filename
, false, context
);
781 Chardev
*qemu_chr_new_mux_mon(const char *label
, const char *filename
,
782 GMainContext
*context
)
784 return qemu_chr_new_permit_mux_mon(label
, filename
, true, context
);
787 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
789 Chardev
*chr
= CHARDEV(obj
);
790 ChardevInfoList
**list
= data
;
791 ChardevInfo
*value
= g_malloc0(sizeof(*value
));
793 value
->label
= g_strdup(chr
->label
);
794 value
->filename
= g_strdup(chr
->filename
);
795 value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
797 QAPI_LIST_PREPEND(*list
, value
);
802 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
804 ChardevInfoList
*chr_list
= NULL
;
806 object_child_foreach(get_chardevs_root(),
807 qmp_query_chardev_foreach
, &chr_list
);
813 qmp_prepend_backend(const char *name
, bool is_cli_alias
, void *opaque
)
815 ChardevBackendInfoList
**list
= opaque
;
816 ChardevBackendInfo
*value
;
822 value
= g_new0(ChardevBackendInfo
, 1);
823 value
->name
= g_strdup(name
);
824 QAPI_LIST_PREPEND(*list
, value
);
827 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
829 ChardevBackendInfoList
*backend_list
= NULL
;
831 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
836 Chardev
*qemu_chr_find(const char *name
)
838 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
840 return obj
? CHARDEV(obj
) : NULL
;
843 QemuOptsList qemu_chardev_opts
= {
845 .implied_opt_name
= "backend",
846 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
850 .type
= QEMU_OPT_STRING
,
853 .type
= QEMU_OPT_STRING
,
856 .type
= QEMU_OPT_STRING
,
859 .type
= QEMU_OPT_STRING
,
862 .type
= QEMU_OPT_STRING
,
865 .type
= QEMU_OPT_STRING
,
868 .type
= QEMU_OPT_STRING
,
871 .type
= QEMU_OPT_NUMBER
,
874 .type
= QEMU_OPT_BOOL
,
877 .type
= QEMU_OPT_BOOL
,
880 .type
= QEMU_OPT_BOOL
,
883 .type
= QEMU_OPT_BOOL
,
886 .type
= QEMU_OPT_BOOL
,
889 .type
= QEMU_OPT_BOOL
,
892 .type
= QEMU_OPT_NUMBER
,
895 .type
= QEMU_OPT_BOOL
,
898 .type
= QEMU_OPT_BOOL
,
901 .type
= QEMU_OPT_STRING
,
904 .type
= QEMU_OPT_STRING
,
907 .type
= QEMU_OPT_BOOL
,
910 .type
= QEMU_OPT_NUMBER
,
913 .type
= QEMU_OPT_NUMBER
,
916 .type
= QEMU_OPT_NUMBER
,
919 .type
= QEMU_OPT_NUMBER
,
922 .type
= QEMU_OPT_BOOL
,
925 .type
= QEMU_OPT_BOOL
,
928 .type
= QEMU_OPT_STRING
,
931 .type
= QEMU_OPT_NUMBER
,
934 .type
= QEMU_OPT_SIZE
,
937 .type
= QEMU_OPT_STRING
,
940 .type
= QEMU_OPT_BOOL
,
943 .type
= QEMU_OPT_STRING
,
946 .type
= QEMU_OPT_BOOL
,
950 .type
= QEMU_OPT_BOOL
,
951 .def_value_str
= "on",
954 .type
= QEMU_OPT_BOOL
,
957 { /* end of list */ }
961 bool qemu_chr_has_feature(Chardev
*chr
,
962 ChardevFeature feature
)
964 return test_bit(feature
, chr
->features
);
967 void qemu_chr_set_feature(Chardev
*chr
,
968 ChardevFeature feature
)
970 return set_bit(feature
, chr
->features
);
973 static Chardev
*chardev_new(const char *id
, const char *typename
,
974 ChardevBackend
*backend
,
975 GMainContext
*gcontext
,
980 Error
*local_err
= NULL
;
981 bool be_opened
= true;
983 assert(g_str_has_prefix(typename
, "chardev-"));
985 obj
= object_new(typename
);
987 chr
->label
= g_strdup(id
);
988 chr
->gcontext
= gcontext
;
990 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
995 if (!chr
->filename
) {
996 chr
->filename
= g_strdup(typename
+ 8);
999 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1003 object_property_try_add_child(get_chardevs_root(), id
, obj
,
1013 error_propagate(errp
, local_err
);
1021 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
1022 ChardevBackend
*backend
,
1023 GMainContext
*gcontext
,
1026 g_autofree
char *genid
= NULL
;
1029 genid
= id_generate(ID_CHR
);
1033 return chardev_new(id
, typename
, backend
, gcontext
, errp
);
1036 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
1039 const ChardevClass
*cc
;
1043 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1048 chr
= chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
1049 backend
, NULL
, errp
);
1054 ret
= g_new0(ChardevReturn
, 1);
1055 if (CHARDEV_IS_PTY(chr
)) {
1056 ret
->pty
= g_strdup(chr
->filename
+ 4);
1057 ret
->has_pty
= true;
1063 ChardevReturn
*qmp_chardev_change(const char *id
, ChardevBackend
*backend
,
1067 const ChardevClass
*cc
;
1068 Chardev
*chr
, *chr_new
;
1069 bool closed_sent
= false;
1072 chr
= qemu_chr_find(id
);
1074 error_setg(errp
, "Chardev '%s' does not exist", id
);
1078 if (CHARDEV_IS_MUX(chr
)) {
1079 error_setg(errp
, "Mux device hotswap not supported yet");
1083 if (qemu_chr_replay(chr
)) {
1085 "Chardev '%s' cannot be changed in record/replay mode", id
);
1092 object_unparent(OBJECT(chr
));
1093 return qmp_chardev_add(id
, backend
, errp
);
1096 if (!be
->chr_be_change
) {
1097 error_setg(errp
, "Chardev user does not support chardev hotswap");
1101 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1106 chr_new
= chardev_new(NULL
, object_class_get_name(OBJECT_CLASS(cc
)),
1107 backend
, chr
->gcontext
, errp
);
1111 chr_new
->label
= g_strdup(id
);
1113 if (chr
->be_open
&& !chr_new
->be_open
) {
1114 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1119 qemu_chr_fe_init(be
, chr_new
, &error_abort
);
1121 if (be
->chr_be_change(be
->opaque
) < 0) {
1122 error_setg(errp
, "Chardev '%s' change failed", chr_new
->label
);
1124 qemu_chr_fe_init(be
, chr
, &error_abort
);
1126 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1128 object_unref(OBJECT(chr_new
));
1132 object_unparent(OBJECT(chr
));
1133 object_property_add_child(get_chardevs_root(), chr_new
->label
,
1135 object_unref(OBJECT(chr_new
));
1137 ret
= g_new0(ChardevReturn
, 1);
1138 if (CHARDEV_IS_PTY(chr_new
)) {
1139 ret
->pty
= g_strdup(chr_new
->filename
+ 4);
1140 ret
->has_pty
= true;
1146 void qmp_chardev_remove(const char *id
, Error
**errp
)
1150 chr
= qemu_chr_find(id
);
1152 error_setg(errp
, "Chardev '%s' not found", id
);
1155 if (qemu_chr_is_busy(chr
)) {
1156 error_setg(errp
, "Chardev '%s' is busy", id
);
1159 if (qemu_chr_replay(chr
)) {
1161 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1164 object_unparent(OBJECT(chr
));
1167 void qmp_chardev_send_break(const char *id
, Error
**errp
)
1171 chr
= qemu_chr_find(id
);
1173 error_setg(errp
, "Chardev '%s' not found", id
);
1176 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
1180 * Add a timeout callback for the chardev (in milliseconds), return
1181 * the GSource object created. Please use this to add timeout hook for
1182 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1183 * make sure the gcontext that the task bound to is correct.
1185 GSource
*qemu_chr_timeout_add_ms(Chardev
*chr
, guint ms
,
1186 GSourceFunc func
, void *private)
1188 GSource
*source
= g_timeout_source_new(ms
);
1191 g_source_set_callback(source
, func
, private, NULL
);
1192 g_source_attach(source
, chr
->gcontext
);
1197 void qemu_chr_cleanup(void)
1199 object_unparent(get_chardevs_root());
1202 static void register_types(void)
1204 type_register_static(&char_type_info
);
1207 type_init(register_types
);