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-file.h"
40 #include "io/channel-tls.h"
41 #include "sysemu/replay.h"
42 #include "qemu/help_option.h"
47 #include <sys/times.h>
50 #include <sys/ioctl.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <arpa/inet.h>
57 #include <sys/select.h>
59 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
60 #include <dev/ppbus/ppi.h>
61 #include <dev/ppbus/ppbconf.h>
62 #elif defined(__DragonFly__)
63 #include <dev/misc/ppi/ppi.h>
64 #include <bus/ppbus/ppbconf.h>
68 #include <linux/ppdev.h>
69 #include <linux/parport.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
85 #include "qemu/sockets.h"
86 #include "ui/qemu-spice.h"
93 #include "char-win-stdio.h"
96 /***********************************************************/
97 /* character device */
99 static QTAILQ_HEAD(ChardevHead, Chardev) chardevs =
100 QTAILQ_HEAD_INITIALIZER(chardevs);
102 void qemu_chr_be_event(Chardev *s, int event)
104 CharBackend *be = s->be;
106 /* Keep track if the char device is open */
108 case CHR_EVENT_OPENED:
111 case CHR_EVENT_CLOSED:
116 if (!be || !be->chr_event) {
120 be->chr_event(be->opaque, event);
123 void qemu_chr_be_generic_open(Chardev *s)
125 qemu_chr_be_event(s, CHR_EVENT_OPENED);
129 /* Not reporting errors from writing to logfile, as logs are
130 * defined to be "best effort" only */
131 static void qemu_chr_fe_write_log(Chardev *s,
132 const uint8_t *buf, size_t len)
143 ret = write(s->logfd, buf + done, len - done);
144 if (ret == -1 && errno == EAGAIN) {
156 static int qemu_chr_fe_write_buffer(Chardev *s,
157 const uint8_t *buf, int len, int *offset)
159 ChardevClass *cc = CHARDEV_GET_CLASS(s);
163 qemu_mutex_lock(&s->chr_write_lock);
164 while (*offset < len) {
166 res = cc->chr_write(s, buf + *offset, len - *offset);
167 if (res < 0 && errno == EAGAIN) {
179 qemu_chr_fe_write_log(s, buf, *offset);
181 qemu_mutex_unlock(&s->chr_write_lock);
186 static bool qemu_chr_replay(Chardev *chr)
188 return qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
191 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)
193 Chardev *s = be->chr;
201 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
203 replay_char_write_event_load(&ret, &offset);
204 assert(offset <= len);
205 qemu_chr_fe_write_buffer(s, buf, offset, &offset);
209 cc = CHARDEV_GET_CLASS(s);
210 qemu_mutex_lock(&s->chr_write_lock);
211 ret = cc->chr_write(s, buf, len);
214 qemu_chr_fe_write_log(s, buf, ret);
217 qemu_mutex_unlock(&s->chr_write_lock);
219 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
220 replay_char_write_event_save(ret, ret < 0 ? 0 : ret);
226 int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len)
231 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
232 replay_char_write_event_load(&res, &offset);
233 assert(offset <= len);
234 qemu_chr_fe_write_buffer(s, buf, offset, &offset);
238 res = qemu_chr_fe_write_buffer(s, buf, len, &offset);
240 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
241 replay_char_write_event_save(res, offset);
250 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len)
252 Chardev *s = be->chr;
258 return qemu_chr_write_all(s, buf, len);
261 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)
263 Chardev *s = be->chr;
264 int offset = 0, counter = 10;
267 if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) {
271 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
272 return replay_char_read_all_load(buf);
275 while (offset < len) {
277 res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset,
279 if (res == -1 && errno == EAGAIN) {
289 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
290 replay_char_read_all_save_error(res);
302 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
303 replay_char_read_all_save_buf(buf, offset);
308 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
310 Chardev *s = be->chr;
313 if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) {
316 res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg);
322 int qemu_chr_be_can_write(Chardev *s)
324 CharBackend *be = s->be;
326 if (!be || !be->chr_can_read) {
330 return be->chr_can_read(be->opaque);
333 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
335 CharBackend *be = s->be;
337 if (be && be->chr_read) {
338 be->chr_read(be->opaque, buf, len);
342 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
344 if (qemu_chr_replay(s)) {
345 if (replay_mode == REPLAY_MODE_PLAY) {
348 replay_chr_be_write(s, buf, len);
350 qemu_chr_be_write_impl(s, buf, len);
354 int qemu_chr_fe_get_msgfd(CharBackend *be)
356 Chardev *s = be->chr;
358 int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1;
359 if (s && qemu_chr_replay(s)) {
360 error_report("Replay: get msgfd is not supported "
361 "for serial devices yet");
367 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
369 Chardev *s = be->chr;
375 return CHARDEV_GET_CLASS(s)->get_msgfds ?
376 CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1;
379 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
381 Chardev *s = be->chr;
387 return CHARDEV_GET_CLASS(s)->set_msgfds ?
388 CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1;
391 int qemu_chr_add_client(Chardev *s, int fd)
393 return CHARDEV_GET_CLASS(s)->chr_add_client ?
394 CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1;
397 void qemu_chr_fe_accept_input(CharBackend *be)
399 Chardev *s = be->chr;
405 if (CHARDEV_GET_CLASS(s)->chr_accept_input) {
406 CHARDEV_GET_CLASS(s)->chr_accept_input(s);
411 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
413 char buf[CHR_READ_BUF_LEN];
416 vsnprintf(buf, sizeof(buf), fmt, ap);
417 /* XXX this blocks entire thread. Rewrite to use
418 * qemu_chr_fe_write and background I/O callbacks */
419 qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf));
423 static void qemu_char_open(Chardev *chr, ChardevBackend *backend,
424 bool *be_opened, Error **errp)
426 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
427 /* Any ChardevCommon member would work */
428 ChardevCommon *common = backend ? backend->u.null.data : NULL;
430 if (common && common->has_logfile) {
431 int flags = O_WRONLY | O_CREAT;
432 if (common->has_logappend &&
438 chr->logfd = qemu_open(common->logfile, flags, 0666);
439 if (chr->logfd < 0) {
440 error_setg_errno(errp, errno,
441 "Unable to open logfile %s",
448 cc->open(chr, backend, be_opened, errp);
452 static void char_init(Object *obj)
454 Chardev *chr = CHARDEV(obj);
457 qemu_mutex_init(&chr->chr_write_lock);
460 static int null_chr_write(Chardev *chr, const uint8_t *buf, int len)
465 static void char_class_init(ObjectClass *oc, void *data)
467 ChardevClass *cc = CHARDEV_CLASS(oc);
469 cc->chr_write = null_chr_write;
472 static void char_finalize(Object *obj)
474 Chardev *chr = CHARDEV(obj);
479 g_free(chr->filename);
481 if (chr->logfd != -1) {
484 qemu_mutex_destroy(&chr->chr_write_lock);
487 static const TypeInfo char_type_info = {
488 .name = TYPE_CHARDEV,
489 .parent = TYPE_OBJECT,
490 .instance_size = sizeof(Chardev),
491 .instance_init = char_init,
492 .instance_finalize = char_finalize,
494 .class_size = sizeof(ChardevClass),
495 .class_init = char_class_init,
499 * Called after processing of default and command-line-specified
500 * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
501 * to a mux chardev. This is done here to ensure that
502 * output/prompts/banners are only displayed for the FE that has
503 * focus when initial command-line processing/machine init is
506 * After this point, any new FE attached to any new or existing
507 * mux will receive CHR_EVENT_OPENED notifications for the BE
510 static void muxes_realize_done(Notifier *notifier, void *unused)
514 QTAILQ_FOREACH(chr, &chardevs, next) {
515 if (CHARDEV_IS_MUX(chr)) {
516 MuxChardev *d = MUX_CHARDEV(chr);
519 /* send OPENED to all already-attached FEs */
520 for (i = 0; i < d->mux_cnt; i++) {
521 mux_chr_send_event(d, i, CHR_EVENT_OPENED);
523 /* mark mux as OPENED so any new FEs will immediately receive
526 qemu_chr_be_generic_open(chr);
529 muxes_realized = true;
532 static Notifier muxes_realize_notify = {
533 .notify = muxes_realize_done,
536 Chardev *qemu_chr_fe_get_driver(CharBackend *be)
541 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
545 if (CHARDEV_IS_MUX(s)) {
546 MuxChardev *d = MUX_CHARDEV(s);
548 if (d->mux_cnt >= MAX_MUX) {
552 d->backends[d->mux_cnt] = b;
566 error_setg(errp, QERR_DEVICE_IN_USE, s->label);
570 static bool qemu_chr_is_busy(Chardev *s)
572 if (CHARDEV_IS_MUX(s)) {
573 MuxChardev *d = MUX_CHARDEV(s);
574 return d->mux_cnt >= 0;
576 return s->be != NULL;
580 void qemu_chr_fe_deinit(CharBackend *b)
585 qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, true);
586 if (b->chr->be == b) {
589 if (CHARDEV_IS_MUX(b->chr)) {
590 MuxChardev *d = MUX_CHARDEV(b->chr);
591 d->backends[b->tag] = NULL;
597 void qemu_chr_fe_set_handlers(CharBackend *b,
598 IOCanReadHandler *fd_can_read,
599 IOReadHandler *fd_read,
600 IOEventHandler *fd_event,
602 GMainContext *context,
614 cc = CHARDEV_GET_CLASS(s);
615 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
617 remove_fd_in_watch(s);
621 b->chr_can_read = fd_can_read;
622 b->chr_read = fd_read;
623 b->chr_event = fd_event;
625 if (cc->chr_update_read_handler) {
626 cc->chr_update_read_handler(s, context);
630 qemu_chr_fe_set_open(b, fe_open);
634 qemu_chr_fe_take_focus(b);
635 /* We're connecting to an already opened device, so let's make sure we
636 also get the open event */
638 qemu_chr_be_generic_open(s);
642 if (CHARDEV_IS_MUX(s)) {
643 mux_chr_set_handlers(s, context);
647 void qemu_chr_fe_take_focus(CharBackend *b)
653 if (CHARDEV_IS_MUX(b->chr)) {
654 mux_set_focus(b->chr, b->tag);
659 static void qemu_chr_open_pipe(Chardev *chr,
660 ChardevBackend *backend,
664 ChardevHostdev *opts = backend->u.pipe.data;
668 const char *filename = opts->device;
670 filename_in = g_strdup_printf("%s.in", filename);
671 filename_out = g_strdup_printf("%s.out", filename);
672 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
673 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
675 g_free(filename_out);
676 if (fd_in < 0 || fd_out < 0) {
681 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
683 error_setg_file_open(errp, errno, filename);
687 qemu_chr_open_fd(chr, fd_in, fd_out);
690 /* init terminal so that we can grab keys */
691 static struct termios oldtty;
692 static int old_fd0_flags;
693 static bool stdio_in_use;
694 static bool stdio_allow_signal;
695 static bool stdio_echo_state;
697 static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo);
699 static void term_exit(void)
701 tcsetattr (0, TCSANOW, &oldtty);
702 fcntl(0, F_SETFL, old_fd0_flags);
705 static void term_stdio_handler(int sig)
707 /* restore echo after resume from suspend. */
708 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
711 static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo)
715 stdio_echo_state = echo;
718 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
719 |INLCR|IGNCR|ICRNL|IXON);
720 tty.c_oflag |= OPOST;
721 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
722 tty.c_cflag &= ~(CSIZE|PARENB);
727 if (!stdio_allow_signal)
728 tty.c_lflag &= ~ISIG;
730 tcsetattr (0, TCSANOW, &tty);
733 static void char_stdio_finalize(Object *obj)
738 static void qemu_chr_open_stdio(Chardev *chr,
739 ChardevBackend *backend,
743 ChardevStdio *opts = backend->u.stdio.data;
744 struct sigaction act;
746 if (is_daemonized()) {
747 error_setg(errp, "cannot use stdio with -daemonize");
752 error_setg(errp, "cannot use stdio by multiple character devices");
757 old_fd0_flags = fcntl(0, F_GETFL);
758 tcgetattr(0, &oldtty);
759 qemu_set_nonblock(0);
762 memset(&act, 0, sizeof(act));
763 act.sa_handler = term_stdio_handler;
764 sigaction(SIGCONT, &act, NULL);
766 qemu_chr_open_fd(chr, 0, 1);
768 if (opts->has_signal) {
769 stdio_allow_signal = opts->signal;
771 qemu_chr_set_echo_stdio(chr, false);
774 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
775 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
776 || defined(__GLIBC__)
778 #define HAVE_CHARDEV_SERIAL 1
779 #define HAVE_CHARDEV_PTY 1
786 /* Protected by the Chardev chr_write_lock. */
792 #define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY)
794 static void pty_chr_update_read_handler_locked(Chardev *chr);
795 static void pty_chr_state(Chardev *chr, int connected);
797 static gboolean pty_chr_timer(gpointer opaque)
799 struct Chardev *chr = CHARDEV(opaque);
800 PtyChardev *s = PTY_CHARDEV(opaque);
802 qemu_mutex_lock(&chr->chr_write_lock);
807 pty_chr_update_read_handler_locked(chr);
809 qemu_mutex_unlock(&chr->chr_write_lock);
813 /* Called with chr_write_lock held. */
814 static void pty_chr_rearm_timer(Chardev *chr, int ms)
816 PtyChardev *s = PTY_CHARDEV(chr);
820 g_source_remove(s->timer_tag);
825 name = g_strdup_printf("pty-timer-secs-%s", chr->label);
826 s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
828 name = g_strdup_printf("pty-timer-ms-%s", chr->label);
829 s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
831 g_source_set_name_by_id(s->timer_tag, name);
835 /* Called with chr_write_lock held. */
836 static void pty_chr_update_read_handler_locked(Chardev *chr)
838 PtyChardev *s = PTY_CHARDEV(chr);
841 QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc);
844 pfd.events = G_IO_OUT;
847 rc = g_poll(&pfd, 1, 0);
848 } while (rc == -1 && errno == EINTR);
851 if (pfd.revents & G_IO_HUP) {
852 pty_chr_state(chr, 0);
854 pty_chr_state(chr, 1);
858 static void pty_chr_update_read_handler(Chardev *chr,
859 GMainContext *context)
861 qemu_mutex_lock(&chr->chr_write_lock);
862 pty_chr_update_read_handler_locked(chr);
863 qemu_mutex_unlock(&chr->chr_write_lock);
866 /* Called with chr_write_lock held. */
867 static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len)
869 PtyChardev *s = PTY_CHARDEV(chr);
872 /* guest sends data, check for (re-)connect */
873 pty_chr_update_read_handler_locked(chr);
878 return io_channel_send(s->ioc, buf, len);
881 static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond)
883 PtyChardev *s = PTY_CHARDEV(chr);
887 return qio_channel_create_watch(s->ioc, cond);
890 static int pty_chr_read_poll(void *opaque)
892 Chardev *chr = CHARDEV(opaque);
893 PtyChardev *s = PTY_CHARDEV(opaque);
895 s->read_bytes = qemu_chr_be_can_write(chr);
896 return s->read_bytes;
899 static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
901 Chardev *chr = CHARDEV(opaque);
902 PtyChardev *s = PTY_CHARDEV(opaque);
904 uint8_t buf[CHR_READ_BUF_LEN];
908 if (len > s->read_bytes)
913 ret = qio_channel_read(s->ioc, (char *)buf, len, NULL);
915 pty_chr_state(chr, 0);
918 pty_chr_state(chr, 1);
919 qemu_chr_be_write(chr, buf, ret);
924 static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
926 Chardev *chr = CHARDEV(opaque);
927 PtyChardev *s = PTY_CHARDEV(opaque);
930 qemu_chr_be_generic_open(chr);
934 /* Called with chr_write_lock held. */
935 static void pty_chr_state(Chardev *chr, int connected)
937 PtyChardev *s = PTY_CHARDEV(chr);
941 g_source_remove(s->open_tag);
944 remove_fd_in_watch(chr);
946 /* (re-)connect poll interval for idle guests: once per second.
947 * We check more frequently in case the guests sends data to
948 * the virtual device linked to our pty. */
949 pty_chr_rearm_timer(chr, 1000);
952 g_source_remove(s->timer_tag);
956 g_assert(s->open_tag == 0);
958 s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
960 if (!chr->fd_in_tag) {
961 chr->fd_in_tag = io_add_watch_poll(chr, s->ioc,
969 static void char_pty_finalize(Object *obj)
971 Chardev *chr = CHARDEV(obj);
972 PtyChardev *s = PTY_CHARDEV(obj);
974 qemu_mutex_lock(&chr->chr_write_lock);
975 pty_chr_state(chr, 0);
976 object_unref(OBJECT(s->ioc));
978 g_source_remove(s->timer_tag);
981 qemu_mutex_unlock(&chr->chr_write_lock);
982 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
985 static void char_pty_open(Chardev *chr,
986 ChardevBackend *backend,
991 int master_fd, slave_fd;
992 char pty_name[PATH_MAX];
995 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
997 error_setg_errno(errp, errno, "Failed to create PTY");
1002 qemu_set_nonblock(master_fd);
1004 chr->filename = g_strdup_printf("pty:%s", pty_name);
1005 error_report("char device redirected to %s (label %s)",
1006 pty_name, chr->label);
1008 s = PTY_CHARDEV(chr);
1009 s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd));
1010 name = g_strdup_printf("chardev-pty-%s", chr->label);
1011 qio_channel_set_name(QIO_CHANNEL(s->ioc), name);
1017 static void char_pty_class_init(ObjectClass *oc, void *data)
1019 ChardevClass *cc = CHARDEV_CLASS(oc);
1021 cc->open = char_pty_open;
1022 cc->chr_write = char_pty_chr_write;
1023 cc->chr_update_read_handler = pty_chr_update_read_handler;
1024 cc->chr_add_watch = pty_chr_add_watch;
1027 static const TypeInfo char_pty_type_info = {
1028 .name = TYPE_CHARDEV_PTY,
1029 .parent = TYPE_CHARDEV,
1030 .instance_size = sizeof(PtyChardev),
1031 .instance_finalize = char_pty_finalize,
1032 .class_init = char_pty_class_init,
1035 static void tty_serial_init(int fd, int speed,
1036 int parity, int data_bits, int stop_bits)
1042 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1043 speed, parity, data_bits, stop_bits);
1045 tcgetattr (fd, &tty);
1047 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1048 speed = speed * 10 / 11;
1065 /* Non-Posix values follow. They may be unsupported on some systems. */
1067 check_speed(115200);
1069 check_speed(230400);
1072 check_speed(460800);
1075 check_speed(500000);
1078 check_speed(576000);
1081 check_speed(921600);
1084 check_speed(1000000);
1087 check_speed(1152000);
1090 check_speed(1500000);
1093 check_speed(2000000);
1096 check_speed(2500000);
1099 check_speed(3000000);
1102 check_speed(3500000);
1105 check_speed(4000000);
1110 cfsetispeed(&tty, spd);
1111 cfsetospeed(&tty, spd);
1113 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1114 |INLCR|IGNCR|ICRNL|IXON);
1115 tty.c_oflag |= OPOST;
1116 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1117 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1138 tty.c_cflag |= PARENB;
1141 tty.c_cflag |= PARENB | PARODD;
1145 tty.c_cflag |= CSTOPB;
1147 tcsetattr (fd, TCSANOW, &tty);
1150 static int tty_serial_ioctl(Chardev *chr, int cmd, void *arg)
1152 FDChardev *s = FD_CHARDEV(chr);
1153 QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc_in);
1156 case CHR_IOCTL_SERIAL_SET_PARAMS:
1158 QEMUSerialSetParams *ssp = arg;
1159 tty_serial_init(fioc->fd,
1160 ssp->speed, ssp->parity,
1161 ssp->data_bits, ssp->stop_bits);
1164 case CHR_IOCTL_SERIAL_SET_BREAK:
1166 int enable = *(int *)arg;
1168 tcsendbreak(fioc->fd, 1);
1172 case CHR_IOCTL_SERIAL_GET_TIOCM:
1175 int *targ = (int *)arg;
1176 ioctl(fioc->fd, TIOCMGET, &sarg);
1178 if (sarg & TIOCM_CTS)
1179 *targ |= CHR_TIOCM_CTS;
1180 if (sarg & TIOCM_CAR)
1181 *targ |= CHR_TIOCM_CAR;
1182 if (sarg & TIOCM_DSR)
1183 *targ |= CHR_TIOCM_DSR;
1184 if (sarg & TIOCM_RI)
1185 *targ |= CHR_TIOCM_RI;
1186 if (sarg & TIOCM_DTR)
1187 *targ |= CHR_TIOCM_DTR;
1188 if (sarg & TIOCM_RTS)
1189 *targ |= CHR_TIOCM_RTS;
1192 case CHR_IOCTL_SERIAL_SET_TIOCM:
1194 int sarg = *(int *)arg;
1196 ioctl(fioc->fd, TIOCMGET, &targ);
1197 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1198 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1199 if (sarg & CHR_TIOCM_CTS)
1201 if (sarg & CHR_TIOCM_CAR)
1203 if (sarg & CHR_TIOCM_DSR)
1205 if (sarg & CHR_TIOCM_RI)
1207 if (sarg & CHR_TIOCM_DTR)
1209 if (sarg & CHR_TIOCM_RTS)
1211 ioctl(fioc->fd, TIOCMSET, &targ);
1219 #endif /* __linux__ || __sun__ */
1221 #if defined(__linux__)
1223 #define HAVE_CHARDEV_PARPORT 1
1231 #define PARALLEL_CHARDEV(obj) \
1232 OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL)
1234 static int pp_hw_mode(ParallelChardev *s, uint16_t mode)
1236 if (s->mode != mode) {
1238 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1245 static int pp_ioctl(Chardev *chr, int cmd, void *arg)
1247 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
1252 case CHR_IOCTL_PP_READ_DATA:
1253 if (ioctl(fd, PPRDATA, &b) < 0)
1255 *(uint8_t *)arg = b;
1257 case CHR_IOCTL_PP_WRITE_DATA:
1258 b = *(uint8_t *)arg;
1259 if (ioctl(fd, PPWDATA, &b) < 0)
1262 case CHR_IOCTL_PP_READ_CONTROL:
1263 if (ioctl(fd, PPRCONTROL, &b) < 0)
1265 /* Linux gives only the lowest bits, and no way to know data
1266 direction! For better compatibility set the fixed upper
1268 *(uint8_t *)arg = b | 0xc0;
1270 case CHR_IOCTL_PP_WRITE_CONTROL:
1271 b = *(uint8_t *)arg;
1272 if (ioctl(fd, PPWCONTROL, &b) < 0)
1275 case CHR_IOCTL_PP_READ_STATUS:
1276 if (ioctl(fd, PPRSTATUS, &b) < 0)
1278 *(uint8_t *)arg = b;
1280 case CHR_IOCTL_PP_DATA_DIR:
1281 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1284 case CHR_IOCTL_PP_EPP_READ_ADDR:
1285 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1286 struct ParallelIOArg *parg = arg;
1287 int n = read(fd, parg->buffer, parg->count);
1288 if (n != parg->count) {
1293 case CHR_IOCTL_PP_EPP_READ:
1294 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1295 struct ParallelIOArg *parg = arg;
1296 int n = read(fd, parg->buffer, parg->count);
1297 if (n != parg->count) {
1302 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1303 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1304 struct ParallelIOArg *parg = arg;
1305 int n = write(fd, parg->buffer, parg->count);
1306 if (n != parg->count) {
1311 case CHR_IOCTL_PP_EPP_WRITE:
1312 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1313 struct ParallelIOArg *parg = arg;
1314 int n = write(fd, parg->buffer, parg->count);
1315 if (n != parg->count) {
1326 static void qemu_chr_open_pp_fd(Chardev *chr,
1331 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
1333 if (ioctl(fd, PPCLAIM) < 0) {
1334 error_setg_errno(errp, errno, "not a parallel port");
1340 drv->mode = IEEE1284_MODE_COMPAT;
1342 #endif /* __linux__ */
1344 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1346 #define HAVE_CHARDEV_PARPORT 1
1353 #define PARALLEL_CHARDEV(obj) \
1354 OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL)
1356 static int pp_ioctl(Chardev *chr, int cmd, void *arg)
1358 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
1362 case CHR_IOCTL_PP_READ_DATA:
1363 if (ioctl(drv->fd, PPIGDATA, &b) < 0) {
1366 *(uint8_t *)arg = b;
1368 case CHR_IOCTL_PP_WRITE_DATA:
1369 b = *(uint8_t *)arg;
1370 if (ioctl(drv->fd, PPISDATA, &b) < 0) {
1374 case CHR_IOCTL_PP_READ_CONTROL:
1375 if (ioctl(drv->fd, PPIGCTRL, &b) < 0) {
1378 *(uint8_t *)arg = b;
1380 case CHR_IOCTL_PP_WRITE_CONTROL:
1381 b = *(uint8_t *)arg;
1382 if (ioctl(drv->fd, PPISCTRL, &b) < 0) {
1386 case CHR_IOCTL_PP_READ_STATUS:
1387 if (ioctl(drv->fd, PPIGSTATUS, &b) < 0) {
1390 *(uint8_t *)arg = b;
1398 static void qemu_chr_open_pp_fd(Chardev *chr,
1403 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
1411 #define HAVE_CHARDEV_SERIAL 1
1413 #define MAXCONNECT 1
1414 #define NTIMEOUT 5000
1416 static int win_chr_pipe_init(Chardev *chr, const char *filename,
1419 WinChardev *s = WIN_CHARDEV(chr);
1427 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1429 error_setg(errp, "Failed CreateEvent");
1432 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1434 error_setg(errp, "Failed CreateEvent");
1438 openname = g_strdup_printf("\\\\.\\pipe\\%s", filename);
1439 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1440 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1442 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1444 if (s->hcom == INVALID_HANDLE_VALUE) {
1445 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
1450 ZeroMemory(&ov, sizeof(ov));
1451 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1452 ret = ConnectNamedPipe(s->hcom, &ov);
1454 error_setg(errp, "Failed ConnectNamedPipe");
1458 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1460 error_setg(errp, "Failed GetOverlappedResult");
1462 CloseHandle(ov.hEvent);
1469 CloseHandle(ov.hEvent);
1472 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1480 static void qemu_chr_open_pipe(Chardev *chr,
1481 ChardevBackend *backend,
1485 ChardevHostdev *opts = backend->u.pipe.data;
1486 const char *filename = opts->device;
1488 if (win_chr_pipe_init(chr, filename, errp) < 0) {
1493 static void qemu_chr_open_win_con(Chardev *chr,
1494 ChardevBackend *backend,
1498 qemu_chr_open_win_file(chr, GetStdHandle(STD_OUTPUT_HANDLE));
1501 static void char_console_class_init(ObjectClass *oc, void *data)
1503 ChardevClass *cc = CHARDEV_CLASS(oc);
1505 cc->open = qemu_chr_open_win_con;
1508 static const TypeInfo char_console_type_info = {
1509 .name = TYPE_CHARDEV_CONSOLE,
1510 .parent = TYPE_CHARDEV_WIN,
1511 .class_init = char_console_class_init,
1514 #endif /* !_WIN32 */
1516 int qemu_chr_wait_connected(Chardev *chr, Error **errp)
1518 ChardevClass *cc = CHARDEV_GET_CLASS(chr);
1520 if (cc->chr_wait_connected) {
1521 return cc->chr_wait_connected(chr, errp);
1527 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)
1530 error_setg(errp, "missing associated backend");
1534 return qemu_chr_wait_connected(be->chr, errp);
1537 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
1539 char host[65], port[33], width[8], height[8];
1543 Error *local_err = NULL;
1545 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
1547 error_report_err(local_err);
1551 if (strstart(filename, "mon:", &p)) {
1553 qemu_opt_set(opts, "mux", "on", &error_abort);
1554 if (strcmp(filename, "stdio") == 0) {
1555 /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
1556 * but pass it to the guest. Handle this only for compat syntax,
1557 * for -chardev syntax we have special option for this.
1558 * This is what -nographic did, redirecting+muxing serial+monitor
1559 * to stdio causing Ctrl+C to be passed to guest. */
1560 qemu_opt_set(opts, "signal", "off", &error_abort);
1564 if (strcmp(filename, "null") == 0 ||
1565 strcmp(filename, "pty") == 0 ||
1566 strcmp(filename, "msmouse") == 0 ||
1567 strcmp(filename, "braille") == 0 ||
1568 strcmp(filename, "testdev") == 0 ||
1569 strcmp(filename, "stdio") == 0) {
1570 qemu_opt_set(opts, "backend", filename, &error_abort);
1573 if (strstart(filename, "vc", &p)) {
1574 qemu_opt_set(opts, "backend", "vc", &error_abort);
1576 if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
1578 qemu_opt_set(opts, "width", width, &error_abort);
1579 qemu_opt_set(opts, "height", height, &error_abort);
1580 } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
1582 qemu_opt_set(opts, "cols", width, &error_abort);
1583 qemu_opt_set(opts, "rows", height, &error_abort);
1590 if (strcmp(filename, "con:") == 0) {
1591 qemu_opt_set(opts, "backend", "console", &error_abort);
1594 if (strstart(filename, "COM", NULL)) {
1595 qemu_opt_set(opts, "backend", "serial", &error_abort);
1596 qemu_opt_set(opts, "path", filename, &error_abort);
1599 if (strstart(filename, "file:", &p)) {
1600 qemu_opt_set(opts, "backend", "file", &error_abort);
1601 qemu_opt_set(opts, "path", p, &error_abort);
1604 if (strstart(filename, "pipe:", &p)) {
1605 qemu_opt_set(opts, "backend", "pipe", &error_abort);
1606 qemu_opt_set(opts, "path", p, &error_abort);
1609 if (strstart(filename, "tcp:", &p) ||
1610 strstart(filename, "telnet:", &p)) {
1611 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
1613 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
1616 qemu_opt_set(opts, "backend", "socket", &error_abort);
1617 qemu_opt_set(opts, "host", host, &error_abort);
1618 qemu_opt_set(opts, "port", port, &error_abort);
1619 if (p[pos] == ',') {
1620 qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err);
1622 error_report_err(local_err);
1626 if (strstart(filename, "telnet:", &p))
1627 qemu_opt_set(opts, "telnet", "on", &error_abort);
1630 if (strstart(filename, "udp:", &p)) {
1631 qemu_opt_set(opts, "backend", "udp", &error_abort);
1632 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
1634 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
1638 qemu_opt_set(opts, "host", host, &error_abort);
1639 qemu_opt_set(opts, "port", port, &error_abort);
1640 if (p[pos] == '@') {
1642 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
1644 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
1648 qemu_opt_set(opts, "localaddr", host, &error_abort);
1649 qemu_opt_set(opts, "localport", port, &error_abort);
1653 if (strstart(filename, "unix:", &p)) {
1654 qemu_opt_set(opts, "backend", "socket", &error_abort);
1655 qemu_opts_do_parse(opts, p, "path", &local_err);
1657 error_report_err(local_err);
1662 if (strstart(filename, "/dev/parport", NULL) ||
1663 strstart(filename, "/dev/ppi", NULL)) {
1664 qemu_opt_set(opts, "backend", "parport", &error_abort);
1665 qemu_opt_set(opts, "path", filename, &error_abort);
1668 if (strstart(filename, "/dev/", NULL)) {
1669 qemu_opt_set(opts, "backend", "tty", &error_abort);
1670 qemu_opt_set(opts, "path", filename, &error_abort);
1675 qemu_opts_del(opts);
1679 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend)
1681 const char *logfile = qemu_opt_get(opts, "logfile");
1683 backend->has_logfile = logfile != NULL;
1684 backend->logfile = logfile ? g_strdup(logfile) : NULL;
1686 backend->has_logappend = true;
1687 backend->logappend = qemu_opt_get_bool(opts, "logappend", false);
1691 static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
1694 const char *path = qemu_opt_get(opts, "path");
1697 backend->type = CHARDEV_BACKEND_KIND_FILE;
1699 error_setg(errp, "chardev: file: no filename given");
1702 file = backend->u.file.data = g_new0(ChardevFile, 1);
1703 qemu_chr_parse_common(opts, qapi_ChardevFile_base(file));
1704 file->out = g_strdup(path);
1706 file->has_append = true;
1707 file->append = qemu_opt_get_bool(opts, "append", false);
1710 static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
1713 ChardevStdio *stdio;
1715 backend->type = CHARDEV_BACKEND_KIND_STDIO;
1716 stdio = backend->u.stdio.data = g_new0(ChardevStdio, 1);
1717 qemu_chr_parse_common(opts, qapi_ChardevStdio_base(stdio));
1718 stdio->has_signal = true;
1719 stdio->signal = qemu_opt_get_bool(opts, "signal", true);
1722 static void char_stdio_class_init(ObjectClass *oc, void *data)
1724 ChardevClass *cc = CHARDEV_CLASS(oc);
1726 cc->parse = qemu_chr_parse_stdio;
1728 cc->open = qemu_chr_open_stdio;
1729 cc->chr_set_echo = qemu_chr_set_echo_stdio;
1733 static const TypeInfo char_stdio_type_info = {
1734 .name = TYPE_CHARDEV_STDIO,
1736 .parent = TYPE_CHARDEV_WIN_STDIO,
1738 .parent = TYPE_CHARDEV_FD,
1739 .instance_finalize = char_stdio_finalize,
1741 .class_init = char_stdio_class_init,
1744 #ifdef HAVE_CHARDEV_SERIAL
1745 static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
1748 const char *device = qemu_opt_get(opts, "path");
1749 ChardevHostdev *serial;
1751 backend->type = CHARDEV_BACKEND_KIND_SERIAL;
1752 if (device == NULL) {
1753 error_setg(errp, "chardev: serial/tty: no device path given");
1756 serial = backend->u.serial.data = g_new0(ChardevHostdev, 1);
1757 qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(serial));
1758 serial->device = g_strdup(device);
1762 #ifdef HAVE_CHARDEV_PARPORT
1763 static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
1766 const char *device = qemu_opt_get(opts, "path");
1767 ChardevHostdev *parallel;
1769 backend->type = CHARDEV_BACKEND_KIND_PARALLEL;
1770 if (device == NULL) {
1771 error_setg(errp, "chardev: parallel: no device path given");
1774 parallel = backend->u.parallel.data = g_new0(ChardevHostdev, 1);
1775 qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(parallel));
1776 parallel->device = g_strdup(device);
1780 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
1783 const char *device = qemu_opt_get(opts, "path");
1784 ChardevHostdev *dev;
1786 backend->type = CHARDEV_BACKEND_KIND_PIPE;
1787 if (device == NULL) {
1788 error_setg(errp, "chardev: pipe: no device path given");
1791 dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1);
1792 qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev));
1793 dev->device = g_strdup(device);
1796 static void char_pipe_class_init(ObjectClass *oc, void *data)
1798 ChardevClass *cc = CHARDEV_CLASS(oc);
1800 cc->parse = qemu_chr_parse_pipe;
1801 cc->open = qemu_chr_open_pipe;
1804 static const TypeInfo char_pipe_type_info = {
1805 .name = TYPE_CHARDEV_PIPE,
1807 .parent = TYPE_CHARDEV_WIN,
1809 .parent = TYPE_CHARDEV_FD,
1811 .class_init = char_pipe_class_init,
1814 static const ChardevClass *char_get_class(const char *driver, Error **errp)
1817 const ChardevClass *cc;
1818 char *typename = g_strdup_printf("chardev-%s", driver);
1820 oc = object_class_by_name(typename);
1823 if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) {
1824 error_setg(errp, "'%s' is not a valid char driver name", driver);
1828 if (object_class_is_abstract(oc)) {
1829 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
1830 "abstract device type");
1834 cc = CHARDEV_CLASS(oc);
1836 error_setg(errp, "'%s' is not a valid char driver name", driver);
1843 static Chardev *qemu_chardev_add(const char *id, const char *typename,
1844 ChardevBackend *backend, Error **errp)
1848 chr = qemu_chr_find(id);
1850 error_setg(errp, "Chardev '%s' already exists", id);
1854 chr = qemu_chardev_new(id, typename, backend, errp);
1859 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
1863 static const struct ChardevAlias {
1864 const char *typename;
1866 } chardev_alias_table[] = {
1867 #ifdef HAVE_CHARDEV_PARPORT
1868 { "parallel", "parport" },
1870 #ifdef HAVE_CHARDEV_SERIAL
1871 { "serial", "tty" },
1875 typedef struct ChadevClassFE {
1876 void (*fn)(const char *name, void *opaque);
1881 chardev_class_foreach(ObjectClass *klass, void *opaque)
1883 ChadevClassFE *fe = opaque;
1885 assert(g_str_has_prefix(object_class_get_name(klass), "chardev-"));
1886 if (CHARDEV_CLASS(klass)->internal) {
1890 fe->fn(object_class_get_name(klass) + 8, fe->opaque);
1894 chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque)
1896 ChadevClassFE fe = { .fn = fn, .opaque = opaque };
1899 object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe);
1901 for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) {
1902 fn(chardev_alias_table[i].alias, opaque);
1907 help_string_append(const char *name, void *opaque)
1909 GString *str = opaque;
1911 g_string_append_printf(str, "\n%s", name);
1914 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
1917 Error *local_err = NULL;
1918 const ChardevClass *cc;
1921 ChardevBackend *backend = NULL;
1922 const char *name = qemu_opt_get(opts, "backend");
1923 const char *id = qemu_opts_id(opts);
1927 error_setg(errp, "chardev: \"%s\" missing backend",
1928 qemu_opts_id(opts));
1932 if (is_help_option(name)) {
1933 GString *str = g_string_new("");
1935 chardev_name_foreach(help_string_append, str);
1937 error_report("Available chardev backend types: %s", str->str);
1938 g_string_free(str, true);
1943 error_setg(errp, "chardev: no id specified");
1947 for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) {
1948 if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) {
1949 name = chardev_alias_table[i].typename;
1954 cc = char_get_class(name, errp);
1959 backend = g_new0(ChardevBackend, 1);
1960 backend->type = CHARDEV_BACKEND_KIND_NULL;
1962 if (qemu_opt_get_bool(opts, "mux", 0)) {
1963 bid = g_strdup_printf("%s-base", id);
1968 cc->parse(opts, backend, &local_err);
1970 error_propagate(errp, local_err);
1974 ChardevCommon *ccom = g_new0(ChardevCommon, 1);
1975 qemu_chr_parse_common(opts, ccom);
1976 backend->u.null.data = ccom; /* Any ChardevCommon member would work */
1979 chr = qemu_chardev_add(bid ? bid : id,
1980 object_class_get_name(OBJECT_CLASS(cc)),
1988 qapi_free_ChardevBackend(backend);
1989 backend = g_new0(ChardevBackend, 1);
1990 backend->type = CHARDEV_BACKEND_KIND_MUX;
1991 backend->u.mux.data = g_new0(ChardevMux, 1);
1992 backend->u.mux.data->chardev = g_strdup(bid);
1993 mux = qemu_chardev_add(id, TYPE_CHARDEV_MUX, backend, errp);
1995 qemu_chr_delete(chr);
2003 qapi_free_ChardevBackend(backend);
2008 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename)
2015 if (strstart(filename, "chardev:", &p)) {
2016 return qemu_chr_find(p);
2019 opts = qemu_chr_parse_compat(label, filename);
2023 chr = qemu_chr_new_from_opts(opts, &err);
2025 error_report_err(err);
2027 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2028 monitor_init(chr, MONITOR_USE_READLINE);
2030 qemu_opts_del(opts);
2034 Chardev *qemu_chr_new(const char *label, const char *filename)
2037 chr = qemu_chr_new_noreplay(label, filename);
2039 if (replay_mode != REPLAY_MODE_NONE) {
2040 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
2042 if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) {
2043 error_report("Replay: ioctl is not supported "
2044 "for serial devices yet");
2046 replay_register_char_driver(chr);
2051 void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
2053 Chardev *chr = be->chr;
2055 if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) {
2056 CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo);
2060 void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
2062 Chardev *chr = be->chr;
2068 if (be->fe_open == fe_open) {
2071 be->fe_open = fe_open;
2072 if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) {
2073 CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open);
2077 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
2078 GIOFunc func, void *user_data)
2080 Chardev *s = be->chr;
2084 if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) {
2088 src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond);
2093 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
2094 tag = g_source_attach(src, NULL);
2095 g_source_unref(src);
2100 void qemu_chr_fe_disconnect(CharBackend *be)
2102 Chardev *chr = be->chr;
2104 if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) {
2105 CHARDEV_GET_CLASS(chr)->chr_disconnect(chr);
2109 void qemu_chr_delete(Chardev *chr)
2111 QTAILQ_REMOVE(&chardevs, chr, next);
2112 object_unref(OBJECT(chr));
2115 ChardevInfoList *qmp_query_chardev(Error **errp)
2117 ChardevInfoList *chr_list = NULL;
2120 QTAILQ_FOREACH(chr, &chardevs, next) {
2121 ChardevInfoList *info = g_malloc0(sizeof(*info));
2122 info->value = g_malloc0(sizeof(*info->value));
2123 info->value->label = g_strdup(chr->label);
2124 info->value->filename = g_strdup(chr->filename);
2125 info->value->frontend_open = chr->be && chr->be->fe_open;
2127 info->next = chr_list;
2135 qmp_prepend_backend(const char *name, void *opaque)
2137 ChardevBackendInfoList **list = opaque;
2138 ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
2140 info->value = g_malloc0(sizeof(*info->value));
2141 info->value->name = g_strdup(name);
2146 ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
2148 ChardevBackendInfoList *backend_list = NULL;
2150 chardev_name_foreach(qmp_prepend_backend, &backend_list);
2152 return backend_list;
2155 Chardev *qemu_chr_find(const char *name)
2159 QTAILQ_FOREACH(chr, &chardevs, next) {
2160 if (strcmp(chr->label, name) != 0)
2167 QemuOptsList qemu_chardev_opts = {
2169 .implied_opt_name = "backend",
2170 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
2174 .type = QEMU_OPT_STRING,
2177 .type = QEMU_OPT_STRING,
2180 .type = QEMU_OPT_STRING,
2183 .type = QEMU_OPT_STRING,
2185 .name = "localaddr",
2186 .type = QEMU_OPT_STRING,
2188 .name = "localport",
2189 .type = QEMU_OPT_STRING,
2192 .type = QEMU_OPT_NUMBER,
2195 .type = QEMU_OPT_BOOL,
2198 .type = QEMU_OPT_BOOL,
2201 .type = QEMU_OPT_BOOL,
2204 .type = QEMU_OPT_BOOL,
2207 .type = QEMU_OPT_BOOL,
2209 .name = "reconnect",
2210 .type = QEMU_OPT_NUMBER,
2213 .type = QEMU_OPT_BOOL,
2215 .name = "tls-creds",
2216 .type = QEMU_OPT_STRING,
2219 .type = QEMU_OPT_NUMBER,
2222 .type = QEMU_OPT_NUMBER,
2225 .type = QEMU_OPT_NUMBER,
2228 .type = QEMU_OPT_NUMBER,
2231 .type = QEMU_OPT_BOOL,
2234 .type = QEMU_OPT_BOOL,
2237 .type = QEMU_OPT_STRING,
2240 .type = QEMU_OPT_NUMBER,
2243 .type = QEMU_OPT_SIZE,
2246 .type = QEMU_OPT_STRING,
2249 .type = QEMU_OPT_BOOL,
2252 .type = QEMU_OPT_STRING,
2254 .name = "logappend",
2255 .type = QEMU_OPT_BOOL,
2257 { /* end of list */ }
2263 static void qmp_chardev_open_file(Chardev *chr,
2264 ChardevBackend *backend,
2268 ChardevFile *file = backend->u.file.data;
2274 error_setg(errp, "input file not supported");
2278 if (file->has_append && file->append) {
2279 /* Append to file if it already exists. */
2280 accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
2281 flags = OPEN_ALWAYS;
2283 /* Truncate file if it already exists. */
2284 accessmode = GENERIC_WRITE;
2285 flags = CREATE_ALWAYS;
2288 out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
2289 FILE_ATTRIBUTE_NORMAL, NULL);
2290 if (out == INVALID_HANDLE_VALUE) {
2291 error_setg(errp, "open %s failed", file->out);
2295 qemu_chr_open_win_file(chr, out);
2298 static void qmp_chardev_open_serial(Chardev *chr,
2299 ChardevBackend *backend,
2303 ChardevHostdev *serial = backend->u.serial.data;
2305 win_chr_init(chr, serial->device, errp);
2310 static void qmp_chardev_open_file(Chardev *chr,
2311 ChardevBackend *backend,
2315 ChardevFile *file = backend->u.file.data;
2316 int flags, in = -1, out;
2318 flags = O_WRONLY | O_CREAT | O_BINARY;
2319 if (file->has_append && file->append) {
2325 out = qmp_chardev_open_file_source(file->out, flags, errp);
2332 in = qmp_chardev_open_file_source(file->in, flags, errp);
2339 qemu_chr_open_fd(chr, in, out);
2342 #ifdef HAVE_CHARDEV_SERIAL
2343 static void qmp_chardev_open_serial(Chardev *chr,
2344 ChardevBackend *backend,
2348 ChardevHostdev *serial = backend->u.serial.data;
2351 fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
2355 qemu_set_nonblock(fd);
2356 tty_serial_init(fd, 115200, 'N', 8, 1);
2358 qemu_chr_open_fd(chr, fd, fd);
2362 #ifdef HAVE_CHARDEV_PARPORT
2363 static void qmp_chardev_open_parallel(Chardev *chr,
2364 ChardevBackend *backend,
2368 ChardevHostdev *parallel = backend->u.parallel.data;
2371 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
2375 qemu_chr_open_pp_fd(chr, fd, be_opened, errp);
2378 static void char_parallel_class_init(ObjectClass *oc, void *data)
2380 ChardevClass *cc = CHARDEV_CLASS(oc);
2382 cc->parse = qemu_chr_parse_parallel;
2383 cc->open = qmp_chardev_open_parallel;
2384 #if defined(__linux__)
2385 cc->chr_ioctl = pp_ioctl;
2386 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
2387 cc->chr_ioctl = pp_ioctl;
2391 static void char_parallel_finalize(Object *obj)
2393 #if defined(__linux__)
2394 Chardev *chr = CHARDEV(obj);
2395 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
2398 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2399 ioctl(fd, PPRELEASE);
2401 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
2402 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
2403 /* FIXME: close fd? */
2407 static const TypeInfo char_parallel_type_info = {
2408 .name = TYPE_CHARDEV_PARALLEL,
2409 .parent = TYPE_CHARDEV,
2410 .instance_size = sizeof(ParallelChardev),
2411 .instance_finalize = char_parallel_finalize,
2412 .class_init = char_parallel_class_init,
2418 static void char_file_class_init(ObjectClass *oc, void *data)
2420 ChardevClass *cc = CHARDEV_CLASS(oc);
2422 cc->parse = qemu_chr_parse_file_out;
2423 cc->open = qmp_chardev_open_file;
2426 static const TypeInfo char_file_type_info = {
2427 .name = TYPE_CHARDEV_FILE,
2429 .parent = TYPE_CHARDEV_WIN,
2431 .parent = TYPE_CHARDEV_FD,
2433 .class_init = char_file_class_init,
2436 #ifdef HAVE_CHARDEV_SERIAL
2438 static void char_serial_class_init(ObjectClass *oc, void *data)
2440 ChardevClass *cc = CHARDEV_CLASS(oc);
2442 cc->parse = qemu_chr_parse_serial;
2443 cc->open = qmp_chardev_open_serial;
2445 cc->chr_ioctl = tty_serial_ioctl;
2449 static const TypeInfo char_serial_type_info = {
2450 .name = TYPE_CHARDEV_SERIAL,
2452 .parent = TYPE_CHARDEV_WIN,
2454 .parent = TYPE_CHARDEV_FD,
2456 .class_init = char_serial_class_init,
2460 bool qemu_chr_has_feature(Chardev *chr,
2461 ChardevFeature feature)
2463 return test_bit(feature, chr->features);
2466 void qemu_chr_set_feature(Chardev *chr,
2467 ChardevFeature feature)
2469 return set_bit(feature, chr->features);
2472 Chardev *qemu_chardev_new(const char *id, const char *typename,
2473 ChardevBackend *backend, Error **errp)
2475 Chardev *chr = NULL;
2476 Error *local_err = NULL;
2477 bool be_opened = true;
2479 assert(g_str_has_prefix(typename, "chardev-"));
2481 chr = CHARDEV(object_new(typename));
2482 chr->label = g_strdup(id);
2484 qemu_char_open(chr, backend, &be_opened, &local_err);
2486 error_propagate(errp, local_err);
2487 object_unref(OBJECT(chr));
2491 if (!chr->filename) {
2492 chr->filename = g_strdup(typename + 8);
2495 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
2501 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
2504 const ChardevClass *cc;
2508 cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp);
2513 chr = qemu_chardev_add(id, object_class_get_name(OBJECT_CLASS(cc)),
2519 ret = g_new0(ChardevReturn, 1);
2520 if (CHARDEV_IS_PTY(chr)) {
2521 ret->pty = g_strdup(chr->filename + 4);
2522 ret->has_pty = true;
2528 void qmp_chardev_remove(const char *id, Error **errp)
2532 chr = qemu_chr_find(id);
2534 error_setg(errp, "Chardev '%s' not found", id);
2537 if (qemu_chr_is_busy(chr)) {
2538 error_setg(errp, "Chardev '%s' is busy", id);
2541 if (qemu_chr_replay(chr)) {
2543 "Chardev '%s' cannot be unplugged in record/replay mode", id);
2546 qemu_chr_delete(chr);
2549 void qemu_chr_cleanup(void)
2553 QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) {
2554 qemu_chr_delete(chr);
2558 static void register_types(void)
2560 type_register_static(&char_type_info);
2561 type_register_static(&char_file_type_info);
2562 type_register_static(&char_stdio_type_info);
2563 #ifdef HAVE_CHARDEV_SERIAL
2564 type_register_static(&char_serial_type_info);
2566 #ifdef HAVE_CHARDEV_PARPORT
2567 type_register_static(&char_parallel_type_info);
2569 #ifdef HAVE_CHARDEV_PTY
2570 type_register_static(&char_pty_type_info);
2573 type_register_static(&char_console_type_info);
2575 type_register_static(&char_pipe_type_info);
2577 /* this must be done after machine init, since we register FEs with muxes
2578 * as part of realize functions like serial_isa_realizefn when -nographic
2581 qemu_add_machine_init_done_notifier(&muxes_realize_notify);
2584 type_init(register_types);