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 "monitor/qmp-helpers.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"
42 #include "qemu/yank.h"
44 #include "chardev-internal.h"
46 /***********************************************************/
47 /* character device */
49 Object
*get_chardevs_root(void)
51 return container_get(object_get_root(), "/chardevs");
54 static void chr_be_event(Chardev
*s
, QEMUChrEvent event
)
56 CharBackend
*be
= s
->be
;
58 if (!be
|| !be
->chr_event
) {
62 be
->chr_event(be
->opaque
, event
);
65 void qemu_chr_be_event(Chardev
*s
, QEMUChrEvent event
)
67 /* Keep track if the char device is open */
69 case CHR_EVENT_OPENED
:
72 case CHR_EVENT_CLOSED
:
76 case CHR_EVENT_MUX_IN
:
77 case CHR_EVENT_MUX_OUT
:
82 CHARDEV_GET_CLASS(s
)->chr_be_event(s
, event
);
85 /* Not reporting errors from writing to logfile, as logs are
86 * defined to be "best effort" only */
87 static void qemu_chr_write_log(Chardev
*s
, const uint8_t *buf
, size_t len
)
98 ret
= write(s
->logfd
, buf
+ done
, len
- done
);
99 if (ret
== -1 && errno
== EAGAIN
) {
111 static int qemu_chr_write_buffer(Chardev
*s
,
112 const uint8_t *buf
, int len
,
113 int *offset
, bool write_all
)
115 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
119 qemu_mutex_lock(&s
->chr_write_lock
);
120 while (*offset
< len
) {
122 res
= cc
->chr_write(s
, buf
+ *offset
, len
- *offset
);
123 if (res
< 0 && errno
== EAGAIN
&& write_all
) {
124 if (qemu_in_coroutine()) {
125 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
143 * If some data was written by backend, we should
144 * only log what was actually written. This method
145 * may be invoked again to write the remaining
146 * method, thus we'll log the remainder at that time.
148 qemu_chr_write_log(s
, buf
, *offset
);
149 } else if (res
< 0) {
151 * If a fatal error was reported by the backend,
152 * assume this method won't be invoked again with
153 * this buffer, so log it all right away.
155 qemu_chr_write_log(s
, buf
, len
);
157 qemu_mutex_unlock(&s
->chr_write_lock
);
162 int qemu_chr_write(Chardev
*s
, const uint8_t *buf
, int len
, bool write_all
)
167 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_PLAY
) {
168 replay_char_write_event_load(&res
, &offset
);
169 assert(offset
<= len
);
170 qemu_chr_write_buffer(s
, buf
, offset
, &offset
, true);
174 if (replay_mode
== REPLAY_MODE_RECORD
) {
176 * When recording we don't want temporary conditions to
177 * perturb the result. By ensuring we write everything we can
178 * while recording we avoid playback being out of sync if it
179 * doesn't encounter the same temporary conditions (usually
180 * triggered by external programs not reading the chardev fast
181 * enough and pipes filling up).
186 res
= qemu_chr_write_buffer(s
, buf
, len
, &offset
, write_all
);
188 if (qemu_chr_replay(s
) && replay_mode
== REPLAY_MODE_RECORD
) {
189 replay_char_write_event_save(res
, offset
);
198 int qemu_chr_be_can_write(Chardev
*s
)
200 CharBackend
*be
= s
->be
;
202 if (!be
|| !be
->chr_can_read
) {
206 return be
->chr_can_read(be
->opaque
);
209 void qemu_chr_be_write_impl(Chardev
*s
, const uint8_t *buf
, int len
)
211 CharBackend
*be
= s
->be
;
213 if (be
&& be
->chr_read
) {
214 be
->chr_read(be
->opaque
, buf
, len
);
218 void qemu_chr_be_write(Chardev
*s
, const uint8_t *buf
, int len
)
220 if (qemu_chr_replay(s
)) {
221 if (replay_mode
== REPLAY_MODE_PLAY
) {
224 replay_chr_be_write(s
, buf
, len
);
226 qemu_chr_be_write_impl(s
, buf
, len
);
230 void qemu_chr_be_update_read_handlers(Chardev
*s
,
231 GMainContext
*context
)
233 ChardevClass
*cc
= CHARDEV_GET_CLASS(s
);
235 assert(qemu_chr_has_feature(s
, QEMU_CHAR_FEATURE_GCONTEXT
)
237 s
->gcontext
= context
;
238 if (cc
->chr_update_read_handler
) {
239 cc
->chr_update_read_handler(s
);
243 int qemu_chr_add_client(Chardev
*s
, int fd
)
245 return CHARDEV_GET_CLASS(s
)->chr_add_client
?
246 CHARDEV_GET_CLASS(s
)->chr_add_client(s
, fd
) : -1;
249 static void qemu_char_open(Chardev
*chr
, ChardevBackend
*backend
,
250 bool *be_opened
, Error
**errp
)
252 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
253 /* Any ChardevCommon member would work */
254 ChardevCommon
*common
= backend
? backend
->u
.null
.data
: NULL
;
256 if (common
&& common
->logfile
) {
257 int flags
= O_WRONLY
;
258 if (common
->has_logappend
&&
264 chr
->logfd
= qemu_create(common
->logfile
, flags
, 0666, errp
);
265 if (chr
->logfd
< 0) {
271 cc
->open(chr
, backend
, be_opened
, errp
);
275 static void char_init(Object
*obj
)
277 Chardev
*chr
= CHARDEV(obj
);
279 chr
->handover_yank_instance
= false;
281 qemu_mutex_init(&chr
->chr_write_lock
);
284 * Assume if chr_update_read_handler is implemented it will
285 * take the updated gcontext into account.
287 if (CHARDEV_GET_CLASS(chr
)->chr_update_read_handler
) {
288 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
);
293 static int null_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
298 static void char_class_init(ObjectClass
*oc
, void *data
)
300 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
302 cc
->chr_write
= null_chr_write
;
303 cc
->chr_be_event
= chr_be_event
;
306 static void char_finalize(Object
*obj
)
308 Chardev
*chr
= CHARDEV(obj
);
313 g_free(chr
->filename
);
315 if (chr
->logfd
!= -1) {
318 qemu_mutex_destroy(&chr
->chr_write_lock
);
321 static const TypeInfo char_type_info
= {
322 .name
= TYPE_CHARDEV
,
323 .parent
= TYPE_OBJECT
,
324 .instance_size
= sizeof(Chardev
),
325 .instance_init
= char_init
,
326 .instance_finalize
= char_finalize
,
328 .class_size
= sizeof(ChardevClass
),
329 .class_init
= char_class_init
,
332 static bool qemu_chr_is_busy(Chardev
*s
)
334 if (CHARDEV_IS_MUX(s
)) {
335 MuxChardev
*d
= MUX_CHARDEV(s
);
336 return d
->mux_cnt
>= 0;
338 return s
->be
!= NULL
;
342 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
)
344 ChardevClass
*cc
= CHARDEV_GET_CLASS(chr
);
346 if (cc
->chr_wait_connected
) {
347 return cc
->chr_wait_connected(chr
, errp
);
353 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
,
356 char host
[65], port
[33], width
[8], height
[8];
360 Error
*local_err
= NULL
;
362 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1, &local_err
);
364 error_report_err(local_err
);
368 if (strstart(filename
, "mon:", &p
)) {
369 if (!permit_mux_mon
) {
370 error_report("mon: isn't supported in this context");
374 qemu_opt_set(opts
, "mux", "on", &error_abort
);
375 if (strcmp(filename
, "stdio") == 0) {
376 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
377 * but pass it to the guest. Handle this only for compat syntax,
378 * for -chardev syntax we have special option for this.
379 * This is what -nographic did, redirecting+muxing serial+monitor
380 * to stdio causing Ctrl+C to be passed to guest. */
381 qemu_opt_set(opts
, "signal", "off", &error_abort
);
385 if (strcmp(filename
, "null") == 0 ||
386 strcmp(filename
, "pty") == 0 ||
387 strcmp(filename
, "msmouse") == 0 ||
388 strcmp(filename
, "wctablet") == 0 ||
389 strcmp(filename
, "braille") == 0 ||
390 strcmp(filename
, "testdev") == 0 ||
391 strcmp(filename
, "stdio") == 0) {
392 qemu_opt_set(opts
, "backend", filename
, &error_abort
);
395 if (strstart(filename
, "vc", &p
)) {
396 qemu_opt_set(opts
, "backend", "vc", &error_abort
);
398 if (sscanf(p
+1, "%7[0-9]x%7[0-9]", width
, height
) == 2) {
400 qemu_opt_set(opts
, "width", width
, &error_abort
);
401 qemu_opt_set(opts
, "height", height
, &error_abort
);
402 } else if (sscanf(p
+1, "%7[0-9]Cx%7[0-9]C", width
, height
) == 2) {
404 qemu_opt_set(opts
, "cols", width
, &error_abort
);
405 qemu_opt_set(opts
, "rows", height
, &error_abort
);
412 if (strcmp(filename
, "con:") == 0) {
413 qemu_opt_set(opts
, "backend", "console", &error_abort
);
416 if (strstart(filename
, "COM", NULL
)) {
417 qemu_opt_set(opts
, "backend", "serial", &error_abort
);
418 qemu_opt_set(opts
, "path", filename
, &error_abort
);
421 if (strstart(filename
, "file:", &p
)) {
422 qemu_opt_set(opts
, "backend", "file", &error_abort
);
423 qemu_opt_set(opts
, "path", p
, &error_abort
);
426 if (strstart(filename
, "pipe:", &p
)) {
427 qemu_opt_set(opts
, "backend", "pipe", &error_abort
);
428 qemu_opt_set(opts
, "path", p
, &error_abort
);
431 if (strstart(filename
, "tcp:", &p
) ||
432 strstart(filename
, "telnet:", &p
) ||
433 strstart(filename
, "tn3270:", &p
) ||
434 strstart(filename
, "websocket:", &p
)) {
435 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
437 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
440 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
441 qemu_opt_set(opts
, "host", host
, &error_abort
);
442 qemu_opt_set(opts
, "port", port
, &error_abort
);
444 if (!qemu_opts_do_parse(opts
, p
+ pos
+ 1, NULL
, &local_err
)) {
445 error_report_err(local_err
);
449 if (strstart(filename
, "telnet:", &p
)) {
450 qemu_opt_set(opts
, "telnet", "on", &error_abort
);
451 } else if (strstart(filename
, "tn3270:", &p
)) {
452 qemu_opt_set(opts
, "tn3270", "on", &error_abort
);
453 } else if (strstart(filename
, "websocket:", &p
)) {
454 qemu_opt_set(opts
, "websocket", "on", &error_abort
);
458 if (strstart(filename
, "udp:", &p
)) {
459 qemu_opt_set(opts
, "backend", "udp", &error_abort
);
460 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
462 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
466 qemu_opt_set(opts
, "host", host
, &error_abort
);
467 qemu_opt_set(opts
, "port", port
, &error_abort
);
470 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
472 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
476 qemu_opt_set(opts
, "localaddr", host
, &error_abort
);
477 qemu_opt_set(opts
, "localport", port
, &error_abort
);
481 if (strstart(filename
, "unix:", &p
)) {
482 qemu_opt_set(opts
, "backend", "socket", &error_abort
);
483 if (!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
->logfile
= g_strdup(logfile
);
513 backend
->has_logappend
= true;
514 backend
->logappend
= qemu_opt_get_bool(opts
, "logappend", false);
517 static const ChardevClass
*char_get_class(const char *driver
, Error
**errp
)
520 const ChardevClass
*cc
;
521 char *typename
= g_strdup_printf("chardev-%s", driver
);
523 oc
= module_object_class_by_name(typename
);
526 if (!object_class_dynamic_cast(oc
, TYPE_CHARDEV
)) {
527 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
531 if (object_class_is_abstract(oc
)) {
532 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
533 "a non-abstract device type");
537 cc
= CHARDEV_CLASS(oc
);
539 error_setg(errp
, "'%s' is not a valid char driver name", driver
);
546 typedef struct ChadevClassFE
{
547 void (*fn
)(const char *name
, void *opaque
);
552 chardev_class_foreach(ObjectClass
*klass
, void *opaque
)
554 ChadevClassFE
*fe
= opaque
;
556 assert(g_str_has_prefix(object_class_get_name(klass
), "chardev-"));
557 if (CHARDEV_CLASS(klass
)->internal
) {
561 fe
->fn(object_class_get_name(klass
) + 8, fe
->opaque
);
565 chardev_name_foreach(void (*fn
)(const char *name
, void *opaque
),
568 ChadevClassFE fe
= { .fn
= fn
, .opaque
= opaque
};
570 object_class_foreach(chardev_class_foreach
, TYPE_CHARDEV
, false, &fe
);
574 help_string_append(const char *name
, void *opaque
)
576 GString
*str
= opaque
;
578 g_string_append_printf(str
, "\n %s", name
);
581 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
, Error
**errp
)
583 Error
*local_err
= NULL
;
584 const ChardevClass
*cc
;
585 ChardevBackend
*backend
= NULL
;
586 const char *name
= qemu_opt_get(opts
, "backend");
589 error_setg(errp
, "chardev: \"%s\" missing backend",
594 cc
= char_get_class(name
, errp
);
599 backend
= g_new0(ChardevBackend
, 1);
600 backend
->type
= CHARDEV_BACKEND_KIND_NULL
;
603 cc
->parse(opts
, backend
, &local_err
);
605 error_propagate(errp
, local_err
);
606 qapi_free_ChardevBackend(backend
);
610 ChardevCommon
*ccom
= g_new0(ChardevCommon
, 1);
611 qemu_chr_parse_common(opts
, ccom
);
612 backend
->u
.null
.data
= ccom
; /* Any ChardevCommon member would work */
618 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
, GMainContext
*context
,
621 const ChardevClass
*cc
;
623 ChardevBackend
*backend
= NULL
;
624 const char *name
= qemu_opt_get(opts
, "backend");
625 const char *id
= qemu_opts_id(opts
);
628 if (name
&& is_help_option(name
)) {
629 GString
*str
= g_string_new("");
631 chardev_name_foreach(help_string_append
, str
);
633 qemu_printf("Available chardev backend types: %s\n", str
->str
);
634 g_string_free(str
, true);
639 error_setg(errp
, "chardev: no id specified");
643 backend
= qemu_chr_parse_opts(opts
, errp
);
644 if (backend
== NULL
) {
648 cc
= char_get_class(name
, errp
);
653 if (qemu_opt_get_bool(opts
, "mux", 0)) {
654 bid
= g_strdup_printf("%s-base", id
);
657 chr
= qemu_chardev_new(bid
? bid
: id
,
658 object_class_get_name(OBJECT_CLASS(cc
)),
659 backend
, context
, errp
);
667 qapi_free_ChardevBackend(backend
);
668 backend
= g_new0(ChardevBackend
, 1);
669 backend
->type
= CHARDEV_BACKEND_KIND_MUX
;
670 backend
->u
.mux
.data
= g_new0(ChardevMux
, 1);
671 backend
->u
.mux
.data
->chardev
= g_strdup(bid
);
672 mux
= qemu_chardev_new(id
, TYPE_CHARDEV_MUX
, backend
, context
, errp
);
674 object_unparent(OBJECT(chr
));
682 qapi_free_ChardevBackend(backend
);
687 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
688 bool permit_mux_mon
, GMainContext
*context
)
695 if (strstart(filename
, "chardev:", &p
)) {
696 return qemu_chr_find(p
);
699 opts
= qemu_chr_parse_compat(label
, filename
, permit_mux_mon
);
703 chr
= qemu_chr_new_from_opts(opts
, context
, &err
);
705 error_report_err(err
);
709 if (qemu_opt_get_bool(opts
, "mux", 0)) {
710 assert(permit_mux_mon
);
711 monitor_init_hmp(chr
, true, &err
);
713 error_report_err(err
);
714 object_unparent(OBJECT(chr
));
725 static Chardev
*qemu_chr_new_permit_mux_mon(const char *label
,
726 const char *filename
,
728 GMainContext
*context
)
731 chr
= qemu_chr_new_noreplay(label
, filename
, permit_mux_mon
, context
);
733 if (replay_mode
!= REPLAY_MODE_NONE
) {
734 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_REPLAY
);
736 if (qemu_chr_replay(chr
) && CHARDEV_GET_CLASS(chr
)->chr_ioctl
) {
737 error_report("Replay: ioctl is not supported "
738 "for serial devices yet");
740 replay_register_char_driver(chr
);
745 Chardev
*qemu_chr_new(const char *label
, const char *filename
,
746 GMainContext
*context
)
748 return qemu_chr_new_permit_mux_mon(label
, filename
, false, context
);
751 Chardev
*qemu_chr_new_mux_mon(const char *label
, const char *filename
,
752 GMainContext
*context
)
754 return qemu_chr_new_permit_mux_mon(label
, filename
, true, context
);
757 static int qmp_query_chardev_foreach(Object
*obj
, void *data
)
759 Chardev
*chr
= CHARDEV(obj
);
760 ChardevInfoList
**list
= data
;
761 ChardevInfo
*value
= g_malloc0(sizeof(*value
));
763 value
->label
= g_strdup(chr
->label
);
764 value
->filename
= g_strdup(chr
->filename
);
765 value
->frontend_open
= chr
->be
&& chr
->be
->fe_is_open
;
767 QAPI_LIST_PREPEND(*list
, value
);
772 ChardevInfoList
*qmp_query_chardev(Error
**errp
)
774 ChardevInfoList
*chr_list
= NULL
;
776 object_child_foreach(get_chardevs_root(),
777 qmp_query_chardev_foreach
, &chr_list
);
783 qmp_prepend_backend(const char *name
, void *opaque
)
785 ChardevBackendInfoList
**list
= opaque
;
786 ChardevBackendInfo
*value
;
788 value
= g_new0(ChardevBackendInfo
, 1);
789 value
->name
= g_strdup(name
);
790 QAPI_LIST_PREPEND(*list
, value
);
793 ChardevBackendInfoList
*qmp_query_chardev_backends(Error
**errp
)
795 ChardevBackendInfoList
*backend_list
= NULL
;
797 chardev_name_foreach(qmp_prepend_backend
, &backend_list
);
802 Chardev
*qemu_chr_find(const char *name
)
804 Object
*obj
= object_resolve_path_component(get_chardevs_root(), name
);
806 return obj
? CHARDEV(obj
) : NULL
;
809 QemuOptsList qemu_chardev_opts
= {
811 .implied_opt_name
= "backend",
812 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
816 .type
= QEMU_OPT_STRING
,
819 .type
= QEMU_OPT_STRING
,
821 .name
= "input-path",
822 .type
= QEMU_OPT_STRING
,
825 .type
= QEMU_OPT_STRING
,
828 .type
= QEMU_OPT_STRING
,
831 .type
= QEMU_OPT_STRING
,
834 .type
= QEMU_OPT_STRING
,
837 .type
= QEMU_OPT_STRING
,
840 .type
= QEMU_OPT_NUMBER
,
843 .type
= QEMU_OPT_BOOL
,
846 .type
= QEMU_OPT_BOOL
,
849 .type
= QEMU_OPT_BOOL
,
852 .type
= QEMU_OPT_BOOL
,
855 .type
= QEMU_OPT_BOOL
,
858 .type
= QEMU_OPT_BOOL
,
861 .type
= QEMU_OPT_NUMBER
,
864 .type
= QEMU_OPT_BOOL
,
867 .type
= QEMU_OPT_BOOL
,
870 .type
= QEMU_OPT_STRING
,
873 .type
= QEMU_OPT_STRING
,
876 .type
= QEMU_OPT_BOOL
,
879 .type
= QEMU_OPT_NUMBER
,
882 .type
= QEMU_OPT_NUMBER
,
885 .type
= QEMU_OPT_NUMBER
,
888 .type
= QEMU_OPT_NUMBER
,
891 .type
= QEMU_OPT_BOOL
,
894 .type
= QEMU_OPT_BOOL
,
897 .type
= QEMU_OPT_STRING
,
900 .type
= QEMU_OPT_NUMBER
,
903 .type
= QEMU_OPT_SIZE
,
906 .type
= QEMU_OPT_STRING
,
909 .type
= QEMU_OPT_BOOL
,
912 .type
= QEMU_OPT_STRING
,
915 .type
= QEMU_OPT_BOOL
,
918 .type
= QEMU_OPT_BOOL
,
921 .type
= QEMU_OPT_BOOL
,
925 .type
= QEMU_OPT_BOOL
,
926 .def_value_str
= "on",
929 .type
= QEMU_OPT_BOOL
,
932 { /* end of list */ }
936 bool qemu_chr_has_feature(Chardev
*chr
,
937 ChardevFeature feature
)
939 return test_bit(feature
, chr
->features
);
942 void qemu_chr_set_feature(Chardev
*chr
,
943 ChardevFeature feature
)
945 return set_bit(feature
, chr
->features
);
948 static Chardev
*chardev_new(const char *id
, const char *typename
,
949 ChardevBackend
*backend
,
950 GMainContext
*gcontext
,
951 bool handover_yank_instance
,
956 Error
*local_err
= NULL
;
957 bool be_opened
= true;
959 assert(g_str_has_prefix(typename
, "chardev-"));
962 obj
= object_new(typename
);
964 chr
->handover_yank_instance
= handover_yank_instance
;
965 chr
->label
= g_strdup(id
);
966 chr
->gcontext
= gcontext
;
968 qemu_char_open(chr
, backend
, &be_opened
, &local_err
);
970 error_propagate(errp
, local_err
);
975 if (!chr
->filename
) {
976 chr
->filename
= g_strdup(typename
+ 8);
979 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
985 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
986 ChardevBackend
*backend
,
987 GMainContext
*gcontext
,
991 g_autofree
char *genid
= NULL
;
994 genid
= id_generate(ID_CHR
);
998 chr
= chardev_new(id
, typename
, backend
, gcontext
, false, errp
);
1003 if (!object_property_try_add_child(get_chardevs_root(), id
, OBJECT(chr
),
1005 object_unref(OBJECT(chr
));
1008 object_unref(OBJECT(chr
));
1013 ChardevReturn
*qmp_chardev_add(const char *id
, ChardevBackend
*backend
,
1017 const ChardevClass
*cc
;
1019 g_autoptr(Chardev
) chr
= NULL
;
1021 if (qemu_chr_find(id
)) {
1022 error_setg(errp
, "Chardev with id '%s' already exists", id
);
1026 cc
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1031 chr
= chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc
)),
1032 backend
, NULL
, false, errp
);
1037 if (!object_property_try_add_child(get_chardevs_root(), id
, OBJECT(chr
),
1042 ret
= g_new0(ChardevReturn
, 1);
1043 if (CHARDEV_IS_PTY(chr
)) {
1044 ret
->pty
= g_strdup(chr
->filename
+ 4);
1050 error_prepend(errp
, "Failed to add chardev '%s': ", id
);
1054 ChardevReturn
*qmp_chardev_change(const char *id
, ChardevBackend
*backend
,
1058 const ChardevClass
*cc
, *cc_new
;
1059 Chardev
*chr
, *chr_new
;
1060 bool closed_sent
= false;
1061 bool handover_yank_instance
;
1064 chr
= qemu_chr_find(id
);
1066 error_setg(errp
, "Chardev '%s' does not exist", id
);
1070 if (CHARDEV_IS_MUX(chr
)) {
1071 error_setg(errp
, "Mux device hotswap not supported yet");
1075 if (qemu_chr_replay(chr
)) {
1077 "Chardev '%s' cannot be changed in record/replay mode", id
);
1084 object_unparent(OBJECT(chr
));
1085 return qmp_chardev_add(id
, backend
, errp
);
1088 if (!be
->chr_be_change
) {
1089 error_setg(errp
, "Chardev user does not support chardev hotswap");
1093 cc
= CHARDEV_GET_CLASS(chr
);
1094 cc_new
= char_get_class(ChardevBackendKind_str(backend
->type
), errp
);
1100 * The new chardev should not register a yank instance if the current
1101 * chardev has registered one already.
1103 handover_yank_instance
= cc
->supports_yank
&& cc_new
->supports_yank
;
1105 chr_new
= chardev_new(id
, object_class_get_name(OBJECT_CLASS(cc_new
)),
1106 backend
, chr
->gcontext
, handover_yank_instance
, errp
);
1111 if (chr
->be_open
&& !chr_new
->be_open
) {
1112 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1117 qemu_chr_fe_init(be
, chr_new
, &error_abort
);
1119 if (be
->chr_be_change(be
->opaque
) < 0) {
1120 error_setg(errp
, "Chardev '%s' change failed", chr_new
->label
);
1122 qemu_chr_fe_init(be
, chr
, &error_abort
);
1124 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
1126 object_unref(OBJECT(chr_new
));
1130 /* change successful, clean up */
1131 chr_new
->handover_yank_instance
= false;
1134 * When the old chardev is freed, it should not unregister the yank
1135 * instance if the new chardev needs it.
1137 chr
->handover_yank_instance
= handover_yank_instance
;
1139 object_unparent(OBJECT(chr
));
1140 object_property_add_child(get_chardevs_root(), chr_new
->label
,
1142 object_unref(OBJECT(chr_new
));
1144 ret
= g_new0(ChardevReturn
, 1);
1145 if (CHARDEV_IS_PTY(chr_new
)) {
1146 ret
->pty
= g_strdup(chr_new
->filename
+ 4);
1152 void qmp_chardev_remove(const char *id
, Error
**errp
)
1156 chr
= qemu_chr_find(id
);
1158 error_setg(errp
, "Chardev '%s' not found", id
);
1161 if (qemu_chr_is_busy(chr
)) {
1162 error_setg(errp
, "Chardev '%s' is busy", id
);
1165 if (qemu_chr_replay(chr
)) {
1167 "Chardev '%s' cannot be unplugged in record/replay mode", id
);
1170 object_unparent(OBJECT(chr
));
1173 void qmp_chardev_send_break(const char *id
, Error
**errp
)
1177 chr
= qemu_chr_find(id
);
1179 error_setg(errp
, "Chardev '%s' not found", id
);
1182 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
1185 bool qmp_add_client_char(int fd
, bool has_skipauth
, bool skipauth
,
1186 bool has_tls
, bool tls
, const char *protocol
,
1189 Chardev
*s
= qemu_chr_find(protocol
);
1192 error_setg(errp
, "protocol '%s' is invalid", protocol
);
1195 if (qemu_chr_add_client(s
, fd
) < 0) {
1196 error_setg(errp
, "failed to add client");
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
);