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
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "qemu/cutils.h"
27 #include "monitor/monitor.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/block-backend.h"
30 #include "qemu/error-report.h"
31 #include "qemu/timer.h"
32 #include "sysemu/char.h"
34 #include "qmp-commands.h"
35 #include "qapi/clone-visitor.h"
36 #include "qapi-visit.h"
37 #include "qemu/base64.h"
38 #include "io/channel-socket.h"
39 #include "io/channel-tls.h"
40 #include "sysemu/replay.h"
41 #include "qemu/help_option.h"
46 #include <sys/times.h>
48 #include <sys/ioctl.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
53 #include <arpa/inet.h>
55 #include <sys/select.h>
57 #include <sys/ethernet.h>
58 #include <sys/sockio.h>
59 #include <netinet/arp.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_icmp.h> // must come after ip.h
64 #include <netinet/udp.h>
65 #include <netinet/tcp.h>
69 #include "qemu/sockets.h"
70 #include "ui/qemu-spice.h"
77 #include "char-parallel.h"
78 #include "char-serial.h"
80 /***********************************************************/
81 /* character device */
83 static QTAILQ_HEAD(ChardevHead, Chardev) chardevs =
84 QTAILQ_HEAD_INITIALIZER(chardevs);
86 void qemu_chr_be_event(Chardev *s, int event)
88 CharBackend *be = s->be;
90 /* Keep track if the char device is open */
92 case CHR_EVENT_OPENED:
95 case CHR_EVENT_CLOSED:
100 if (!be || !be->chr_event) {
104 be->chr_event(be->opaque, event);
107 void qemu_chr_be_generic_open(Chardev *s)
109 qemu_chr_be_event(s, CHR_EVENT_OPENED);
113 /* Not reporting errors from writing to logfile, as logs are
114 * defined to be "best effort" only */
115 static void qemu_chr_fe_write_log(Chardev *s,
116 const uint8_t *buf, size_t len)
127 ret = write(s->logfd, buf + done, len - done);
128 if (ret == -1 && errno == EAGAIN) {
140 static int qemu_chr_fe_write_buffer(Chardev *s,
141 const uint8_t *buf, int len, int *offset)
143 ChardevClass *cc = CHARDEV_GET_CLASS(s);
147 qemu_mutex_lock(&s->chr_write_lock);
148 while (*offset < len) {
150 res = cc->chr_write(s, buf + *offset, len - *offset);
151 if (res < 0 && errno == EAGAIN) {
163 qemu_chr_fe_write_log(s, buf, *offset);
165 qemu_mutex_unlock(&s->chr_write_lock);
170 static bool qemu_chr_replay(Chardev *chr)
172 return qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
175 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)
177 Chardev *s = be->chr;
185 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
187 replay_char_write_event_load(&ret, &offset);
188 assert(offset <= len);
189 qemu_chr_fe_write_buffer(s, buf, offset, &offset);
193 cc = CHARDEV_GET_CLASS(s);
194 qemu_mutex_lock(&s->chr_write_lock);
195 ret = cc->chr_write(s, buf, len);
198 qemu_chr_fe_write_log(s, buf, ret);
201 qemu_mutex_unlock(&s->chr_write_lock);
203 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
204 replay_char_write_event_save(ret, ret < 0 ? 0 : ret);
210 int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len)
215 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
216 replay_char_write_event_load(&res, &offset);
217 assert(offset <= len);
218 qemu_chr_fe_write_buffer(s, buf, offset, &offset);
222 res = qemu_chr_fe_write_buffer(s, buf, len, &offset);
224 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
225 replay_char_write_event_save(res, offset);
234 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len)
236 Chardev *s = be->chr;
242 return qemu_chr_write_all(s, buf, len);
245 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)
247 Chardev *s = be->chr;
248 int offset = 0, counter = 10;
251 if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) {
255 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
256 return replay_char_read_all_load(buf);
259 while (offset < len) {
261 res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset,
263 if (res == -1 && errno == EAGAIN) {
273 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
274 replay_char_read_all_save_error(res);
286 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
287 replay_char_read_all_save_buf(buf, offset);
292 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
294 Chardev *s = be->chr;
297 if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) {
300 res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg);
306 int qemu_chr_be_can_write(Chardev *s)
308 CharBackend *be = s->be;
310 if (!be || !be->chr_can_read) {
314 return be->chr_can_read(be->opaque);
317 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
319 CharBackend *be = s->be;
321 if (be && be->chr_read) {
322 be->chr_read(be->opaque, buf, len);
326 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
328 if (qemu_chr_replay(s)) {
329 if (replay_mode == REPLAY_MODE_PLAY) {
332 replay_chr_be_write(s, buf, len);
334 qemu_chr_be_write_impl(s, buf, len);
338 int qemu_chr_fe_get_msgfd(CharBackend *be)
340 Chardev *s = be->chr;
342 int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1;
343 if (s && qemu_chr_replay(s)) {
344 error_report("Replay: get msgfd is not supported "
345 "for serial devices yet");
351 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
353 Chardev *s = be->chr;
359 return CHARDEV_GET_CLASS(s)->get_msgfds ?
360 CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1;
363 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
365 Chardev *s = be->chr;
371 return CHARDEV_GET_CLASS(s)->set_msgfds ?
372 CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1;
375 int qemu_chr_add_client(Chardev *s, int fd)
377 return CHARDEV_GET_CLASS(s)->chr_add_client ?
378 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
381 void qemu_chr_fe_accept_input(CharBackend *be)
383 Chardev *s = be->chr;
389 if (CHARDEV_GET_CLASS(s)->chr_accept_input) {
390 CHARDEV_GET_CLASS(s)->chr_accept_input(s);
395 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
397 char buf[CHR_READ_BUF_LEN];
400 vsnprintf(buf, sizeof(buf), fmt, ap);
401 /* XXX this blocks entire thread. Rewrite to use
402 * qemu_chr_fe_write and background I/O callbacks */
403 qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf));
407 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
408 bool *be_opened, Error **errp)
410 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
411 /* Any ChardevCommon member would work */
412 ChardevCommon *common = backend ? backend->u.null.data : NULL;
414 if (common && common->has_logfile) {
415 int flags = O_WRONLY | O_CREAT;
416 if (common->has_logappend &&
422 chr->logfd = qemu_open(common->logfile, flags, 0666);
423 if (chr->logfd < 0) {
424 error_setg_errno(errp, errno,
425 "Unable to open logfile %s",
432 cc->open(chr, backend, be_opened, errp);
436 static void char_init(Object *obj)
438 Chardev *chr = CHARDEV(obj);
441 qemu_mutex_init(&chr->chr_write_lock);
444 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
449 static void char_class_init(ObjectClass *oc, void *data)
451 ChardevClass *cc = CHARDEV_CLASS(oc);
453 cc->chr_write = null_chr_write;
456 static void char_finalize(Object *obj)
458 Chardev *chr = CHARDEV(obj);
463 g_free(chr->filename);
465 if (chr->logfd != -1) {
468 qemu_mutex_destroy(&chr->chr_write_lock);
471 static const TypeInfo char_type_info = {
472 .name = TYPE_CHARDEV,
473 .parent = TYPE_OBJECT,
474 .instance_size = sizeof(Chardev),
475 .instance_init = char_init,
476 .instance_finalize = char_finalize,
478 .class_size = sizeof(ChardevClass),
479 .class_init = char_class_init,
483 * Called after processing of default and command-line-specified
484 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
485 * to a mux chardev. This is done here to ensure that
486 * output/prompts/banners are only displayed for the FE that has
487 * focus when initial command-line processing/machine init is
490 * After this point, any new FE attached to any new or existing
491 * mux will receive CHR_EVENT_OPENED notifications for the BE
494 static void muxes_realize_done(Notifier *notifier, void *unused)
498 QTAILQ_FOREACH(chr, &chardevs, next) {
499 if (CHARDEV_IS_MUX(chr)) {
500 MuxChardev *d = MUX_CHARDEV(chr);
503 /* send OPENED to all already-attached FEs */
504 for (i = 0; i < d->mux_cnt; i++) {
505 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
507 /* mark mux as OPENED so any new FEs will immediately receive
510 qemu_chr_be_generic_open(chr);
513 muxes_realized = true;
516 static Notifier muxes_realize_notify = {
517 .notify = muxes_realize_done,
520 Chardev *qemu_chr_fe_get_driver(CharBackend *be)
525 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
529 if (CHARDEV_IS_MUX(s)) {
530 MuxChardev *d = MUX_CHARDEV(s);
532 if (d->mux_cnt >= MAX_MUX) {
536 d->backends[d->mux_cnt] = b;
550 error_setg(errp, QERR_DEVICE_IN_USE, s->label);
554 static bool qemu_chr_is_busy(Chardev *s)
556 if (CHARDEV_IS_MUX(s)) {
557 MuxChardev *d = MUX_CHARDEV(s);
558 return d->mux_cnt >= 0;
560 return s->be != NULL;
564 void qemu_chr_fe_deinit(CharBackend *b)
569 qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, true);
570 if (b->chr->be == b) {
573 if (CHARDEV_IS_MUX(b->chr)) {
574 MuxChardev *d = MUX_CHARDEV(b->chr);
575 d->backends[b->tag] = NULL;
581 void qemu_chr_fe_set_handlers(CharBackend *b,
582 IOCanReadHandler *fd_can_read,
583 IOReadHandler *fd_read,
584 IOEventHandler *fd_event,
586 GMainContext *context,
598 cc = CHARDEV_GET_CLASS(s);
599 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
601 remove_fd_in_watch(s);
605 b->chr_can_read = fd_can_read;
606 b->chr_read = fd_read;
607 b->chr_event = fd_event;
609 if (cc->chr_update_read_handler) {
610 cc->chr_update_read_handler(s, context);
614 qemu_chr_fe_set_open(b, fe_open);
618 qemu_chr_fe_take_focus(b);
619 /* We're connecting to an already opened device, so let's make sure we
620 also get the open event */
622 qemu_chr_be_generic_open(s);
626 if (CHARDEV_IS_MUX(s)) {
627 mux_chr_set_handlers(s, context);
631 void qemu_chr_fe_take_focus(CharBackend *b)
637 if (CHARDEV_IS_MUX(b->chr)) {
638 mux_set_focus(b->chr, b->tag);
642 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
644 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
646 if (cc->chr_wait_connected) {
647 return cc->chr_wait_connected(chr, errp);
653 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)
656 error_setg(errp, "missing associated backend");
660 return qemu_chr_wait_connected(be->chr, errp);
663 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
665 char host[65], port[33], width[8], height[8];
669 Error *local_err = NULL;
671 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
673 error_report_err(local_err);
677 if (strstart(filename, "mon:", &p)) {
679 qemu_opt_set(opts, "mux", "on", &error_abort);
680 if (strcmp(filename, "stdio") == 0) {
681 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
682 * but pass it to the guest. Handle this only for compat syntax,
683 * for -chardev syntax we have special option for this.
684 * This is what -nographic did, redirecting+muxing serial+monitor
685 * to stdio causing Ctrl+C to be passed to guest. */
686 qemu_opt_set(opts, "signal", "off", &error_abort);
690 if (strcmp(filename, "null") == 0 ||
691 strcmp(filename, "pty") == 0 ||
692 strcmp(filename, "msmouse") == 0 ||
693 strcmp(filename, "braille") == 0 ||
694 strcmp(filename, "testdev") == 0 ||
695 strcmp(filename, "stdio") == 0) {
696 qemu_opt_set(opts, "backend", filename, &error_abort);
699 if (strstart(filename, "vc", &p)) {
700 qemu_opt_set(opts, "backend", "vc", &error_abort);
702 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
704 qemu_opt_set(opts, "width", width, &error_abort);
705 qemu_opt_set(opts, "height", height, &error_abort);
706 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
708 qemu_opt_set(opts, "cols", width, &error_abort);
709 qemu_opt_set(opts, "rows", height, &error_abort);
716 if (strcmp(filename, "con:") == 0) {
717 qemu_opt_set(opts, "backend", "console", &error_abort);
720 if (strstart(filename, "COM", NULL)) {
721 qemu_opt_set(opts, "backend", "serial", &error_abort);
722 qemu_opt_set(opts, "path", filename, &error_abort);
725 if (strstart(filename, "file:", &p)) {
726 qemu_opt_set(opts, "backend", "file", &error_abort);
727 qemu_opt_set(opts, "path", p, &error_abort);
730 if (strstart(filename, "pipe:", &p)) {
731 qemu_opt_set(opts, "backend", "pipe", &error_abort);
732 qemu_opt_set(opts, "path", p, &error_abort);
735 if (strstart(filename, "tcp:", &p) ||
736 strstart(filename, "telnet:", &p)) {
737 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
739 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
742 qemu_opt_set(opts, "backend", "socket", &error_abort);
743 qemu_opt_set(opts, "host", host, &error_abort);
744 qemu_opt_set(opts, "port", port, &error_abort);
746 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
748 error_report_err(local_err);
752 if (strstart(filename, "telnet:", &p))
753 qemu_opt_set(opts, "telnet", "on", &error_abort);
756 if (strstart(filename, "udp:", &p)) {
757 qemu_opt_set(opts, "backend", "udp", &error_abort);
758 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
760 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
764 qemu_opt_set(opts, "host", host, &error_abort);
765 qemu_opt_set(opts, "port", port, &error_abort);
768 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
770 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
774 qemu_opt_set(opts, "localaddr", host, &error_abort);
775 qemu_opt_set(opts, "localport", port, &error_abort);
779 if (strstart(filename, "unix:", &p)) {
780 qemu_opt_set(opts, "backend", "socket", &error_abort);
781 qemu_opts_do_parse(opts, p, "path", &local_err);
783 error_report_err(local_err);
788 if (strstart(filename, "/dev/parport", NULL) ||
789 strstart(filename, "/dev/ppi", NULL)) {
790 qemu_opt_set(opts, "backend", "parport", &error_abort);
791 qemu_opt_set(opts, "path", filename, &error_abort);
794 if (strstart(filename, "/dev/", NULL)) {
795 qemu_opt_set(opts, "backend", "tty", &error_abort);
796 qemu_opt_set(opts, "path", filename, &error_abort);
805 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
807 const char *logfile = qemu_opt_get(opts, "logfile");
809 backend->has_logfile = logfile != NULL;
810 backend->logfile = logfile ? g_strdup(logfile) : NULL;
812 backend->has_logappend = true;
813 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
816 static const ChardevClass *char_get_class(const char *driver, Error **errp)
819 const ChardevClass *cc;
820 char *typename = g_strdup_printf("chardev-%s", driver);
822 oc = object_class_by_name(typename);
825 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
826 error_setg(errp, "'%s' is not a valid char driver name", driver);
830 if (object_class_is_abstract(oc)) {
831 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
832 "abstract device type");
836 cc = CHARDEV_CLASS(oc);
838 error_setg(errp, "'%s' is not a valid char driver name", driver);
845 static Chardev *qemu_chardev_add(const char *id, const char *typename,
846 ChardevBackend *backend, Error **errp)
850 chr = qemu_chr_find(id);
852 error_setg(errp, "Chardev '%s' already exists", id);
856 chr = qemu_chardev_new(id, typename, backend, errp);
861 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
865 static const struct ChardevAlias {
866 const char *typename;
868 } chardev_alias_table[] = {
869 #ifdef HAVE_CHARDEV_PARPORT
870 { "parallel", "parport" },
872 #ifdef HAVE_CHARDEV_SERIAL
877 typedef struct ChadevClassFE {
878 void (*fn)(const char *name, void *opaque);
883 chardev_class_foreach(ObjectClass *klass, void *opaque)
885 ChadevClassFE *fe = opaque;
887 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
888 if (CHARDEV_CLASS(klass)->internal) {
892 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
896 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
898 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
901 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
903 for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) {
904 fn(chardev_alias_table[i].alias, opaque);
909 help_string_append(const char *name, void *opaque)
911 GString *str = opaque;
913 g_string_append_printf(str, "\n%s", name);
916 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
919 Error *local_err = NULL;
920 const ChardevClass *cc;
923 ChardevBackend *backend = NULL;
924 const char *name = qemu_opt_get(opts, "backend");
925 const char *id = qemu_opts_id(opts);
929 error_setg(errp, "chardev: \"%s\" missing backend",
934 if (is_help_option(name)) {
935 GString *str = g_string_new("");
937 chardev_name_foreach(help_string_append, str);
939 error_report("Available chardev backend types: %s", str->str);
940 g_string_free(str, true);
945 error_setg(errp, "chardev: no id specified");
949 for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) {
950 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
951 name = chardev_alias_table[i].typename;
956 cc = char_get_class(name, errp);
961 backend = g_new0(ChardevBackend, 1);
962 backend->type = CHARDEV_BACKEND_KIND_NULL;
964 if (qemu_opt_get_bool(opts, "mux", 0)) {
965 bid = g_strdup_printf("%s-base", id);
970 cc->parse(opts, backend, &local_err);
972 error_propagate(errp, local_err);
976 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
977 qemu_chr_parse_common(opts, ccom);
978 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
981 chr = qemu_chardev_add(bid ? bid : id,
982 object_class_get_name(OBJECT_CLASS(cc)),
990 qapi_free_ChardevBackend(backend);
991 backend = g_new0(ChardevBackend, 1);
992 backend->type = CHARDEV_BACKEND_KIND_MUX;
993 backend->u.mux.data = g_new0(ChardevMux, 1);
994 backend->u.mux.data->chardev = g_strdup(bid);
995 mux = qemu_chardev_add(id, TYPE_CHARDEV_MUX, backend, errp);
997 qemu_chr_delete(chr);
1005 qapi_free_ChardevBackend(backend);
1010 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename)
1017 if (strstart(filename, "chardev:", &p)) {
1018 return qemu_chr_find(p);
1021 opts = qemu_chr_parse_compat(label, filename);
1025 chr = qemu_chr_new_from_opts(opts, &err);
1027 error_report_err(err);
1029 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
1030 monitor_init(chr, MONITOR_USE_READLINE);
1032 qemu_opts_del(opts);
1036 Chardev *qemu_chr_new(const char *label, const char *filename)
1039 chr = qemu_chr_new_noreplay(label, filename);
1041 if (replay_mode != REPLAY_MODE_NONE) {
1042 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
1044 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
1045 error_report("Replay: ioctl is not supported "
1046 "for serial devices yet");
1048 replay_register_char_driver(chr);
1053 void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
1055 Chardev *chr = be->chr;
1057 if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) {
1058 CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo);
1062 void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
1064 Chardev *chr = be->chr;
1070 if (be->fe_open == fe_open) {
1073 be->fe_open = fe_open;
1074 if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) {
1075 CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open);
1079 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
1080 GIOFunc func, void *user_data)
1082 Chardev *s = be->chr;
1086 if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) {
1090 src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond);
1095 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
1096 tag = g_source_attach(src, NULL);
1097 g_source_unref(src);
1102 void qemu_chr_fe_disconnect(CharBackend *be)
1104 Chardev *chr = be->chr;
1106 if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) {
1107 CHARDEV_GET_CLASS(chr)->chr_disconnect(chr);
1111 void qemu_chr_delete(Chardev *chr)
1113 QTAILQ_REMOVE(&chardevs, chr, next);
1114 object_unref(OBJECT(chr));
1117 ChardevInfoList *qmp_query_chardev(Error **errp)
1119 ChardevInfoList *chr_list = NULL;
1122 QTAILQ_FOREACH(chr, &chardevs, next) {
1123 ChardevInfoList *info = g_malloc0(sizeof(*info));
1124 info->value = g_malloc0(sizeof(*info->value));
1125 info->value->label = g_strdup(chr->label);
1126 info->value->filename = g_strdup(chr->filename);
1127 info->value->frontend_open = chr->be && chr->be->fe_open;
1129 info->next = chr_list;
1137 qmp_prepend_backend(const char *name, void *opaque)
1139 ChardevBackendInfoList **list = opaque;
1140 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
1142 info->value = g_malloc0(sizeof(*info->value));
1143 info->value->name = g_strdup(name);
1148 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
1150 ChardevBackendInfoList *backend_list = NULL;
1152 chardev_name_foreach(qmp_prepend_backend, &backend_list);
1154 return backend_list;
1157 Chardev *qemu_chr_find(const char *name)
1161 QTAILQ_FOREACH(chr, &chardevs, next) {
1162 if (strcmp(chr->label, name) != 0)
1169 QemuOptsList qemu_chardev_opts = {
1171 .implied_opt_name = "backend",
1172 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
1176 .type = QEMU_OPT_STRING,
1179 .type = QEMU_OPT_STRING,
1182 .type = QEMU_OPT_STRING,
1185 .type = QEMU_OPT_STRING,
1187 .name = "localaddr",
1188 .type = QEMU_OPT_STRING,
1190 .name = "localport",
1191 .type = QEMU_OPT_STRING,
1194 .type = QEMU_OPT_NUMBER,
1197 .type = QEMU_OPT_BOOL,
1200 .type = QEMU_OPT_BOOL,
1203 .type = QEMU_OPT_BOOL,
1206 .type = QEMU_OPT_BOOL,
1209 .type = QEMU_OPT_BOOL,
1211 .name = "reconnect",
1212 .type = QEMU_OPT_NUMBER,
1215 .type = QEMU_OPT_BOOL,
1217 .name = "tls-creds",
1218 .type = QEMU_OPT_STRING,
1221 .type = QEMU_OPT_NUMBER,
1224 .type = QEMU_OPT_NUMBER,
1227 .type = QEMU_OPT_NUMBER,
1230 .type = QEMU_OPT_NUMBER,
1233 .type = QEMU_OPT_BOOL,
1236 .type = QEMU_OPT_BOOL,
1239 .type = QEMU_OPT_STRING,
1242 .type = QEMU_OPT_NUMBER,
1245 .type = QEMU_OPT_SIZE,
1248 .type = QEMU_OPT_STRING,
1251 .type = QEMU_OPT_BOOL,
1254 .type = QEMU_OPT_STRING,
1256 .name = "logappend",
1257 .type = QEMU_OPT_BOOL,
1259 { /* end of list */ }
1263 bool qemu_chr_has_feature(Chardev *chr,
1264 ChardevFeature feature)
1266 return test_bit(feature, chr->features);
1269 void qemu_chr_set_feature(Chardev *chr,
1270 ChardevFeature feature)
1272 return set_bit(feature, chr->features);
1275 Chardev *qemu_chardev_new(const char *id, const char *typename,
1276 ChardevBackend *backend, Error **errp)
1278 Chardev *chr = NULL;
1279 Error *local_err = NULL;
1280 bool be_opened = true;
1282 assert(g_str_has_prefix(typename, "chardev-"));
1284 chr = CHARDEV(object_new(typename));
1285 chr->label = g_strdup(id);
1287 qemu_char_open(chr, backend, &be_opened, &local_err);
1289 error_propagate(errp, local_err);
1290 object_unref(OBJECT(chr));
1294 if (!chr->filename) {
1295 chr->filename = g_strdup(typename + 8);
1298 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
1304 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
1307 const ChardevClass *cc;
1311 cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp);
1316 chr = qemu_chardev_add(id, object_class_get_name(OBJECT_CLASS(cc)),
1322 ret = g_new0(ChardevReturn, 1);
1323 if (CHARDEV_IS_PTY(chr)) {
1324 ret->pty = g_strdup(chr->filename + 4);
1325 ret->has_pty = true;
1331 void qmp_chardev_remove(const char *id, Error **errp)
1335 chr = qemu_chr_find(id);
1337 error_setg(errp, "Chardev '%s' not found", id);
1340 if (qemu_chr_is_busy(chr)) {
1341 error_setg(errp, "Chardev '%s' is busy", id);
1344 if (qemu_chr_replay(chr)) {
1346 "Chardev '%s' cannot be unplugged in record/replay mode", id);
1349 qemu_chr_delete(chr);
1352 void qemu_chr_cleanup(void)
1356 QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) {
1357 qemu_chr_delete(chr);
1361 static void register_types(void)
1363 type_register_static(&char_type_info);
1365 /* this must be done after machine init, since we register FEs with muxes
1366 * as part of realize functions like serial_isa_realizefn when -nographic
1369 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
1372 type_init(register_types);