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 "chardev/char-mux.h"
43 /***********************************************************/
44 /* character device */
46 static Object
*get_chardevs_root(void)
48 return container_get(object_get_root(), "/chardevs");
51 static void chr_be_event(Chardev
*s
, int event
)
53 CharBackend
*be
= s
->be
;
55 if (!be
|| !be
->chr_event
) {
59 be
->chr_event(be
->opaque
, event
);
62 void qemu_chr_be_event(Chardev
*s
, int event
)
64 /* Keep track if the char device is open */
66 case CHR_EVENT_OPENED
:
69 case CHR_EVENT_CLOSED
:
74 CHARDEV_GET_CLASS(s
)->chr_be_event(s
, event
);
77 /* Not reporting errors from writing to logfile, as logs are
78 * defined to be "best effort" only */
79 static void qemu_chr_write_log(Chardev
*s
, const uint8_t *buf
, size_t len
)
90 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
91 if (ret
== -1 && errno
== EAGAIN
) {
103 static int qemu_chr_write_buffer(Chardev
*s
,
104 const uint8_t *buf
, int len
,
105 int *offset
, bool write_all
)
107 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
111 qemu_mutex_lock(&s
->chr_write_lock
);
112 while (*offset
< len
) {
114 res
= cc
->chr_write(s
, buf
+ *offset
, len
- *offset
);
115 if (res
< 0 && errno
== EAGAIN
&& write_all
) {
130 qemu_chr_write_log(s
, buf
, *offset
);
132 qemu_mutex_unlock(&s
->chr_write_lock
);
137 int qemu_chr_write(Chardev
*s
, const uint8_t *buf
, int len
, bool write_all
)
142 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
143 replay_char_write_event_load(&res
, &offset
);
144 assert(offset
<= len
);
145 qemu_chr_write_buffer(s
, buf
, offset
, &offset
, true);
149 res
= qemu_chr_write_buffer(s
, buf
, len
, &offset
, write_all
);
151 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
152 replay_char_write_event_save(res
, offset
);
161 int qemu_chr_be_can_write(Chardev
*s
)
163 CharBackend
*be
= s
->be
;
165 if (!be
|| !be
->chr_can_read
) {
169 return be
->chr_can_read(be
->opaque
);
172 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
)
174 CharBackend
*be
= s
->be
;
176 if (be
&& be
->chr_read
) {
177 be
->chr_read(be
->opaque
, buf
, len
);
181 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
)
183 if (qemu_chr_replay(s
)) {
184 if (replay_mode
== REPLAY_MODE_PLAY
) {
187 replay_chr_be_write(s
, buf
, len
);
189 qemu_chr_be_write_impl(s
, buf
, len
);
193 void qemu_chr_be_update_read_handlers(Chardev
*s
,
194 GMainContext
*context
)
196 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
198 assert(qemu_chr_has_feature(s
, QEMU_CHAR_FEATURE_GCONTEXT
)
200 s
->gcontext
= context
;
201 if (cc
->chr_update_read_handler
) {
202 cc
->chr_update_read_handler(s
);
206 int qemu_chr_add_client(Chardev
*s
, int fd
)
208 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
209 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
212 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
213 bool *be_opened
, Error
**errp
)
215 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
216 /* Any ChardevCommon member would work */
217 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
219 if (common
&& common
->has_logfile
) {
220 int flags
= O_WRONLY
| O_CREAT
;
221 if (common
->has_logappend
&&
227 chr
->logfd
= qemu_open(common
->logfile
, flags
, 0666);
228 if (chr
->logfd
< 0) {
229 error_setg_errno(errp
, errno
,
230 "Unable to open logfile %s",
237 cc
->open(chr
, backend
, be_opened
, errp
);
241 static void char_init(Object
*obj
)
243 Chardev
*chr
= CHARDEV(obj
);
246 qemu_mutex_init(&chr
->chr_write_lock
);
249 * Assume if chr_update_read_handler is implemented it will
250 * take the updated gcontext into account.
252 if (CHARDEV_GET_CLASS(chr
)->chr_update_read_handler
) {
253 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
);
258 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
263 static void char_class_init(ObjectClass
*oc
, void *data
)
265 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
267 cc
->chr_write
= null_chr_write
;
268 cc
->chr_be_event
= chr_be_event
;
271 static void char_finalize(Object
*obj
)
273 Chardev
*chr
= CHARDEV(obj
);
278 g_free(chr
->filename
);
280 if (chr
->logfd
!= -1) {
283 qemu_mutex_destroy(&chr
->chr_write_lock
);
286 static const TypeInfo char_type_info
= {
287 .name
= TYPE_CHARDEV
,
288 .parent
= TYPE_OBJECT
,
289 .instance_size
= sizeof(Chardev
),
290 .instance_init
= char_init
,
291 .instance_finalize
= char_finalize
,
293 .class_size
= sizeof(ChardevClass
),
294 .class_init
= char_class_init
,
297 static int chardev_machine_done_notify_one(Object
*child
, void *opaque
)
299 Chardev
*chr
= (Chardev
*)child
;
300 ChardevClass
*class = CHARDEV_GET_CLASS(chr
);
302 if (class->chr_machine_done
) {
303 return class->chr_machine_done(chr
);
309 static void chardev_machine_done_hook(Notifier
*notifier
, void *unused
)
311 int ret
= object_child_foreach(get_chardevs_root(),
312 chardev_machine_done_notify_one
, NULL
);
315 error_report("Failed to call chardev machine_done hooks");
320 static Notifier chardev_machine_done_notify
= {
321 .notify
= chardev_machine_done_hook
,
324 static bool qemu_chr_is_busy(Chardev
*s
)
326 if (CHARDEV_IS_MUX(s
)) {
327 MuxChardev
*d
= MUX_CHARDEV(s
);
328 return d
->mux_cnt
>= 0;
330 return s
->be
!= NULL
;
334 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
336 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
338 if (cc
->chr_wait_connected
) {
339 return cc
->chr_wait_connected(chr
, errp
);
345 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
,
348 char host
[65], port
[33], width
[8], height
[8];
352 Error
*local_err
= NULL
;
354 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
356 error_report_err(local_err
);
360 if (strstart(filename
, "mon:", &p
)) {
361 if (!permit_mux_mon
) {
362 error_report("mon: isn't supported in this context");
366 qemu_opt_set(opts
, "mux", "on", &error_abort
);
367 if (strcmp(filename
, "stdio") == 0) {
368 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
369 * but pass it to the guest. Handle this only for compat syntax,
370 * for -chardev syntax we have special option for this.
371 * This is what -nographic did, redirecting+muxing serial+monitor
372 * to stdio causing Ctrl+C to be passed to guest. */
373 qemu_opt_set(opts
, "signal", "off", &error_abort
);
377 if (strcmp(filename
, "null") == 0 ||
378 strcmp(filename
, "pty") == 0 ||
379 strcmp(filename
, "msmouse") == 0 ||
380 strcmp(filename
, "wctablet") == 0 ||
381 strcmp(filename
, "braille") == 0 ||
382 strcmp(filename
, "testdev") == 0 ||
383 strcmp(filename
, "stdio") == 0) {
384 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
387 if (strstart(filename
, "vc", &p
)) {
388 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
390 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
392 qemu_opt_set(opts
, "width", width
, &error_abort
);
393 qemu_opt_set(opts
, "height", height
, &error_abort
);
394 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
396 qemu_opt_set(opts
, "cols", width
, &error_abort
);
397 qemu_opt_set(opts
, "rows", height
, &error_abort
);
404 if (strcmp(filename
, "con:") == 0) {
405 qemu_opt_set(opts
, "backend", "console", &error_abort
);
408 if (strstart(filename
, "COM", NULL
)) {
409 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
410 qemu_opt_set(opts
, "path", filename
, &error_abort
);
413 if (strstart(filename
, "file:", &p
)) {
414 qemu_opt_set(opts
, "backend", "file", &error_abort
);
415 qemu_opt_set(opts
, "path", p
, &error_abort
);
418 if (strstart(filename
, "pipe:", &p
)) {
419 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
420 qemu_opt_set(opts
, "path", p
, &error_abort
);
423 if (strstart(filename
, "tcp:", &p
) ||
424 strstart(filename
, "telnet:", &p
) ||
425 strstart(filename
, "tn3270:", &p
) ||
426 strstart(filename
, "websocket:", &p
)) {
427 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
429 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
432 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
433 qemu_opt_set(opts
, "host", host
, &error_abort
);
434 qemu_opt_set(opts
, "port", port
, &error_abort
);
436 qemu_opts_do_parse(opts
, p
+pos
+1, NULL
, &local_err
);
438 error_report_err(local_err
);
442 if (strstart(filename
, "telnet:", &p
)) {
443 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
444 } else if (strstart(filename
, "tn3270:", &p
)) {
445 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
446 } else if (strstart(filename
, "websocket:", &p
)) {
447 qemu_opt_set(opts
, "websocket", "on", &error_abort
);
451 if (strstart(filename
, "udp:", &p
)) {
452 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
453 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
455 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
459 qemu_opt_set(opts
, "host", host
, &error_abort
);
460 qemu_opt_set(opts
, "port", port
, &error_abort
);
463 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
465 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
469 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
470 qemu_opt_set(opts
, "localport", port
, &error_abort
);
474 if (strstart(filename
, "unix:", &p
)) {
475 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
476 qemu_opts_do_parse(opts
, p
, "path", &local_err
);
478 error_report_err(local_err
);
483 if (strstart(filename
, "/dev/parport", NULL
) ||
484 strstart(filename
, "/dev/ppi", NULL
)) {
485 qemu_opt_set(opts
, "backend", "parallel", &error_abort
);
486 qemu_opt_set(opts
, "path", filename
, &error_abort
);
489 if (strstart(filename
, "/dev/", NULL
)) {
490 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
491 qemu_opt_set(opts
, "path", filename
, &error_abort
);
495 error_report("'%s' is not a valid char driver", filename
);
502 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
)
504 const char *logfile
= qemu_opt_get(opts
, "logfile");
506 backend
->has_logfile
= logfile
!= NULL
;
507 backend
->logfile
= g_strdup(logfile
);
509 backend
->has_logappend
= true;
510 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
513 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
516 const ChardevClass
*cc
;
517 char *typename
= g_strdup_printf("chardev-%s", driver
);
519 oc
= object_class_by_name(typename
);
522 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
523 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
527 if (object_class_is_abstract(oc
)) {
528 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
529 "abstract device type");
533 cc
= CHARDEV_CLASS(oc
);
535 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
542 static const struct ChardevAlias
{
543 const char *typename
;
545 } chardev_alias_table
[] = {
546 #ifdef HAVE_CHARDEV_PARPORT
547 { "parallel", "parport" },
549 #ifdef HAVE_CHARDEV_SERIAL
554 typedef struct ChadevClassFE
{
555 void (*fn
)(const char *name
, void *opaque
);
560 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
562 ChadevClassFE
*fe
= opaque
;
564 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
565 if (CHARDEV_CLASS(klass
)->internal
) {
569 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
573 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
), void *opaque
)
575 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
578 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
580 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
581 fn(chardev_alias_table
[i
].alias
, opaque
);
586 help_string_append(const char *name
, void *opaque
)
588 GString
*str
= opaque
;
590 g_string_append_printf(str
, "\n %s", name
);
593 static const char *chardev_alias_translate(const char *name
)
596 for (i
= 0; i
< (int)ARRAY_SIZE(chardev_alias_table
); i
++) {
597 if (g_strcmp0(chardev_alias_table
[i
].alias
, name
) == 0) {
598 return chardev_alias_table
[i
].typename
;
604 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
, Error
**errp
)
606 Error
*local_err
= NULL
;
607 const ChardevClass
*cc
;
608 ChardevBackend
*backend
= NULL
;
609 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
612 error_setg(errp
, "chardev: \"%s\" missing backend",
617 cc
= char_get_class(name
, errp
);
622 backend
= g_new0(ChardevBackend
, 1);
623 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
626 cc
->parse(opts
, backend
, &local_err
);
628 error_propagate(errp
, local_err
);
629 qapi_free_ChardevBackend(backend
);
633 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
634 qemu_chr_parse_common(opts
, ccom
);
635 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
641 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
, GMainContext
*context
,
644 const ChardevClass
*cc
;
646 ChardevBackend
*backend
= NULL
;
647 const char *name
= chardev_alias_translate(qemu_opt_get(opts
, "backend"));
648 const char *id
= qemu_opts_id(opts
);
651 if (name
&& is_help_option(name
)) {
652 GString
*str
= g_string_new("");
654 chardev_name_foreach(help_string_append
, str
);
656 qemu_printf("Available chardev backend types: %s\n", str
->str
);
657 g_string_free(str
, true);
662 error_setg(errp
, "chardev: no id specified");
666 backend
= qemu_chr_parse_opts(opts
, errp
);
667 if (backend
== NULL
) {
671 cc
= char_get_class(name
, errp
);
676 if (qemu_opt_get_bool(opts
, "mux", 0)) {
677 bid
= g_strdup_printf("%s-base", id
);
680 chr
= qemu_chardev_new(bid
? bid
: id
,
681 object_class_get_name(OBJECT_CLASS(cc
)),
682 backend
, context
, errp
);
690 qapi_free_ChardevBackend(backend
);
691 backend
= g_new0(ChardevBackend
, 1);
692 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
693 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
694 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
695 mux
= qemu_chardev_new(id
, TYPE_CHARDEV_MUX
, backend
, context
, errp
);
697 object_unparent(OBJECT(chr
));
705 qapi_free_ChardevBackend(backend
);
710 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
711 bool permit_mux_mon
, GMainContext
*context
)
718 if (strstart(filename
, "chardev:", &p
)) {
719 return qemu_chr_find(p
);
722 opts
= qemu_chr_parse_compat(label
, filename
, permit_mux_mon
);
726 chr
= qemu_chr_new_from_opts(opts
, context
, &err
);
728 error_report_err(err
);
732 if (qemu_opt_get_bool(opts
, "mux", 0)) {
733 assert(permit_mux_mon
);
734 monitor_init_hmp(chr
, true);
742 static Chardev
*qemu_chr_new_permit_mux_mon(const char *label
,
743 const char *filename
,
745 GMainContext
*context
)
748 chr
= qemu_chr_new_noreplay(label
, filename
, permit_mux_mon
, context
);
750 if (replay_mode
!= REPLAY_MODE_NONE
) {
751 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
753 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
754 error_report("Replay: ioctl is not supported "
755 "for serial devices yet");
757 replay_register_char_driver(chr
);
762 Chardev
*qemu_chr_new(const char *label
, const char *filename
,
763 GMainContext
*context
)
765 return qemu_chr_new_permit_mux_mon(label
, filename
, false, context
);
768 Chardev
*qemu_chr_new_mux_mon(const char *label
, const char *filename
,
769 GMainContext
*context
)
771 return qemu_chr_new_permit_mux_mon(label
, filename
, true, context
);
774 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
776 Chardev
*chr
= CHARDEV(obj
);
777 ChardevInfoList
**list
= data
;
778 ChardevInfoList
*info
= g_malloc0(sizeof(*info
));
780 info
->value
= g_malloc0(sizeof(*info
->value
));
781 info
->value
->label
= g_strdup(chr
->label
);
782 info
->value
->filename
= g_strdup(chr
->filename
);
783 info
->value
->frontend_open
= chr
->be
&& chr
->be
->fe_open
;
791 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
793 ChardevInfoList
*chr_list
= NULL
;
795 object_child_foreach(get_chardevs_root(),
796 qmp_query_chardev_foreach
, &chr_list
);
802 qmp_prepend_backend(const char *name
, void *opaque
)
804 ChardevBackendInfoList
**list
= opaque
;
805 ChardevBackendInfoList
*info
= g_malloc0(sizeof(*info
));
807 info
->value
= g_malloc0(sizeof(*info
->value
));
808 info
->value
->name
= g_strdup(name
);
813 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
815 ChardevBackendInfoList
*backend_list
= NULL
;
817 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
822 Chardev
*qemu_chr_find(const char *name
)
824 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
826 return obj
? CHARDEV(obj
) : NULL
;
829 QemuOptsList qemu_chardev_opts
= {
831 .implied_opt_name
= "backend",
832 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
836 .type
= QEMU_OPT_STRING
,
839 .type
= QEMU_OPT_STRING
,
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_NUMBER
,
860 .type
= QEMU_OPT_BOOL
,
863 .type
= QEMU_OPT_BOOL
,
866 .type
= QEMU_OPT_BOOL
,
869 .type
= QEMU_OPT_BOOL
,
872 .type
= QEMU_OPT_BOOL
,
875 .type
= QEMU_OPT_NUMBER
,
878 .type
= QEMU_OPT_BOOL
,
881 .type
= QEMU_OPT_BOOL
,
884 .type
= QEMU_OPT_STRING
,
887 .type
= QEMU_OPT_STRING
,
890 .type
= QEMU_OPT_BOOL
,
893 .type
= QEMU_OPT_NUMBER
,
896 .type
= QEMU_OPT_NUMBER
,
899 .type
= QEMU_OPT_NUMBER
,
902 .type
= QEMU_OPT_NUMBER
,
905 .type
= QEMU_OPT_BOOL
,
908 .type
= QEMU_OPT_BOOL
,
911 .type
= QEMU_OPT_STRING
,
914 .type
= QEMU_OPT_NUMBER
,
917 .type
= QEMU_OPT_SIZE
,
920 .type
= QEMU_OPT_STRING
,
923 .type
= QEMU_OPT_BOOL
,
926 .type
= QEMU_OPT_STRING
,
929 .type
= QEMU_OPT_BOOL
,
931 { /* end of list */ }
935 bool qemu_chr_has_feature(Chardev
*chr
,
936 ChardevFeature feature
)
938 return test_bit(feature
, chr
->features
);
941 void qemu_chr_set_feature(Chardev
*chr
,
942 ChardevFeature feature
)
944 return set_bit(feature
, chr
->features
);
947 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
948 ChardevBackend
*backend
,
949 GMainContext
*gcontext
,
954 Error
*local_err
= NULL
;
955 bool be_opened
= true;
957 assert(g_str_has_prefix(typename
, "chardev-"));
959 obj
= object_new(typename
);
961 chr
->label
= g_strdup(id
);
962 chr
->gcontext
= gcontext
;
964 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
969 if (!chr
->filename
) {
970 chr
->filename
= g_strdup(typename
+ 8);
973 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
977 object_property_add_child(get_chardevs_root(), id
, obj
, &local_err
);
986 error_propagate(errp
, local_err
);
994 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
997 const ChardevClass
*cc
;
1001 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1006 chr
= qemu_chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
1007 backend
, NULL
, errp
);
1012 ret
= g_new0(ChardevReturn
, 1);
1013 if (CHARDEV_IS_PTY(chr
)) {
1014 ret
->pty
= g_strdup(chr
->filename
+ 4);
1015 ret
->has_pty
= true;
1021 ChardevReturn
*qmp_chardev_change(const char *id
, ChardevBackend
*backend
,
1025 const ChardevClass
*cc
;
1026 Chardev
*chr
, *chr_new
;
1027 bool closed_sent
= false;
1030 chr
= qemu_chr_find(id
);
1032 error_setg(errp
, "Chardev '%s' does not exist", id
);
1036 if (CHARDEV_IS_MUX(chr
)) {
1037 error_setg(errp
, "Mux device hotswap not supported yet");
1041 if (qemu_chr_replay(chr
)) {
1043 "Chardev '%s' cannot be changed in record/replay mode", id
);
1050 object_unparent(OBJECT(chr
));
1051 return qmp_chardev_add(id
, backend
, errp
);
1054 if (!be
->chr_be_change
) {
1055 error_setg(errp
, "Chardev user does not support chardev hotswap");
1059 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1064 chr_new
= qemu_chardev_new(NULL
, object_class_get_name(OBJECT_CLASS(cc
)),
1065 backend
, chr
->gcontext
, errp
);
1069 chr_new
->label
= g_strdup(id
);
1071 if (chr
->be_open
&& !chr_new
->be_open
) {
1072 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1077 qemu_chr_fe_init(be
, chr_new
, &error_abort
);
1079 if (be
->chr_be_change(be
->opaque
) < 0) {
1080 error_setg(errp
, "Chardev '%s' change failed", chr_new
->label
);
1082 qemu_chr_fe_init(be
, chr
, &error_abort
);
1084 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1086 object_unref(OBJECT(chr_new
));
1090 object_unparent(OBJECT(chr
));
1091 object_property_add_child(get_chardevs_root(), chr_new
->label
,
1092 OBJECT(chr_new
), &error_abort
);
1093 object_unref(OBJECT(chr_new
));
1095 ret
= g_new0(ChardevReturn
, 1);
1096 if (CHARDEV_IS_PTY(chr_new
)) {
1097 ret
->pty
= g_strdup(chr_new
->filename
+ 4);
1098 ret
->has_pty
= true;
1104 void qmp_chardev_remove(const char *id
, Error
**errp
)
1108 chr
= qemu_chr_find(id
);
1110 error_setg(errp
, "Chardev '%s' not found", id
);
1113 if (qemu_chr_is_busy(chr
)) {
1114 error_setg(errp
, "Chardev '%s' is busy", id
);
1117 if (qemu_chr_replay(chr
)) {
1119 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1122 object_unparent(OBJECT(chr
));
1125 void qmp_chardev_send_break(const char *id
, Error
**errp
)
1129 chr
= qemu_chr_find(id
);
1131 error_setg(errp
, "Chardev '%s' not found", id
);
1134 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
1138 * Add a timeout callback for the chardev (in milliseconds), return
1139 * the GSource object created. Please use this to add timeout hook for
1140 * chardev instead of g_timeout_add() and g_timeout_add_seconds(), to
1141 * make sure the gcontext that the task bound to is correct.
1143 GSource
*qemu_chr_timeout_add_ms(Chardev
*chr
, guint ms
,
1144 GSourceFunc func
, void *private)
1146 GSource
*source
= g_timeout_source_new(ms
);
1149 g_source_set_callback(source
, func
, private, NULL
);
1150 g_source_attach(source
, chr
->gcontext
);
1155 void qemu_chr_cleanup(void)
1157 object_unparent(get_chardevs_root());
1160 static void register_types(void)
1162 type_register_static(&char_type_info
);
1164 /* this must be done after machine init, since we register FEs with muxes
1165 * as part of realize functions like serial_isa_realizefn when -nographic
1168 qemu_add_machine_init_done_notifier(&chardev_machine_done_notify
);
1171 type_init(register_types
);