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-common.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
34 #include "hw/msmouse.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
55 #include <net/if_tap.h>
58 #include <linux/if_tun.h>
60 #include <arpa/inet.h>
63 #include <sys/select.h>
68 #include <dev/ppbus/ppi.h>
69 #include <dev/ppbus/ppbconf.h>
70 #elif defined(__DragonFly__)
72 #include <dev/misc/ppi/ppi.h>
73 #include <bus/ppbus/ppbconf.h>
77 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
78 #include <freebsd/stdlib.h>
83 #include <linux/ppdev.h>
84 #include <linux/parport.h>
88 #include <sys/ethernet.h>
89 #include <sys/sockio.h>
90 #include <netinet/arp.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_icmp.h> // must come after ip.h
95 #include <netinet/udp.h>
96 #include <netinet/tcp.h>
104 #include "qemu_socket.h"
106 /***********************************************************/
107 /* character device */
109 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
110 TAILQ_HEAD_INITIALIZER(chardevs);
111 static int initial_reset_issued;
113 static void qemu_chr_event(CharDriverState *s, int event)
117 s->chr_event(s->handler_opaque, event);
120 static void qemu_chr_reset_bh(void *opaque)
122 CharDriverState *s = opaque;
123 qemu_chr_event(s, CHR_EVENT_RESET);
124 qemu_bh_delete(s->bh);
128 void qemu_chr_reset(CharDriverState *s)
130 if (s->bh == NULL && initial_reset_issued) {
131 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
132 qemu_bh_schedule(s->bh);
136 void qemu_chr_initial_reset(void)
138 CharDriverState *chr;
140 initial_reset_issued = 1;
142 TAILQ_FOREACH(chr, &chardevs, next) {
147 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
149 return s->chr_write(s, buf, len);
152 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
156 return s->chr_ioctl(s, cmd, arg);
159 int qemu_chr_can_read(CharDriverState *s)
161 if (!s->chr_can_read)
163 return s->chr_can_read(s->handler_opaque);
166 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
168 s->chr_read(s->handler_opaque, buf, len);
171 int qemu_chr_get_msgfd(CharDriverState *s)
173 return s->get_msgfd ? s->get_msgfd(s) : -1;
176 void qemu_chr_accept_input(CharDriverState *s)
178 if (s->chr_accept_input)
179 s->chr_accept_input(s);
182 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
187 vsnprintf(buf, sizeof(buf), fmt, ap);
188 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
192 void qemu_chr_send_event(CharDriverState *s, int event)
194 if (s->chr_send_event)
195 s->chr_send_event(s, event);
198 void qemu_chr_add_handlers(CharDriverState *s,
199 IOCanRWHandler *fd_can_read,
200 IOReadHandler *fd_read,
201 IOEventHandler *fd_event,
204 s->chr_can_read = fd_can_read;
205 s->chr_read = fd_read;
206 s->chr_event = fd_event;
207 s->handler_opaque = opaque;
208 if (s->chr_update_read_handler)
209 s->chr_update_read_handler(s);
212 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
217 static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
219 CharDriverState *chr;
221 chr = qemu_mallocz(sizeof(CharDriverState));
222 chr->chr_write = null_chr_write;
226 /* MUX driver for serial I/O splitting */
228 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
229 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
231 IOCanRWHandler *chr_can_read[MAX_MUX];
232 IOReadHandler *chr_read[MAX_MUX];
233 IOEventHandler *chr_event[MAX_MUX];
234 void *ext_opaque[MAX_MUX];
235 CharDriverState *drv;
239 /* Intermediate input buffer allows to catch escape sequences even if the
240 currently active device is not accepting any input - but only until it
242 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
247 int64_t timestamps_start;
251 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
253 MuxDriver *d = chr->opaque;
255 if (!d->timestamps) {
256 ret = d->drv->chr_write(d->drv, buf, len);
261 for (i = 0; i < len; i++) {
267 ti = qemu_get_clock(rt_clock);
268 if (d->timestamps_start == -1)
269 d->timestamps_start = ti;
270 ti -= d->timestamps_start;
272 snprintf(buf1, sizeof(buf1),
273 "[%02d:%02d:%02d.%03d] ",
278 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
281 ret += d->drv->chr_write(d->drv, buf+i, 1);
282 if (buf[i] == '\n') {
290 static const char * const mux_help[] = {
291 "% h print this help\n\r",
292 "% x exit emulator\n\r",
293 "% s save disk data back to file (if -snapshot)\n\r",
294 "% t toggle console timestamps\n\r"
295 "% b send break (magic sysrq)\n\r",
296 "% c switch between console and monitor\n\r",
301 int term_escape_char = 0x01; /* ctrl-a is used for escape */
302 static void mux_print_help(CharDriverState *chr)
305 char ebuf[15] = "Escape-Char";
306 char cbuf[50] = "\n\r";
308 if (term_escape_char > 0 && term_escape_char < 26) {
309 snprintf(cbuf, sizeof(cbuf), "\n\r");
310 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
312 snprintf(cbuf, sizeof(cbuf),
313 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
316 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
317 for (i = 0; mux_help[i] != NULL; i++) {
318 for (j=0; mux_help[i][j] != '\0'; j++) {
319 if (mux_help[i][j] == '%')
320 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
322 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
327 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
329 if (d->chr_event[mux_nr])
330 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
333 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
335 if (d->term_got_escape) {
336 d->term_got_escape = 0;
337 if (ch == term_escape_char)
346 const char *term = "QEMU: Terminated\n\r";
347 chr->chr_write(chr,(uint8_t *)term,strlen(term));
354 TAILQ_FOREACH(dinfo, &drives, next) {
355 bdrv_commit(dinfo->bdrv);
360 qemu_chr_event(chr, CHR_EVENT_BREAK);
363 /* Switch to the next registered device */
364 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_OUT);
366 if (chr->focus >= d->mux_cnt)
368 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_IN);
371 d->timestamps = !d->timestamps;
372 d->timestamps_start = -1;
376 } else if (ch == term_escape_char) {
377 d->term_got_escape = 1;
385 static void mux_chr_accept_input(CharDriverState *chr)
388 MuxDriver *d = chr->opaque;
390 while (d->prod[m] != d->cons[m] &&
391 d->chr_can_read[m] &&
392 d->chr_can_read[m](d->ext_opaque[m])) {
393 d->chr_read[m](d->ext_opaque[m],
394 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
398 static int mux_chr_can_read(void *opaque)
400 CharDriverState *chr = opaque;
401 MuxDriver *d = chr->opaque;
404 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
406 if (d->chr_can_read[m])
407 return d->chr_can_read[m](d->ext_opaque[m]);
411 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
413 CharDriverState *chr = opaque;
414 MuxDriver *d = chr->opaque;
418 mux_chr_accept_input (opaque);
420 for(i = 0; i < size; i++)
421 if (mux_proc_byte(chr, d, buf[i])) {
422 if (d->prod[m] == d->cons[m] &&
423 d->chr_can_read[m] &&
424 d->chr_can_read[m](d->ext_opaque[m]))
425 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
427 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
431 static void mux_chr_event(void *opaque, int event)
433 CharDriverState *chr = opaque;
434 MuxDriver *d = chr->opaque;
437 /* Send the event to all registered listeners */
438 for (i = 0; i < d->mux_cnt; i++)
439 mux_chr_send_event(d, i, event);
442 static void mux_chr_update_read_handler(CharDriverState *chr)
444 MuxDriver *d = chr->opaque;
446 if (d->mux_cnt >= MAX_MUX) {
447 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
450 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
451 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
452 d->chr_read[d->mux_cnt] = chr->chr_read;
453 d->chr_event[d->mux_cnt] = chr->chr_event;
454 /* Fix up the real driver with mux routines */
455 if (d->mux_cnt == 0) {
456 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
459 chr->focus = d->mux_cnt;
463 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
465 CharDriverState *chr;
468 chr = qemu_mallocz(sizeof(CharDriverState));
469 d = qemu_mallocz(sizeof(MuxDriver));
474 chr->chr_write = mux_chr_write;
475 chr->chr_update_read_handler = mux_chr_update_read_handler;
476 chr->chr_accept_input = mux_chr_accept_input;
482 int send_all(int fd, const void *buf, int len1)
488 ret = send(fd, buf, len, 0);
490 errno = WSAGetLastError();
491 if (errno != WSAEWOULDBLOCK) {
494 } else if (ret == 0) {
506 static int unix_write(int fd, const uint8_t *buf, int len1)
512 ret = write(fd, buf, len);
514 if (errno != EINTR && errno != EAGAIN)
516 } else if (ret == 0) {
526 int send_all(int fd, const void *buf, int len1)
528 return unix_write(fd, buf, len1);
539 #define STDIO_MAX_CLIENTS 1
540 static int stdio_nb_clients = 0;
542 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
544 FDCharDriver *s = chr->opaque;
545 return send_all(s->fd_out, buf, len);
548 static int fd_chr_read_poll(void *opaque)
550 CharDriverState *chr = opaque;
551 FDCharDriver *s = chr->opaque;
553 s->max_size = qemu_chr_can_read(chr);
557 static void fd_chr_read(void *opaque)
559 CharDriverState *chr = opaque;
560 FDCharDriver *s = chr->opaque;
565 if (len > s->max_size)
569 size = read(s->fd_in, buf, len);
571 /* FD has been closed. Remove it from the active list. */
572 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
573 qemu_chr_event(chr, CHR_EVENT_CLOSED);
577 qemu_chr_read(chr, buf, size);
581 static void fd_chr_update_read_handler(CharDriverState *chr)
583 FDCharDriver *s = chr->opaque;
586 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
588 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
589 fd_chr_read, NULL, chr);
594 static void fd_chr_close(struct CharDriverState *chr)
596 FDCharDriver *s = chr->opaque;
599 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
601 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
606 qemu_chr_event(chr, CHR_EVENT_CLOSED);
609 /* open a character device to a unix fd */
610 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
612 CharDriverState *chr;
615 chr = qemu_mallocz(sizeof(CharDriverState));
616 s = qemu_mallocz(sizeof(FDCharDriver));
620 chr->chr_write = fd_chr_write;
621 chr->chr_update_read_handler = fd_chr_update_read_handler;
622 chr->chr_close = fd_chr_close;
629 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
633 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
636 return qemu_chr_open_fd(-1, fd_out);
639 static CharDriverState *qemu_chr_open_pipe(const char *filename)
642 char filename_in[256], filename_out[256];
644 snprintf(filename_in, 256, "%s.in", filename);
645 snprintf(filename_out, 256, "%s.out", filename);
646 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
647 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
648 if (fd_in < 0 || fd_out < 0) {
653 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
657 return qemu_chr_open_fd(fd_in, fd_out);
661 /* for STDIO, we handle the case where several clients use it
664 #define TERM_FIFO_MAX_SIZE 1
666 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
667 static int term_fifo_size;
669 static int stdio_read_poll(void *opaque)
671 CharDriverState *chr = opaque;
673 /* try to flush the queue if needed */
674 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
675 qemu_chr_read(chr, term_fifo, 1);
678 /* see if we can absorb more chars */
679 if (term_fifo_size == 0)
685 static void stdio_read(void *opaque)
689 CharDriverState *chr = opaque;
691 size = read(0, buf, 1);
693 /* stdin has been closed. Remove it from the active list. */
694 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
695 qemu_chr_event(chr, CHR_EVENT_CLOSED);
699 if (qemu_chr_can_read(chr) > 0) {
700 qemu_chr_read(chr, buf, 1);
701 } else if (term_fifo_size == 0) {
702 term_fifo[term_fifo_size++] = buf[0];
707 /* init terminal so that we can grab keys */
708 static struct termios oldtty;
709 static int old_fd0_flags;
710 static int term_atexit_done;
712 static void term_exit(void)
714 tcsetattr (0, TCSANOW, &oldtty);
715 fcntl(0, F_SETFL, old_fd0_flags);
718 static void term_init(void)
724 old_fd0_flags = fcntl(0, F_GETFL);
726 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
727 |INLCR|IGNCR|ICRNL|IXON);
728 tty.c_oflag |= OPOST;
729 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
730 /* if graphical mode, we allow Ctrl-C handling */
731 if (display_type == DT_NOGRAPHIC)
732 tty.c_lflag &= ~ISIG;
733 tty.c_cflag &= ~(CSIZE|PARENB);
738 tcsetattr (0, TCSANOW, &tty);
740 if (!term_atexit_done++)
743 fcntl(0, F_SETFL, O_NONBLOCK);
746 static void qemu_chr_close_stdio(struct CharDriverState *chr)
750 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
754 static CharDriverState *qemu_chr_open_stdio(void)
756 CharDriverState *chr;
758 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
760 chr = qemu_chr_open_fd(0, 1);
761 chr->chr_close = qemu_chr_close_stdio;
762 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
770 /* Once Solaris has openpty(), this is going to be removed. */
771 static int openpty(int *amaster, int *aslave, char *name,
772 struct termios *termp, struct winsize *winp)
775 int mfd = -1, sfd = -1;
777 *amaster = *aslave = -1;
779 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
783 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
786 if ((slave = ptsname(mfd)) == NULL)
789 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
792 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
793 (termp != NULL && tcgetattr(sfd, termp) < 0))
801 ioctl(sfd, TIOCSWINSZ, winp);
812 static void cfmakeraw (struct termios *termios_p)
814 termios_p->c_iflag &=
815 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
816 termios_p->c_oflag &= ~OPOST;
817 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
818 termios_p->c_cflag &= ~(CSIZE|PARENB);
819 termios_p->c_cflag |= CS8;
821 termios_p->c_cc[VMIN] = 0;
822 termios_p->c_cc[VTIME] = 0;
826 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
827 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
837 static void pty_chr_update_read_handler(CharDriverState *chr);
838 static void pty_chr_state(CharDriverState *chr, int connected);
840 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
842 PtyCharDriver *s = chr->opaque;
845 /* guest sends data, check for (re-)connect */
846 pty_chr_update_read_handler(chr);
849 return send_all(s->fd, buf, len);
852 static int pty_chr_read_poll(void *opaque)
854 CharDriverState *chr = opaque;
855 PtyCharDriver *s = chr->opaque;
857 s->read_bytes = qemu_chr_can_read(chr);
858 return s->read_bytes;
861 static void pty_chr_read(void *opaque)
863 CharDriverState *chr = opaque;
864 PtyCharDriver *s = chr->opaque;
869 if (len > s->read_bytes)
873 size = read(s->fd, buf, len);
874 if ((size == -1 && errno == EIO) ||
876 pty_chr_state(chr, 0);
880 pty_chr_state(chr, 1);
881 qemu_chr_read(chr, buf, size);
885 static void pty_chr_update_read_handler(CharDriverState *chr)
887 PtyCharDriver *s = chr->opaque;
889 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
890 pty_chr_read, NULL, chr);
893 * Short timeout here: just need wait long enougth that qemu makes
894 * it through the poll loop once. When reconnected we want a
895 * short timeout so we notice it almost instantly. Otherwise
896 * read() gives us -EIO instantly, making pty_chr_state() reset the
897 * timeout to the normal (much longer) poll interval before the
900 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
903 static void pty_chr_state(CharDriverState *chr, int connected)
905 PtyCharDriver *s = chr->opaque;
908 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
911 /* (re-)connect poll interval for idle guests: once per second.
912 * We check more frequently in case the guests sends data to
913 * the virtual device linked to our pty. */
914 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
922 static void pty_chr_timer(void *opaque)
924 struct CharDriverState *chr = opaque;
925 PtyCharDriver *s = chr->opaque;
930 /* If we arrive here without polling being cleared due
931 * read returning -EIO, then we are (re-)connected */
932 pty_chr_state(chr, 1);
937 pty_chr_update_read_handler(chr);
940 static void pty_chr_close(struct CharDriverState *chr)
942 PtyCharDriver *s = chr->opaque;
944 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
946 qemu_del_timer(s->timer);
947 qemu_free_timer(s->timer);
949 qemu_chr_event(chr, CHR_EVENT_CLOSED);
952 static CharDriverState *qemu_chr_open_pty(void)
954 CharDriverState *chr;
958 #if defined(__OpenBSD__) || defined(__DragonFly__)
959 char pty_name[PATH_MAX];
960 #define q_ptsname(x) pty_name
962 char *pty_name = NULL;
963 #define q_ptsname(x) ptsname(x)
966 chr = qemu_mallocz(sizeof(CharDriverState));
967 s = qemu_mallocz(sizeof(PtyCharDriver));
969 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
973 /* Set raw attributes on the pty. */
974 tcgetattr(slave_fd, &tty);
976 tcsetattr(slave_fd, TCSAFLUSH, &tty);
979 len = strlen(q_ptsname(s->fd)) + 5;
980 chr->filename = qemu_malloc(len);
981 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
982 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
985 chr->chr_write = pty_chr_write;
986 chr->chr_update_read_handler = pty_chr_update_read_handler;
987 chr->chr_close = pty_chr_close;
989 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
994 static void tty_serial_init(int fd, int speed,
995 int parity, int data_bits, int stop_bits)
1001 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1002 speed, parity, data_bits, stop_bits);
1004 tcgetattr (fd, &tty);
1007 if (speed <= 50 * MARGIN)
1009 else if (speed <= 75 * MARGIN)
1011 else if (speed <= 300 * MARGIN)
1013 else if (speed <= 600 * MARGIN)
1015 else if (speed <= 1200 * MARGIN)
1017 else if (speed <= 2400 * MARGIN)
1019 else if (speed <= 4800 * MARGIN)
1021 else if (speed <= 9600 * MARGIN)
1023 else if (speed <= 19200 * MARGIN)
1025 else if (speed <= 38400 * MARGIN)
1027 else if (speed <= 57600 * MARGIN)
1029 else if (speed <= 115200 * MARGIN)
1034 cfsetispeed(&tty, spd);
1035 cfsetospeed(&tty, spd);
1037 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1038 |INLCR|IGNCR|ICRNL|IXON);
1039 tty.c_oflag |= OPOST;
1040 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1041 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1062 tty.c_cflag |= PARENB;
1065 tty.c_cflag |= PARENB | PARODD;
1069 tty.c_cflag |= CSTOPB;
1071 tcsetattr (fd, TCSANOW, &tty);
1074 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1076 FDCharDriver *s = chr->opaque;
1079 case CHR_IOCTL_SERIAL_SET_PARAMS:
1081 QEMUSerialSetParams *ssp = arg;
1082 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1083 ssp->data_bits, ssp->stop_bits);
1086 case CHR_IOCTL_SERIAL_SET_BREAK:
1088 int enable = *(int *)arg;
1090 tcsendbreak(s->fd_in, 1);
1093 case CHR_IOCTL_SERIAL_GET_TIOCM:
1096 int *targ = (int *)arg;
1097 ioctl(s->fd_in, TIOCMGET, &sarg);
1099 if (sarg & TIOCM_CTS)
1100 *targ |= CHR_TIOCM_CTS;
1101 if (sarg & TIOCM_CAR)
1102 *targ |= CHR_TIOCM_CAR;
1103 if (sarg & TIOCM_DSR)
1104 *targ |= CHR_TIOCM_DSR;
1105 if (sarg & TIOCM_RI)
1106 *targ |= CHR_TIOCM_RI;
1107 if (sarg & TIOCM_DTR)
1108 *targ |= CHR_TIOCM_DTR;
1109 if (sarg & TIOCM_RTS)
1110 *targ |= CHR_TIOCM_RTS;
1113 case CHR_IOCTL_SERIAL_SET_TIOCM:
1115 int sarg = *(int *)arg;
1117 ioctl(s->fd_in, TIOCMGET, &targ);
1118 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1119 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1120 if (sarg & CHR_TIOCM_CTS)
1122 if (sarg & CHR_TIOCM_CAR)
1124 if (sarg & CHR_TIOCM_DSR)
1126 if (sarg & CHR_TIOCM_RI)
1128 if (sarg & CHR_TIOCM_DTR)
1130 if (sarg & CHR_TIOCM_RTS)
1132 ioctl(s->fd_in, TIOCMSET, &targ);
1141 static CharDriverState *qemu_chr_open_tty(const char *filename)
1143 CharDriverState *chr;
1146 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1147 tty_serial_init(fd, 115200, 'N', 8, 1);
1148 chr = qemu_chr_open_fd(fd, fd);
1153 chr->chr_ioctl = tty_serial_ioctl;
1154 qemu_chr_reset(chr);
1157 #else /* ! __linux__ && ! __sun__ */
1158 static CharDriverState *qemu_chr_open_pty(void)
1162 #endif /* __linux__ || __sun__ */
1164 #if defined(__linux__)
1168 } ParallelCharDriver;
1170 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1172 if (s->mode != mode) {
1174 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1181 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1183 ParallelCharDriver *drv = chr->opaque;
1188 case CHR_IOCTL_PP_READ_DATA:
1189 if (ioctl(fd, PPRDATA, &b) < 0)
1191 *(uint8_t *)arg = b;
1193 case CHR_IOCTL_PP_WRITE_DATA:
1194 b = *(uint8_t *)arg;
1195 if (ioctl(fd, PPWDATA, &b) < 0)
1198 case CHR_IOCTL_PP_READ_CONTROL:
1199 if (ioctl(fd, PPRCONTROL, &b) < 0)
1201 /* Linux gives only the lowest bits, and no way to know data
1202 direction! For better compatibility set the fixed upper
1204 *(uint8_t *)arg = b | 0xc0;
1206 case CHR_IOCTL_PP_WRITE_CONTROL:
1207 b = *(uint8_t *)arg;
1208 if (ioctl(fd, PPWCONTROL, &b) < 0)
1211 case CHR_IOCTL_PP_READ_STATUS:
1212 if (ioctl(fd, PPRSTATUS, &b) < 0)
1214 *(uint8_t *)arg = b;
1216 case CHR_IOCTL_PP_DATA_DIR:
1217 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1220 case CHR_IOCTL_PP_EPP_READ_ADDR:
1221 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1222 struct ParallelIOArg *parg = arg;
1223 int n = read(fd, parg->buffer, parg->count);
1224 if (n != parg->count) {
1229 case CHR_IOCTL_PP_EPP_READ:
1230 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1231 struct ParallelIOArg *parg = arg;
1232 int n = read(fd, parg->buffer, parg->count);
1233 if (n != parg->count) {
1238 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1239 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1240 struct ParallelIOArg *parg = arg;
1241 int n = write(fd, parg->buffer, parg->count);
1242 if (n != parg->count) {
1247 case CHR_IOCTL_PP_EPP_WRITE:
1248 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1249 struct ParallelIOArg *parg = arg;
1250 int n = write(fd, parg->buffer, parg->count);
1251 if (n != parg->count) {
1262 static void pp_close(CharDriverState *chr)
1264 ParallelCharDriver *drv = chr->opaque;
1267 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1268 ioctl(fd, PPRELEASE);
1271 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1274 static CharDriverState *qemu_chr_open_pp(const char *filename)
1276 CharDriverState *chr;
1277 ParallelCharDriver *drv;
1280 TFR(fd = open(filename, O_RDWR));
1284 if (ioctl(fd, PPCLAIM) < 0) {
1289 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1291 drv->mode = IEEE1284_MODE_COMPAT;
1293 chr = qemu_mallocz(sizeof(CharDriverState));
1294 chr->chr_write = null_chr_write;
1295 chr->chr_ioctl = pp_ioctl;
1296 chr->chr_close = pp_close;
1299 qemu_chr_reset(chr);
1303 #endif /* __linux__ */
1305 #if defined(__FreeBSD__) || defined(__DragonFly__)
1306 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1308 int fd = (int)chr->opaque;
1312 case CHR_IOCTL_PP_READ_DATA:
1313 if (ioctl(fd, PPIGDATA, &b) < 0)
1315 *(uint8_t *)arg = b;
1317 case CHR_IOCTL_PP_WRITE_DATA:
1318 b = *(uint8_t *)arg;
1319 if (ioctl(fd, PPISDATA, &b) < 0)
1322 case CHR_IOCTL_PP_READ_CONTROL:
1323 if (ioctl(fd, PPIGCTRL, &b) < 0)
1325 *(uint8_t *)arg = b;
1327 case CHR_IOCTL_PP_WRITE_CONTROL:
1328 b = *(uint8_t *)arg;
1329 if (ioctl(fd, PPISCTRL, &b) < 0)
1332 case CHR_IOCTL_PP_READ_STATUS:
1333 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1335 *(uint8_t *)arg = b;
1343 static CharDriverState *qemu_chr_open_pp(const char *filename)
1345 CharDriverState *chr;
1348 fd = open(filename, O_RDWR);
1352 chr = qemu_mallocz(sizeof(CharDriverState));
1353 chr->opaque = (void *)fd;
1354 chr->chr_write = null_chr_write;
1355 chr->chr_ioctl = pp_ioctl;
1364 HANDLE hcom, hrecv, hsend;
1365 OVERLAPPED orecv, osend;
1370 #define NSENDBUF 2048
1371 #define NRECVBUF 2048
1372 #define MAXCONNECT 1
1373 #define NTIMEOUT 5000
1375 static int win_chr_poll(void *opaque);
1376 static int win_chr_pipe_poll(void *opaque);
1378 static void win_chr_close(CharDriverState *chr)
1380 WinCharState *s = chr->opaque;
1383 CloseHandle(s->hsend);
1387 CloseHandle(s->hrecv);
1391 CloseHandle(s->hcom);
1395 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1397 qemu_del_polling_cb(win_chr_poll, chr);
1399 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1402 static int win_chr_init(CharDriverState *chr, const char *filename)
1404 WinCharState *s = chr->opaque;
1406 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1411 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1413 fprintf(stderr, "Failed CreateEvent\n");
1416 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1418 fprintf(stderr, "Failed CreateEvent\n");
1422 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1423 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1424 if (s->hcom == INVALID_HANDLE_VALUE) {
1425 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1430 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1431 fprintf(stderr, "Failed SetupComm\n");
1435 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1436 size = sizeof(COMMCONFIG);
1437 GetDefaultCommConfig(filename, &comcfg, &size);
1438 comcfg.dcb.DCBlength = sizeof(DCB);
1439 CommConfigDialog(filename, NULL, &comcfg);
1441 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1442 fprintf(stderr, "Failed SetCommState\n");
1446 if (!SetCommMask(s->hcom, EV_ERR)) {
1447 fprintf(stderr, "Failed SetCommMask\n");
1451 cto.ReadIntervalTimeout = MAXDWORD;
1452 if (!SetCommTimeouts(s->hcom, &cto)) {
1453 fprintf(stderr, "Failed SetCommTimeouts\n");
1457 if (!ClearCommError(s->hcom, &err, &comstat)) {
1458 fprintf(stderr, "Failed ClearCommError\n");
1461 qemu_add_polling_cb(win_chr_poll, chr);
1469 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1471 WinCharState *s = chr->opaque;
1472 DWORD len, ret, size, err;
1475 ZeroMemory(&s->osend, sizeof(s->osend));
1476 s->osend.hEvent = s->hsend;
1479 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1481 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1483 err = GetLastError();
1484 if (err == ERROR_IO_PENDING) {
1485 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1503 static int win_chr_read_poll(CharDriverState *chr)
1505 WinCharState *s = chr->opaque;
1507 s->max_size = qemu_chr_can_read(chr);
1511 static void win_chr_readfile(CharDriverState *chr)
1513 WinCharState *s = chr->opaque;
1518 ZeroMemory(&s->orecv, sizeof(s->orecv));
1519 s->orecv.hEvent = s->hrecv;
1520 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1522 err = GetLastError();
1523 if (err == ERROR_IO_PENDING) {
1524 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1529 qemu_chr_read(chr, buf, size);
1533 static void win_chr_read(CharDriverState *chr)
1535 WinCharState *s = chr->opaque;
1537 if (s->len > s->max_size)
1538 s->len = s->max_size;
1542 win_chr_readfile(chr);
1545 static int win_chr_poll(void *opaque)
1547 CharDriverState *chr = opaque;
1548 WinCharState *s = chr->opaque;
1552 ClearCommError(s->hcom, &comerr, &status);
1553 if (status.cbInQue > 0) {
1554 s->len = status.cbInQue;
1555 win_chr_read_poll(chr);
1562 static CharDriverState *qemu_chr_open_win(const char *filename)
1564 CharDriverState *chr;
1567 chr = qemu_mallocz(sizeof(CharDriverState));
1568 s = qemu_mallocz(sizeof(WinCharState));
1570 chr->chr_write = win_chr_write;
1571 chr->chr_close = win_chr_close;
1573 if (win_chr_init(chr, filename) < 0) {
1578 qemu_chr_reset(chr);
1582 static int win_chr_pipe_poll(void *opaque)
1584 CharDriverState *chr = opaque;
1585 WinCharState *s = chr->opaque;
1588 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1591 win_chr_read_poll(chr);
1598 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1600 WinCharState *s = chr->opaque;
1608 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1610 fprintf(stderr, "Failed CreateEvent\n");
1613 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1615 fprintf(stderr, "Failed CreateEvent\n");
1619 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1620 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1621 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1623 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1624 if (s->hcom == INVALID_HANDLE_VALUE) {
1625 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1630 ZeroMemory(&ov, sizeof(ov));
1631 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1632 ret = ConnectNamedPipe(s->hcom, &ov);
1634 fprintf(stderr, "Failed ConnectNamedPipe\n");
1638 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1640 fprintf(stderr, "Failed GetOverlappedResult\n");
1642 CloseHandle(ov.hEvent);
1649 CloseHandle(ov.hEvent);
1652 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1661 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1663 CharDriverState *chr;
1666 chr = qemu_mallocz(sizeof(CharDriverState));
1667 s = qemu_mallocz(sizeof(WinCharState));
1669 chr->chr_write = win_chr_write;
1670 chr->chr_close = win_chr_close;
1672 if (win_chr_pipe_init(chr, filename) < 0) {
1677 qemu_chr_reset(chr);
1681 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1683 CharDriverState *chr;
1686 chr = qemu_mallocz(sizeof(CharDriverState));
1687 s = qemu_mallocz(sizeof(WinCharState));
1690 chr->chr_write = win_chr_write;
1691 qemu_chr_reset(chr);
1695 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1697 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1700 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1704 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1705 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1706 if (fd_out == INVALID_HANDLE_VALUE)
1709 return qemu_chr_open_win_file(fd_out);
1711 #endif /* !_WIN32 */
1713 /***********************************************************/
1714 /* UDP Net console */
1718 struct sockaddr_in daddr;
1725 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1727 NetCharDriver *s = chr->opaque;
1729 return sendto(s->fd, (const void *)buf, len, 0,
1730 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1733 static int udp_chr_read_poll(void *opaque)
1735 CharDriverState *chr = opaque;
1736 NetCharDriver *s = chr->opaque;
1738 s->max_size = qemu_chr_can_read(chr);
1740 /* If there were any stray characters in the queue process them
1743 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1744 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1746 s->max_size = qemu_chr_can_read(chr);
1751 static void udp_chr_read(void *opaque)
1753 CharDriverState *chr = opaque;
1754 NetCharDriver *s = chr->opaque;
1756 if (s->max_size == 0)
1758 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1759 s->bufptr = s->bufcnt;
1764 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1765 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1767 s->max_size = qemu_chr_can_read(chr);
1771 static void udp_chr_update_read_handler(CharDriverState *chr)
1773 NetCharDriver *s = chr->opaque;
1776 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1777 udp_chr_read, NULL, chr);
1781 static void udp_chr_close(CharDriverState *chr)
1783 NetCharDriver *s = chr->opaque;
1785 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1789 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1792 static CharDriverState *qemu_chr_open_udp(const char *def)
1794 CharDriverState *chr = NULL;
1795 NetCharDriver *s = NULL;
1797 struct sockaddr_in saddr;
1799 chr = qemu_mallocz(sizeof(CharDriverState));
1800 s = qemu_mallocz(sizeof(NetCharDriver));
1802 fd = socket(PF_INET, SOCK_DGRAM, 0);
1804 perror("socket(PF_INET, SOCK_DGRAM)");
1808 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1809 printf("Could not parse: %s\n", def);
1813 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1823 chr->chr_write = udp_chr_write;
1824 chr->chr_update_read_handler = udp_chr_update_read_handler;
1825 chr->chr_close = udp_chr_close;
1838 /***********************************************************/
1839 /* TCP Net console */
1851 static void tcp_chr_accept(void *opaque);
1853 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1855 TCPCharDriver *s = chr->opaque;
1857 return send_all(s->fd, buf, len);
1859 /* XXX: indicate an error ? */
1864 static int tcp_chr_read_poll(void *opaque)
1866 CharDriverState *chr = opaque;
1867 TCPCharDriver *s = chr->opaque;
1870 s->max_size = qemu_chr_can_read(chr);
1875 #define IAC_BREAK 243
1876 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1878 uint8_t *buf, int *size)
1880 /* Handle any telnet client's basic IAC options to satisfy char by
1881 * char mode with no echo. All IAC options will be removed from
1882 * the buf and the do_telnetopt variable will be used to track the
1883 * state of the width of the IAC information.
1885 * IAC commands come in sets of 3 bytes with the exception of the
1886 * "IAC BREAK" command and the double IAC.
1892 for (i = 0; i < *size; i++) {
1893 if (s->do_telnetopt > 1) {
1894 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1895 /* Double IAC means send an IAC */
1899 s->do_telnetopt = 1;
1901 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1902 /* Handle IAC break commands by sending a serial break */
1903 qemu_chr_event(chr, CHR_EVENT_BREAK);
1908 if (s->do_telnetopt >= 4) {
1909 s->do_telnetopt = 1;
1912 if ((unsigned char)buf[i] == IAC) {
1913 s->do_telnetopt = 2;
1924 static int tcp_get_msgfd(CharDriverState *chr)
1926 TCPCharDriver *s = chr->opaque;
1932 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1934 TCPCharDriver *s = chr->opaque;
1935 struct cmsghdr *cmsg;
1937 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1940 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1941 cmsg->cmsg_level != SOL_SOCKET ||
1942 cmsg->cmsg_type != SCM_RIGHTS)
1945 fd = *((int *)CMSG_DATA(cmsg));
1955 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1957 TCPCharDriver *s = chr->opaque;
1958 struct msghdr msg = { NULL, };
1959 struct iovec iov[1];
1961 struct cmsghdr cmsg;
1962 char control[CMSG_SPACE(sizeof(int))];
1966 iov[0].iov_base = buf;
1967 iov[0].iov_len = len;
1971 msg.msg_control = &msg_control;
1972 msg.msg_controllen = sizeof(msg_control);
1974 ret = recvmsg(s->fd, &msg, 0);
1975 if (ret > 0 && s->is_unix)
1976 unix_process_msgfd(chr, &msg);
1981 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1983 TCPCharDriver *s = chr->opaque;
1984 return recv(s->fd, buf, len, 0);
1988 static void tcp_chr_read(void *opaque)
1990 CharDriverState *chr = opaque;
1991 TCPCharDriver *s = chr->opaque;
1995 if (!s->connected || s->max_size <= 0)
1998 if (len > s->max_size)
2000 size = tcp_chr_recv(chr, (void *)buf, len);
2002 /* connection closed */
2004 if (s->listen_fd >= 0) {
2005 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2007 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2010 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2011 } else if (size > 0) {
2012 if (s->do_telnetopt)
2013 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2015 qemu_chr_read(chr, buf, size);
2016 if (s->msgfd != -1) {
2023 static void tcp_chr_connect(void *opaque)
2025 CharDriverState *chr = opaque;
2026 TCPCharDriver *s = chr->opaque;
2029 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2030 tcp_chr_read, NULL, chr);
2031 qemu_chr_reset(chr);
2034 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2035 static void tcp_chr_telnet_init(int fd)
2038 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2039 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2040 send(fd, (char *)buf, 3, 0);
2041 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2042 send(fd, (char *)buf, 3, 0);
2043 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2044 send(fd, (char *)buf, 3, 0);
2045 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2046 send(fd, (char *)buf, 3, 0);
2049 static void socket_set_nodelay(int fd)
2052 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2055 static void tcp_chr_accept(void *opaque)
2057 CharDriverState *chr = opaque;
2058 TCPCharDriver *s = chr->opaque;
2059 struct sockaddr_in saddr;
2061 struct sockaddr_un uaddr;
2063 struct sockaddr *addr;
2070 len = sizeof(uaddr);
2071 addr = (struct sockaddr *)&uaddr;
2075 len = sizeof(saddr);
2076 addr = (struct sockaddr *)&saddr;
2078 fd = accept(s->listen_fd, addr, &len);
2079 if (fd < 0 && errno != EINTR) {
2081 } else if (fd >= 0) {
2082 if (s->do_telnetopt)
2083 tcp_chr_telnet_init(fd);
2087 socket_set_nonblock(fd);
2089 socket_set_nodelay(fd);
2091 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2092 tcp_chr_connect(chr);
2095 static void tcp_chr_close(CharDriverState *chr)
2097 TCPCharDriver *s = chr->opaque;
2099 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2102 if (s->listen_fd >= 0) {
2103 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2104 closesocket(s->listen_fd);
2107 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2110 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
2114 CharDriverState *chr = NULL;
2115 TCPCharDriver *s = NULL;
2116 int fd = -1, offset = 0;
2118 int is_waitconnect = 1;
2123 while((ptr = strchr(ptr,','))) {
2125 if (!strncmp(ptr,"server",6)) {
2127 } else if (!strncmp(ptr,"nowait",6)) {
2129 } else if (!strncmp(ptr,"nodelay",6)) {
2131 } else if (!strncmp(ptr,"to=",3)) {
2132 /* nothing, inet_listen() parses this one */;
2133 } else if (!strncmp(ptr,"ipv4",4)) {
2134 /* nothing, inet_connect() and inet_listen() parse this one */;
2135 } else if (!strncmp(ptr,"ipv6",4)) {
2136 /* nothing, inet_connect() and inet_listen() parse this one */;
2138 printf("Unknown option: %s\n", ptr);
2145 chr = qemu_mallocz(sizeof(CharDriverState));
2146 s = qemu_mallocz(sizeof(TCPCharDriver));
2149 chr->filename = qemu_malloc(256);
2151 pstrcpy(chr->filename, 256, "unix:");
2152 } else if (is_telnet) {
2153 pstrcpy(chr->filename, 256, "telnet:");
2155 pstrcpy(chr->filename, 256, "tcp:");
2157 offset = strlen(chr->filename);
2161 fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
2163 fd = unix_connect(host_str);
2167 fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
2170 fd = inet_connect(host_str, SOCK_STREAM);
2176 if (!is_waitconnect)
2177 socket_set_nonblock(fd);
2183 s->is_unix = is_unix;
2184 s->do_nodelay = do_nodelay && !is_unix;
2187 chr->chr_write = tcp_chr_write;
2188 chr->chr_close = tcp_chr_close;
2189 chr->get_msgfd = tcp_get_msgfd;
2193 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2195 s->do_telnetopt = 1;
2199 socket_set_nodelay(fd);
2200 tcp_chr_connect(chr);
2203 if (is_listen && is_waitconnect) {
2204 printf("QEMU waiting for connection on: %s\n",
2205 chr->filename ? chr->filename : host_str);
2206 tcp_chr_accept(chr);
2207 socket_set_nonblock(s->listen_fd);
2219 static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2223 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2227 if (strcmp(filename, "null") == 0) {
2228 qemu_opt_set(opts, "backend", "null");
2232 qemu_opts_del(opts);
2236 static const struct {
2238 CharDriverState *(*open)(QemuOpts *opts);
2239 } backend_table[] = {
2240 { .name = "null", .open = qemu_chr_open_null },
2243 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2244 void (*init)(struct CharDriverState *s))
2246 CharDriverState *chr;
2249 if (qemu_opts_id(opts) == NULL) {
2250 fprintf(stderr, "chardev: no id specified\n");
2254 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2255 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2258 if (i == ARRAY_SIZE(backend_table)) {
2259 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2260 qemu_opt_get(opts, "backend"));
2264 chr = backend_table[i].open(opts);
2266 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2267 qemu_opt_get(opts, "backend"));
2272 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2274 chr->label = qemu_strdup(qemu_opts_id(opts));
2275 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2279 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2282 CharDriverState *chr;
2285 opts = qemu_chr_parse_compat(label, filename);
2287 return qemu_chr_open_opts(opts, init);
2290 if (!strcmp(filename, "vc")) {
2291 chr = text_console_init(NULL);
2293 if (strstart(filename, "vc:", &p)) {
2294 chr = text_console_init(p);
2296 if (strstart(filename, "tcp:", &p)) {
2297 chr = qemu_chr_open_tcp(p, 0, 0);
2299 if (strstart(filename, "telnet:", &p)) {
2300 chr = qemu_chr_open_tcp(p, 1, 0);
2302 if (strstart(filename, "udp:", &p)) {
2303 chr = qemu_chr_open_udp(p);
2305 if (strstart(filename, "mon:", &p)) {
2306 chr = qemu_chr_open(label, p, NULL);
2308 chr = qemu_chr_open_mux(chr);
2309 monitor_init(chr, MONITOR_USE_READLINE);
2311 printf("Unable to open driver: %s\n", p);
2313 } else if (!strcmp(filename, "msmouse")) {
2314 chr = qemu_chr_open_msmouse();
2317 if (strstart(filename, "unix:", &p)) {
2318 chr = qemu_chr_open_tcp(p, 0, 1);
2319 } else if (strstart(filename, "file:", &p)) {
2320 chr = qemu_chr_open_file_out(p);
2321 } else if (strstart(filename, "pipe:", &p)) {
2322 chr = qemu_chr_open_pipe(p);
2323 } else if (!strcmp(filename, "pty")) {
2324 chr = qemu_chr_open_pty();
2325 } else if (!strcmp(filename, "stdio")) {
2326 chr = qemu_chr_open_stdio();
2328 #if defined(__linux__)
2329 if (strstart(filename, "/dev/parport", NULL)) {
2330 chr = qemu_chr_open_pp(filename);
2332 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2333 if (strstart(filename, "/dev/ppi", NULL)) {
2334 chr = qemu_chr_open_pp(filename);
2337 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2338 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2339 if (strstart(filename, "/dev/", NULL)) {
2340 chr = qemu_chr_open_tty(filename);
2344 if (strstart(filename, "COM", NULL)) {
2345 chr = qemu_chr_open_win(filename);
2347 if (strstart(filename, "pipe:", &p)) {
2348 chr = qemu_chr_open_win_pipe(p);
2350 if (strstart(filename, "con:", NULL)) {
2351 chr = qemu_chr_open_win_con(filename);
2353 if (strstart(filename, "file:", &p)) {
2354 chr = qemu_chr_open_win_file_out(p);
2357 #ifdef CONFIG_BRLAPI
2358 if (!strcmp(filename, "braille")) {
2359 chr = chr_baum_init();
2368 chr->filename = qemu_strdup(filename);
2370 chr->label = qemu_strdup(label);
2371 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2376 void qemu_chr_close(CharDriverState *chr)
2378 TAILQ_REMOVE(&chardevs, chr, next);
2380 chr->chr_close(chr);
2381 qemu_free(chr->filename);
2382 qemu_free(chr->label);
2386 void qemu_chr_info(Monitor *mon)
2388 CharDriverState *chr;
2390 TAILQ_FOREACH(chr, &chardevs, next) {
2391 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);