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>
54 #include <arpa/inet.h>
57 #include <sys/select.h>
62 #include <dev/ppbus/ppi.h>
63 #include <dev/ppbus/ppbconf.h>
64 #elif defined(__DragonFly__)
66 #include <dev/misc/ppi/ppi.h>
67 #include <bus/ppbus/ppbconf.h>
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
98 #include "qemu_socket.h"
100 /***********************************************************/
101 /* character device */
103 static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
104 QTAILQ_HEAD_INITIALIZER(chardevs);
106 static void qemu_chr_event(CharDriverState *s, int event)
110 s->chr_event(s->handler_opaque, event);
113 static void qemu_chr_reset_bh(void *opaque)
115 CharDriverState *s = opaque;
116 qemu_chr_event(s, CHR_EVENT_OPENED);
117 qemu_bh_delete(s->bh);
121 void qemu_chr_reset(CharDriverState *s)
124 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
125 qemu_bh_schedule(s->bh);
129 void qemu_chr_initial_reset(void)
131 CharDriverState *chr;
133 QTAILQ_FOREACH(chr, &chardevs, next) {
138 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
140 return s->chr_write(s, buf, len);
143 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
147 return s->chr_ioctl(s, cmd, arg);
150 int qemu_chr_can_read(CharDriverState *s)
152 if (!s->chr_can_read)
154 return s->chr_can_read(s->handler_opaque);
157 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
159 s->chr_read(s->handler_opaque, buf, len);
162 int qemu_chr_get_msgfd(CharDriverState *s)
164 return s->get_msgfd ? s->get_msgfd(s) : -1;
167 void qemu_chr_accept_input(CharDriverState *s)
169 if (s->chr_accept_input)
170 s->chr_accept_input(s);
173 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
178 vsnprintf(buf, sizeof(buf), fmt, ap);
179 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
183 void qemu_chr_send_event(CharDriverState *s, int event)
185 if (s->chr_send_event)
186 s->chr_send_event(s, event);
189 void qemu_chr_add_handlers(CharDriverState *s,
190 IOCanRWHandler *fd_can_read,
191 IOReadHandler *fd_read,
192 IOEventHandler *fd_event,
195 s->chr_can_read = fd_can_read;
196 s->chr_read = fd_read;
197 s->chr_event = fd_event;
198 s->handler_opaque = opaque;
199 if (s->chr_update_read_handler)
200 s->chr_update_read_handler(s);
203 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
208 static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
210 CharDriverState *chr;
212 chr = qemu_mallocz(sizeof(CharDriverState));
213 chr->chr_write = null_chr_write;
217 /* MUX driver for serial I/O splitting */
219 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
220 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
222 IOCanRWHandler *chr_can_read[MAX_MUX];
223 IOReadHandler *chr_read[MAX_MUX];
224 IOEventHandler *chr_event[MAX_MUX];
225 void *ext_opaque[MAX_MUX];
226 CharDriverState *drv;
231 /* Intermediate input buffer allows to catch escape sequences even if the
232 currently active device is not accepting any input - but only until it
234 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
239 int64_t timestamps_start;
243 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
245 MuxDriver *d = chr->opaque;
247 if (!d->timestamps) {
248 ret = d->drv->chr_write(d->drv, buf, len);
253 for (i = 0; i < len; i++) {
259 ti = qemu_get_clock(rt_clock);
260 if (d->timestamps_start == -1)
261 d->timestamps_start = ti;
262 ti -= d->timestamps_start;
264 snprintf(buf1, sizeof(buf1),
265 "[%02d:%02d:%02d.%03d] ",
270 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
273 ret += d->drv->chr_write(d->drv, buf+i, 1);
274 if (buf[i] == '\n') {
282 static const char * const mux_help[] = {
283 "% h print this help\n\r",
284 "% x exit emulator\n\r",
285 "% s save disk data back to file (if -snapshot)\n\r",
286 "% t toggle console timestamps\n\r"
287 "% b send break (magic sysrq)\n\r",
288 "% c switch between console and monitor\n\r",
293 int term_escape_char = 0x01; /* ctrl-a is used for escape */
294 static void mux_print_help(CharDriverState *chr)
297 char ebuf[15] = "Escape-Char";
298 char cbuf[50] = "\n\r";
300 if (term_escape_char > 0 && term_escape_char < 26) {
301 snprintf(cbuf, sizeof(cbuf), "\n\r");
302 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
304 snprintf(cbuf, sizeof(cbuf),
305 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
308 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
309 for (i = 0; mux_help[i] != NULL; i++) {
310 for (j=0; mux_help[i][j] != '\0'; j++) {
311 if (mux_help[i][j] == '%')
312 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
314 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
319 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
321 if (d->chr_event[mux_nr])
322 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
325 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
327 if (d->term_got_escape) {
328 d->term_got_escape = 0;
329 if (ch == term_escape_char)
338 const char *term = "QEMU: Terminated\n\r";
339 chr->chr_write(chr,(uint8_t *)term,strlen(term));
346 QTAILQ_FOREACH(dinfo, &drives, next) {
347 bdrv_commit(dinfo->bdrv);
352 qemu_chr_event(chr, CHR_EVENT_BREAK);
355 /* Switch to the next registered device */
356 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
358 if (d->focus >= d->mux_cnt)
360 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
363 d->timestamps = !d->timestamps;
364 d->timestamps_start = -1;
368 } else if (ch == term_escape_char) {
369 d->term_got_escape = 1;
377 static void mux_chr_accept_input(CharDriverState *chr)
379 MuxDriver *d = chr->opaque;
382 while (d->prod[m] != d->cons[m] &&
383 d->chr_can_read[m] &&
384 d->chr_can_read[m](d->ext_opaque[m])) {
385 d->chr_read[m](d->ext_opaque[m],
386 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
390 static int mux_chr_can_read(void *opaque)
392 CharDriverState *chr = opaque;
393 MuxDriver *d = chr->opaque;
396 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
398 if (d->chr_can_read[m])
399 return d->chr_can_read[m](d->ext_opaque[m]);
403 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
405 CharDriverState *chr = opaque;
406 MuxDriver *d = chr->opaque;
410 mux_chr_accept_input (opaque);
412 for(i = 0; i < size; i++)
413 if (mux_proc_byte(chr, d, buf[i])) {
414 if (d->prod[m] == d->cons[m] &&
415 d->chr_can_read[m] &&
416 d->chr_can_read[m](d->ext_opaque[m]))
417 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
419 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
423 static void mux_chr_event(void *opaque, int event)
425 CharDriverState *chr = opaque;
426 MuxDriver *d = chr->opaque;
429 /* Send the event to all registered listeners */
430 for (i = 0; i < d->mux_cnt; i++)
431 mux_chr_send_event(d, i, event);
434 static void mux_chr_update_read_handler(CharDriverState *chr)
436 MuxDriver *d = chr->opaque;
438 if (d->mux_cnt >= MAX_MUX) {
439 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
442 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
443 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
444 d->chr_read[d->mux_cnt] = chr->chr_read;
445 d->chr_event[d->mux_cnt] = chr->chr_event;
446 /* Fix up the real driver with mux routines */
447 if (d->mux_cnt == 0) {
448 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
451 if (d->focus != -1) {
452 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
454 d->focus = d->mux_cnt;
456 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
459 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
461 CharDriverState *chr;
464 chr = qemu_mallocz(sizeof(CharDriverState));
465 d = qemu_mallocz(sizeof(MuxDriver));
470 chr->chr_write = mux_chr_write;
471 chr->chr_update_read_handler = mux_chr_update_read_handler;
472 chr->chr_accept_input = mux_chr_accept_input;
478 int send_all(int fd, const void *buf, int len1)
484 ret = send(fd, buf, len, 0);
486 errno = WSAGetLastError();
487 if (errno != WSAEWOULDBLOCK) {
490 } else if (ret == 0) {
502 static int unix_write(int fd, const uint8_t *buf, int len1)
508 ret = write(fd, buf, len);
510 if (errno != EINTR && errno != EAGAIN)
512 } else if (ret == 0) {
522 int send_all(int fd, const void *buf, int len1)
524 return unix_write(fd, buf, len1);
535 #define STDIO_MAX_CLIENTS 1
536 static int stdio_nb_clients = 0;
538 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
540 FDCharDriver *s = chr->opaque;
541 return send_all(s->fd_out, buf, len);
544 static int fd_chr_read_poll(void *opaque)
546 CharDriverState *chr = opaque;
547 FDCharDriver *s = chr->opaque;
549 s->max_size = qemu_chr_can_read(chr);
553 static void fd_chr_read(void *opaque)
555 CharDriverState *chr = opaque;
556 FDCharDriver *s = chr->opaque;
561 if (len > s->max_size)
565 size = read(s->fd_in, buf, len);
567 /* FD has been closed. Remove it from the active list. */
568 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
569 qemu_chr_event(chr, CHR_EVENT_CLOSED);
573 qemu_chr_read(chr, buf, size);
577 static void fd_chr_update_read_handler(CharDriverState *chr)
579 FDCharDriver *s = chr->opaque;
582 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
584 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
585 fd_chr_read, NULL, chr);
590 static void fd_chr_close(struct CharDriverState *chr)
592 FDCharDriver *s = chr->opaque;
595 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
597 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
602 qemu_chr_event(chr, CHR_EVENT_CLOSED);
605 /* open a character device to a unix fd */
606 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
608 CharDriverState *chr;
611 chr = qemu_mallocz(sizeof(CharDriverState));
612 s = qemu_mallocz(sizeof(FDCharDriver));
616 chr->chr_write = fd_chr_write;
617 chr->chr_update_read_handler = fd_chr_update_read_handler;
618 chr->chr_close = fd_chr_close;
625 static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
629 TFR(fd_out = open(qemu_opt_get(opts, "path"),
630 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
633 return qemu_chr_open_fd(-1, fd_out);
636 static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
639 char filename_in[256], filename_out[256];
640 const char *filename = qemu_opt_get(opts, "path");
642 if (filename == NULL) {
643 fprintf(stderr, "chardev: pipe: no filename given\n");
647 snprintf(filename_in, 256, "%s.in", filename);
648 snprintf(filename_out, 256, "%s.out", filename);
649 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
650 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
651 if (fd_in < 0 || fd_out < 0) {
656 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
660 return qemu_chr_open_fd(fd_in, fd_out);
664 /* for STDIO, we handle the case where several clients use it
667 #define TERM_FIFO_MAX_SIZE 1
669 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
670 static int term_fifo_size;
672 static int stdio_read_poll(void *opaque)
674 CharDriverState *chr = opaque;
676 /* try to flush the queue if needed */
677 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
678 qemu_chr_read(chr, term_fifo, 1);
681 /* see if we can absorb more chars */
682 if (term_fifo_size == 0)
688 static void stdio_read(void *opaque)
692 CharDriverState *chr = opaque;
694 size = read(0, buf, 1);
696 /* stdin has been closed. Remove it from the active list. */
697 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
698 qemu_chr_event(chr, CHR_EVENT_CLOSED);
702 if (qemu_chr_can_read(chr) > 0) {
703 qemu_chr_read(chr, buf, 1);
704 } else if (term_fifo_size == 0) {
705 term_fifo[term_fifo_size++] = buf[0];
710 /* init terminal so that we can grab keys */
711 static struct termios oldtty;
712 static int old_fd0_flags;
713 static int term_atexit_done;
715 static void term_exit(void)
717 tcsetattr (0, TCSANOW, &oldtty);
718 fcntl(0, F_SETFL, old_fd0_flags);
721 static void term_init(QemuOpts *opts)
727 old_fd0_flags = fcntl(0, F_GETFL);
729 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
730 |INLCR|IGNCR|ICRNL|IXON);
731 tty.c_oflag |= OPOST;
732 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
733 /* if graphical mode, we allow Ctrl-C handling */
734 if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC))
735 tty.c_lflag &= ~ISIG;
736 tty.c_cflag &= ~(CSIZE|PARENB);
741 tcsetattr (0, TCSANOW, &tty);
743 if (!term_atexit_done++)
746 fcntl(0, F_SETFL, O_NONBLOCK);
749 static void qemu_chr_close_stdio(struct CharDriverState *chr)
753 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
757 static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
759 CharDriverState *chr;
761 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
763 chr = qemu_chr_open_fd(0, 1);
764 chr->chr_close = qemu_chr_close_stdio;
765 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
773 /* Once Solaris has openpty(), this is going to be removed. */
774 static int openpty(int *amaster, int *aslave, char *name,
775 struct termios *termp, struct winsize *winp)
778 int mfd = -1, sfd = -1;
780 *amaster = *aslave = -1;
782 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
786 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
789 if ((slave = ptsname(mfd)) == NULL)
792 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
795 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
796 (termp != NULL && tcgetattr(sfd, termp) < 0))
804 ioctl(sfd, TIOCSWINSZ, winp);
815 static void cfmakeraw (struct termios *termios_p)
817 termios_p->c_iflag &=
818 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
819 termios_p->c_oflag &= ~OPOST;
820 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
821 termios_p->c_cflag &= ~(CSIZE|PARENB);
822 termios_p->c_cflag |= CS8;
824 termios_p->c_cc[VMIN] = 0;
825 termios_p->c_cc[VTIME] = 0;
829 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
830 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
840 static void pty_chr_update_read_handler(CharDriverState *chr);
841 static void pty_chr_state(CharDriverState *chr, int connected);
843 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
845 PtyCharDriver *s = chr->opaque;
848 /* guest sends data, check for (re-)connect */
849 pty_chr_update_read_handler(chr);
852 return send_all(s->fd, buf, len);
855 static int pty_chr_read_poll(void *opaque)
857 CharDriverState *chr = opaque;
858 PtyCharDriver *s = chr->opaque;
860 s->read_bytes = qemu_chr_can_read(chr);
861 return s->read_bytes;
864 static void pty_chr_read(void *opaque)
866 CharDriverState *chr = opaque;
867 PtyCharDriver *s = chr->opaque;
872 if (len > s->read_bytes)
876 size = read(s->fd, buf, len);
877 if ((size == -1 && errno == EIO) ||
879 pty_chr_state(chr, 0);
883 pty_chr_state(chr, 1);
884 qemu_chr_read(chr, buf, size);
888 static void pty_chr_update_read_handler(CharDriverState *chr)
890 PtyCharDriver *s = chr->opaque;
892 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
893 pty_chr_read, NULL, chr);
896 * Short timeout here: just need wait long enougth that qemu makes
897 * it through the poll loop once. When reconnected we want a
898 * short timeout so we notice it almost instantly. Otherwise
899 * read() gives us -EIO instantly, making pty_chr_state() reset the
900 * timeout to the normal (much longer) poll interval before the
903 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
906 static void pty_chr_state(CharDriverState *chr, int connected)
908 PtyCharDriver *s = chr->opaque;
911 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
914 /* (re-)connect poll interval for idle guests: once per second.
915 * We check more frequently in case the guests sends data to
916 * the virtual device linked to our pty. */
917 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
925 static void pty_chr_timer(void *opaque)
927 struct CharDriverState *chr = opaque;
928 PtyCharDriver *s = chr->opaque;
933 /* If we arrive here without polling being cleared due
934 * read returning -EIO, then we are (re-)connected */
935 pty_chr_state(chr, 1);
940 pty_chr_update_read_handler(chr);
943 static void pty_chr_close(struct CharDriverState *chr)
945 PtyCharDriver *s = chr->opaque;
947 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
949 qemu_del_timer(s->timer);
950 qemu_free_timer(s->timer);
952 qemu_chr_event(chr, CHR_EVENT_CLOSED);
955 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
957 CharDriverState *chr;
961 #if defined(__OpenBSD__) || defined(__DragonFly__)
962 char pty_name[PATH_MAX];
963 #define q_ptsname(x) pty_name
965 char *pty_name = NULL;
966 #define q_ptsname(x) ptsname(x)
969 chr = qemu_mallocz(sizeof(CharDriverState));
970 s = qemu_mallocz(sizeof(PtyCharDriver));
972 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
976 /* Set raw attributes on the pty. */
977 tcgetattr(slave_fd, &tty);
979 tcsetattr(slave_fd, TCSAFLUSH, &tty);
982 len = strlen(q_ptsname(s->fd)) + 5;
983 chr->filename = qemu_malloc(len);
984 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
985 qemu_opt_set(opts, "path", q_ptsname(s->fd));
986 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
989 chr->chr_write = pty_chr_write;
990 chr->chr_update_read_handler = pty_chr_update_read_handler;
991 chr->chr_close = pty_chr_close;
993 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
998 static void tty_serial_init(int fd, int speed,
999 int parity, int data_bits, int stop_bits)
1005 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1006 speed, parity, data_bits, stop_bits);
1008 tcgetattr (fd, &tty);
1011 if (speed <= 50 * MARGIN)
1013 else if (speed <= 75 * MARGIN)
1015 else if (speed <= 300 * MARGIN)
1017 else if (speed <= 600 * MARGIN)
1019 else if (speed <= 1200 * MARGIN)
1021 else if (speed <= 2400 * MARGIN)
1023 else if (speed <= 4800 * MARGIN)
1025 else if (speed <= 9600 * MARGIN)
1027 else if (speed <= 19200 * MARGIN)
1029 else if (speed <= 38400 * MARGIN)
1031 else if (speed <= 57600 * MARGIN)
1033 else if (speed <= 115200 * MARGIN)
1038 cfsetispeed(&tty, spd);
1039 cfsetospeed(&tty, spd);
1041 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1042 |INLCR|IGNCR|ICRNL|IXON);
1043 tty.c_oflag |= OPOST;
1044 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1045 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1066 tty.c_cflag |= PARENB;
1069 tty.c_cflag |= PARENB | PARODD;
1073 tty.c_cflag |= CSTOPB;
1075 tcsetattr (fd, TCSANOW, &tty);
1078 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1080 FDCharDriver *s = chr->opaque;
1083 case CHR_IOCTL_SERIAL_SET_PARAMS:
1085 QEMUSerialSetParams *ssp = arg;
1086 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1087 ssp->data_bits, ssp->stop_bits);
1090 case CHR_IOCTL_SERIAL_SET_BREAK:
1092 int enable = *(int *)arg;
1094 tcsendbreak(s->fd_in, 1);
1097 case CHR_IOCTL_SERIAL_GET_TIOCM:
1100 int *targ = (int *)arg;
1101 ioctl(s->fd_in, TIOCMGET, &sarg);
1103 if (sarg & TIOCM_CTS)
1104 *targ |= CHR_TIOCM_CTS;
1105 if (sarg & TIOCM_CAR)
1106 *targ |= CHR_TIOCM_CAR;
1107 if (sarg & TIOCM_DSR)
1108 *targ |= CHR_TIOCM_DSR;
1109 if (sarg & TIOCM_RI)
1110 *targ |= CHR_TIOCM_RI;
1111 if (sarg & TIOCM_DTR)
1112 *targ |= CHR_TIOCM_DTR;
1113 if (sarg & TIOCM_RTS)
1114 *targ |= CHR_TIOCM_RTS;
1117 case CHR_IOCTL_SERIAL_SET_TIOCM:
1119 int sarg = *(int *)arg;
1121 ioctl(s->fd_in, TIOCMGET, &targ);
1122 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1123 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1124 if (sarg & CHR_TIOCM_CTS)
1126 if (sarg & CHR_TIOCM_CAR)
1128 if (sarg & CHR_TIOCM_DSR)
1130 if (sarg & CHR_TIOCM_RI)
1132 if (sarg & CHR_TIOCM_DTR)
1134 if (sarg & CHR_TIOCM_RTS)
1136 ioctl(s->fd_in, TIOCMSET, &targ);
1145 static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
1147 const char *filename = qemu_opt_get(opts, "path");
1148 CharDriverState *chr;
1151 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1152 tty_serial_init(fd, 115200, 'N', 8, 1);
1153 chr = qemu_chr_open_fd(fd, fd);
1158 chr->chr_ioctl = tty_serial_ioctl;
1159 qemu_chr_reset(chr);
1162 #else /* ! __linux__ && ! __sun__ */
1163 static CharDriverState *qemu_chr_open_pty(void)
1167 #endif /* __linux__ || __sun__ */
1169 #if defined(__linux__)
1173 } ParallelCharDriver;
1175 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1177 if (s->mode != mode) {
1179 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1186 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1188 ParallelCharDriver *drv = chr->opaque;
1193 case CHR_IOCTL_PP_READ_DATA:
1194 if (ioctl(fd, PPRDATA, &b) < 0)
1196 *(uint8_t *)arg = b;
1198 case CHR_IOCTL_PP_WRITE_DATA:
1199 b = *(uint8_t *)arg;
1200 if (ioctl(fd, PPWDATA, &b) < 0)
1203 case CHR_IOCTL_PP_READ_CONTROL:
1204 if (ioctl(fd, PPRCONTROL, &b) < 0)
1206 /* Linux gives only the lowest bits, and no way to know data
1207 direction! For better compatibility set the fixed upper
1209 *(uint8_t *)arg = b | 0xc0;
1211 case CHR_IOCTL_PP_WRITE_CONTROL:
1212 b = *(uint8_t *)arg;
1213 if (ioctl(fd, PPWCONTROL, &b) < 0)
1216 case CHR_IOCTL_PP_READ_STATUS:
1217 if (ioctl(fd, PPRSTATUS, &b) < 0)
1219 *(uint8_t *)arg = b;
1221 case CHR_IOCTL_PP_DATA_DIR:
1222 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1225 case CHR_IOCTL_PP_EPP_READ_ADDR:
1226 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1227 struct ParallelIOArg *parg = arg;
1228 int n = read(fd, parg->buffer, parg->count);
1229 if (n != parg->count) {
1234 case CHR_IOCTL_PP_EPP_READ:
1235 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1236 struct ParallelIOArg *parg = arg;
1237 int n = read(fd, parg->buffer, parg->count);
1238 if (n != parg->count) {
1243 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1244 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1245 struct ParallelIOArg *parg = arg;
1246 int n = write(fd, parg->buffer, parg->count);
1247 if (n != parg->count) {
1252 case CHR_IOCTL_PP_EPP_WRITE:
1253 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1254 struct ParallelIOArg *parg = arg;
1255 int n = write(fd, parg->buffer, parg->count);
1256 if (n != parg->count) {
1267 static void pp_close(CharDriverState *chr)
1269 ParallelCharDriver *drv = chr->opaque;
1272 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1273 ioctl(fd, PPRELEASE);
1276 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1279 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1281 const char *filename = qemu_opt_get(opts, "path");
1282 CharDriverState *chr;
1283 ParallelCharDriver *drv;
1286 TFR(fd = open(filename, O_RDWR));
1290 if (ioctl(fd, PPCLAIM) < 0) {
1295 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1297 drv->mode = IEEE1284_MODE_COMPAT;
1299 chr = qemu_mallocz(sizeof(CharDriverState));
1300 chr->chr_write = null_chr_write;
1301 chr->chr_ioctl = pp_ioctl;
1302 chr->chr_close = pp_close;
1305 qemu_chr_reset(chr);
1309 #endif /* __linux__ */
1311 #if defined(__FreeBSD__) || defined(__DragonFly__)
1312 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1314 int fd = (int)chr->opaque;
1318 case CHR_IOCTL_PP_READ_DATA:
1319 if (ioctl(fd, PPIGDATA, &b) < 0)
1321 *(uint8_t *)arg = b;
1323 case CHR_IOCTL_PP_WRITE_DATA:
1324 b = *(uint8_t *)arg;
1325 if (ioctl(fd, PPISDATA, &b) < 0)
1328 case CHR_IOCTL_PP_READ_CONTROL:
1329 if (ioctl(fd, PPIGCTRL, &b) < 0)
1331 *(uint8_t *)arg = b;
1333 case CHR_IOCTL_PP_WRITE_CONTROL:
1334 b = *(uint8_t *)arg;
1335 if (ioctl(fd, PPISCTRL, &b) < 0)
1338 case CHR_IOCTL_PP_READ_STATUS:
1339 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1341 *(uint8_t *)arg = b;
1349 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1351 const char *filename = qemu_opt_get(opts, "path");
1352 CharDriverState *chr;
1355 fd = open(filename, O_RDWR);
1359 chr = qemu_mallocz(sizeof(CharDriverState));
1360 chr->opaque = (void *)fd;
1361 chr->chr_write = null_chr_write;
1362 chr->chr_ioctl = pp_ioctl;
1371 HANDLE hcom, hrecv, hsend;
1372 OVERLAPPED orecv, osend;
1377 #define NSENDBUF 2048
1378 #define NRECVBUF 2048
1379 #define MAXCONNECT 1
1380 #define NTIMEOUT 5000
1382 static int win_chr_poll(void *opaque);
1383 static int win_chr_pipe_poll(void *opaque);
1385 static void win_chr_close(CharDriverState *chr)
1387 WinCharState *s = chr->opaque;
1390 CloseHandle(s->hsend);
1394 CloseHandle(s->hrecv);
1398 CloseHandle(s->hcom);
1402 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1404 qemu_del_polling_cb(win_chr_poll, chr);
1406 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1409 static int win_chr_init(CharDriverState *chr, const char *filename)
1411 WinCharState *s = chr->opaque;
1413 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1418 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1420 fprintf(stderr, "Failed CreateEvent\n");
1423 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1425 fprintf(stderr, "Failed CreateEvent\n");
1429 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1430 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1431 if (s->hcom == INVALID_HANDLE_VALUE) {
1432 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1437 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1438 fprintf(stderr, "Failed SetupComm\n");
1442 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1443 size = sizeof(COMMCONFIG);
1444 GetDefaultCommConfig(filename, &comcfg, &size);
1445 comcfg.dcb.DCBlength = sizeof(DCB);
1446 CommConfigDialog(filename, NULL, &comcfg);
1448 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1449 fprintf(stderr, "Failed SetCommState\n");
1453 if (!SetCommMask(s->hcom, EV_ERR)) {
1454 fprintf(stderr, "Failed SetCommMask\n");
1458 cto.ReadIntervalTimeout = MAXDWORD;
1459 if (!SetCommTimeouts(s->hcom, &cto)) {
1460 fprintf(stderr, "Failed SetCommTimeouts\n");
1464 if (!ClearCommError(s->hcom, &err, &comstat)) {
1465 fprintf(stderr, "Failed ClearCommError\n");
1468 qemu_add_polling_cb(win_chr_poll, chr);
1476 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1478 WinCharState *s = chr->opaque;
1479 DWORD len, ret, size, err;
1482 ZeroMemory(&s->osend, sizeof(s->osend));
1483 s->osend.hEvent = s->hsend;
1486 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1488 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1490 err = GetLastError();
1491 if (err == ERROR_IO_PENDING) {
1492 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1510 static int win_chr_read_poll(CharDriverState *chr)
1512 WinCharState *s = chr->opaque;
1514 s->max_size = qemu_chr_can_read(chr);
1518 static void win_chr_readfile(CharDriverState *chr)
1520 WinCharState *s = chr->opaque;
1525 ZeroMemory(&s->orecv, sizeof(s->orecv));
1526 s->orecv.hEvent = s->hrecv;
1527 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1529 err = GetLastError();
1530 if (err == ERROR_IO_PENDING) {
1531 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1536 qemu_chr_read(chr, buf, size);
1540 static void win_chr_read(CharDriverState *chr)
1542 WinCharState *s = chr->opaque;
1544 if (s->len > s->max_size)
1545 s->len = s->max_size;
1549 win_chr_readfile(chr);
1552 static int win_chr_poll(void *opaque)
1554 CharDriverState *chr = opaque;
1555 WinCharState *s = chr->opaque;
1559 ClearCommError(s->hcom, &comerr, &status);
1560 if (status.cbInQue > 0) {
1561 s->len = status.cbInQue;
1562 win_chr_read_poll(chr);
1569 static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1571 const char *filename = qemu_opt_get(opts, "path");
1572 CharDriverState *chr;
1575 chr = qemu_mallocz(sizeof(CharDriverState));
1576 s = qemu_mallocz(sizeof(WinCharState));
1578 chr->chr_write = win_chr_write;
1579 chr->chr_close = win_chr_close;
1581 if (win_chr_init(chr, filename) < 0) {
1586 qemu_chr_reset(chr);
1590 static int win_chr_pipe_poll(void *opaque)
1592 CharDriverState *chr = opaque;
1593 WinCharState *s = chr->opaque;
1596 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1599 win_chr_read_poll(chr);
1606 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1608 WinCharState *s = chr->opaque;
1616 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1618 fprintf(stderr, "Failed CreateEvent\n");
1621 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1623 fprintf(stderr, "Failed CreateEvent\n");
1627 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1628 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1629 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1631 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1632 if (s->hcom == INVALID_HANDLE_VALUE) {
1633 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1638 ZeroMemory(&ov, sizeof(ov));
1639 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1640 ret = ConnectNamedPipe(s->hcom, &ov);
1642 fprintf(stderr, "Failed ConnectNamedPipe\n");
1646 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1648 fprintf(stderr, "Failed GetOverlappedResult\n");
1650 CloseHandle(ov.hEvent);
1657 CloseHandle(ov.hEvent);
1660 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1669 static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
1671 const char *filename = qemu_opt_get(opts, "path");
1672 CharDriverState *chr;
1675 chr = qemu_mallocz(sizeof(CharDriverState));
1676 s = qemu_mallocz(sizeof(WinCharState));
1678 chr->chr_write = win_chr_write;
1679 chr->chr_close = win_chr_close;
1681 if (win_chr_pipe_init(chr, filename) < 0) {
1686 qemu_chr_reset(chr);
1690 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1692 CharDriverState *chr;
1695 chr = qemu_mallocz(sizeof(CharDriverState));
1696 s = qemu_mallocz(sizeof(WinCharState));
1699 chr->chr_write = win_chr_write;
1700 qemu_chr_reset(chr);
1704 static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
1706 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1709 static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
1711 const char *file_out = qemu_opt_get(opts, "path");
1714 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1715 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1716 if (fd_out == INVALID_HANDLE_VALUE)
1719 return qemu_chr_open_win_file(fd_out);
1721 #endif /* !_WIN32 */
1723 /***********************************************************/
1724 /* UDP Net console */
1734 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1736 NetCharDriver *s = chr->opaque;
1738 return send(s->fd, (const void *)buf, len, 0);
1741 static int udp_chr_read_poll(void *opaque)
1743 CharDriverState *chr = opaque;
1744 NetCharDriver *s = chr->opaque;
1746 s->max_size = qemu_chr_can_read(chr);
1748 /* If there were any stray characters in the queue process them
1751 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1752 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1754 s->max_size = qemu_chr_can_read(chr);
1759 static void udp_chr_read(void *opaque)
1761 CharDriverState *chr = opaque;
1762 NetCharDriver *s = chr->opaque;
1764 if (s->max_size == 0)
1766 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1767 s->bufptr = s->bufcnt;
1772 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1773 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1775 s->max_size = qemu_chr_can_read(chr);
1779 static void udp_chr_update_read_handler(CharDriverState *chr)
1781 NetCharDriver *s = chr->opaque;
1784 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1785 udp_chr_read, NULL, chr);
1789 static void udp_chr_close(CharDriverState *chr)
1791 NetCharDriver *s = chr->opaque;
1793 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1797 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1800 static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
1802 CharDriverState *chr = NULL;
1803 NetCharDriver *s = NULL;
1806 chr = qemu_mallocz(sizeof(CharDriverState));
1807 s = qemu_mallocz(sizeof(NetCharDriver));
1809 fd = inet_dgram_opts(opts);
1811 fprintf(stderr, "inet_dgram_opts failed\n");
1819 chr->chr_write = udp_chr_write;
1820 chr->chr_update_read_handler = udp_chr_update_read_handler;
1821 chr->chr_close = udp_chr_close;
1834 /***********************************************************/
1835 /* TCP Net console */
1847 static void tcp_chr_accept(void *opaque);
1849 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1851 TCPCharDriver *s = chr->opaque;
1853 return send_all(s->fd, buf, len);
1855 /* XXX: indicate an error ? */
1860 static int tcp_chr_read_poll(void *opaque)
1862 CharDriverState *chr = opaque;
1863 TCPCharDriver *s = chr->opaque;
1866 s->max_size = qemu_chr_can_read(chr);
1871 #define IAC_BREAK 243
1872 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1874 uint8_t *buf, int *size)
1876 /* Handle any telnet client's basic IAC options to satisfy char by
1877 * char mode with no echo. All IAC options will be removed from
1878 * the buf and the do_telnetopt variable will be used to track the
1879 * state of the width of the IAC information.
1881 * IAC commands come in sets of 3 bytes with the exception of the
1882 * "IAC BREAK" command and the double IAC.
1888 for (i = 0; i < *size; i++) {
1889 if (s->do_telnetopt > 1) {
1890 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1891 /* Double IAC means send an IAC */
1895 s->do_telnetopt = 1;
1897 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1898 /* Handle IAC break commands by sending a serial break */
1899 qemu_chr_event(chr, CHR_EVENT_BREAK);
1904 if (s->do_telnetopt >= 4) {
1905 s->do_telnetopt = 1;
1908 if ((unsigned char)buf[i] == IAC) {
1909 s->do_telnetopt = 2;
1920 static int tcp_get_msgfd(CharDriverState *chr)
1922 TCPCharDriver *s = chr->opaque;
1928 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1930 TCPCharDriver *s = chr->opaque;
1931 struct cmsghdr *cmsg;
1933 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1936 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1937 cmsg->cmsg_level != SOL_SOCKET ||
1938 cmsg->cmsg_type != SCM_RIGHTS)
1941 fd = *((int *)CMSG_DATA(cmsg));
1951 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1953 TCPCharDriver *s = chr->opaque;
1954 struct msghdr msg = { NULL, };
1955 struct iovec iov[1];
1957 struct cmsghdr cmsg;
1958 char control[CMSG_SPACE(sizeof(int))];
1962 iov[0].iov_base = buf;
1963 iov[0].iov_len = len;
1967 msg.msg_control = &msg_control;
1968 msg.msg_controllen = sizeof(msg_control);
1970 ret = recvmsg(s->fd, &msg, 0);
1971 if (ret > 0 && s->is_unix)
1972 unix_process_msgfd(chr, &msg);
1977 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1979 TCPCharDriver *s = chr->opaque;
1980 return recv(s->fd, buf, len, 0);
1984 static void tcp_chr_read(void *opaque)
1986 CharDriverState *chr = opaque;
1987 TCPCharDriver *s = chr->opaque;
1991 if (!s->connected || s->max_size <= 0)
1994 if (len > s->max_size)
1996 size = tcp_chr_recv(chr, (void *)buf, len);
1998 /* connection closed */
2000 if (s->listen_fd >= 0) {
2001 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2003 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2006 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2007 } else if (size > 0) {
2008 if (s->do_telnetopt)
2009 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2011 qemu_chr_read(chr, buf, size);
2012 if (s->msgfd != -1) {
2019 static void tcp_chr_connect(void *opaque)
2021 CharDriverState *chr = opaque;
2022 TCPCharDriver *s = chr->opaque;
2025 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2026 tcp_chr_read, NULL, chr);
2027 qemu_chr_reset(chr);
2030 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2031 static void tcp_chr_telnet_init(int fd)
2034 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2035 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2036 send(fd, (char *)buf, 3, 0);
2037 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2038 send(fd, (char *)buf, 3, 0);
2039 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2040 send(fd, (char *)buf, 3, 0);
2041 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2042 send(fd, (char *)buf, 3, 0);
2045 static void socket_set_nodelay(int fd)
2048 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2051 static void tcp_chr_accept(void *opaque)
2053 CharDriverState *chr = opaque;
2054 TCPCharDriver *s = chr->opaque;
2055 struct sockaddr_in saddr;
2057 struct sockaddr_un uaddr;
2059 struct sockaddr *addr;
2066 len = sizeof(uaddr);
2067 addr = (struct sockaddr *)&uaddr;
2071 len = sizeof(saddr);
2072 addr = (struct sockaddr *)&saddr;
2074 fd = accept(s->listen_fd, addr, &len);
2075 if (fd < 0 && errno != EINTR) {
2077 } else if (fd >= 0) {
2078 if (s->do_telnetopt)
2079 tcp_chr_telnet_init(fd);
2083 socket_set_nonblock(fd);
2085 socket_set_nodelay(fd);
2087 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2088 tcp_chr_connect(chr);
2091 static void tcp_chr_close(CharDriverState *chr)
2093 TCPCharDriver *s = chr->opaque;
2095 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2098 if (s->listen_fd >= 0) {
2099 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2100 closesocket(s->listen_fd);
2103 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2106 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2108 CharDriverState *chr = NULL;
2109 TCPCharDriver *s = NULL;
2117 is_listen = qemu_opt_get_bool(opts, "server", 0);
2118 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2119 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2120 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2121 is_unix = qemu_opt_get(opts, "path") != NULL;
2125 chr = qemu_mallocz(sizeof(CharDriverState));
2126 s = qemu_mallocz(sizeof(TCPCharDriver));
2130 fd = unix_listen_opts(opts);
2132 fd = unix_connect_opts(opts);
2136 fd = inet_listen_opts(opts, 0);
2138 fd = inet_connect_opts(opts);
2144 if (!is_waitconnect)
2145 socket_set_nonblock(fd);
2151 s->is_unix = is_unix;
2152 s->do_nodelay = do_nodelay && !is_unix;
2155 chr->chr_write = tcp_chr_write;
2156 chr->chr_close = tcp_chr_close;
2157 chr->get_msgfd = tcp_get_msgfd;
2161 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2163 s->do_telnetopt = 1;
2168 socket_set_nodelay(fd);
2169 tcp_chr_connect(chr);
2172 /* for "info chardev" monitor command */
2173 chr->filename = qemu_malloc(256);
2175 snprintf(chr->filename, 256, "unix:%s%s",
2176 qemu_opt_get(opts, "path"),
2177 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2178 } else if (is_telnet) {
2179 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2180 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2181 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2183 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2184 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2185 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2188 if (is_listen && is_waitconnect) {
2189 printf("QEMU waiting for connection on: %s\n",
2191 tcp_chr_accept(chr);
2192 socket_set_nonblock(s->listen_fd);
2204 static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2206 char host[65], port[33], width[8], height[8];
2211 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2215 if (strstart(filename, "mon:", &p)) {
2217 qemu_opt_set(opts, "mux", "on");
2220 if (strcmp(filename, "null") == 0 ||
2221 strcmp(filename, "pty") == 0 ||
2222 strcmp(filename, "msmouse") == 0 ||
2223 strcmp(filename, "braille") == 0 ||
2224 strcmp(filename, "stdio") == 0) {
2225 qemu_opt_set(opts, "backend", filename);
2228 if (strstart(filename, "vc", &p)) {
2229 qemu_opt_set(opts, "backend", "vc");
2231 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2233 qemu_opt_set(opts, "width", width);
2234 qemu_opt_set(opts, "height", height);
2235 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2237 qemu_opt_set(opts, "cols", width);
2238 qemu_opt_set(opts, "rows", height);
2245 if (strcmp(filename, "con:") == 0) {
2246 qemu_opt_set(opts, "backend", "console");
2249 if (strstart(filename, "COM", NULL)) {
2250 qemu_opt_set(opts, "backend", "serial");
2251 qemu_opt_set(opts, "path", filename);
2254 if (strstart(filename, "file:", &p)) {
2255 qemu_opt_set(opts, "backend", "file");
2256 qemu_opt_set(opts, "path", p);
2259 if (strstart(filename, "pipe:", &p)) {
2260 qemu_opt_set(opts, "backend", "pipe");
2261 qemu_opt_set(opts, "path", p);
2264 if (strstart(filename, "tcp:", &p) ||
2265 strstart(filename, "telnet:", &p)) {
2266 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2268 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2271 qemu_opt_set(opts, "backend", "socket");
2272 qemu_opt_set(opts, "host", host);
2273 qemu_opt_set(opts, "port", port);
2274 if (p[pos] == ',') {
2275 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2278 if (strstart(filename, "telnet:", &p))
2279 qemu_opt_set(opts, "telnet", "on");
2282 if (strstart(filename, "udp:", &p)) {
2283 qemu_opt_set(opts, "backend", "udp");
2284 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2286 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2287 fprintf(stderr, "udp #1\n");
2291 qemu_opt_set(opts, "host", host);
2292 qemu_opt_set(opts, "port", port);
2293 if (p[pos] == '@') {
2295 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2297 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2298 fprintf(stderr, "udp #2\n");
2302 qemu_opt_set(opts, "localaddr", host);
2303 qemu_opt_set(opts, "localport", port);
2307 if (strstart(filename, "unix:", &p)) {
2308 qemu_opt_set(opts, "backend", "socket");
2309 if (qemu_opts_do_parse(opts, p, "path") != 0)
2313 if (strstart(filename, "/dev/parport", NULL) ||
2314 strstart(filename, "/dev/ppi", NULL)) {
2315 qemu_opt_set(opts, "backend", "parport");
2316 qemu_opt_set(opts, "path", filename);
2319 if (strstart(filename, "/dev/", NULL)) {
2320 qemu_opt_set(opts, "backend", "tty");
2321 qemu_opt_set(opts, "path", filename);
2326 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
2327 qemu_opts_del(opts);
2331 static const struct {
2333 CharDriverState *(*open)(QemuOpts *opts);
2334 } backend_table[] = {
2335 { .name = "null", .open = qemu_chr_open_null },
2336 { .name = "socket", .open = qemu_chr_open_socket },
2337 { .name = "udp", .open = qemu_chr_open_udp },
2338 { .name = "msmouse", .open = qemu_chr_open_msmouse },
2339 { .name = "vc", .open = text_console_init },
2341 { .name = "file", .open = qemu_chr_open_win_file_out },
2342 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2343 { .name = "console", .open = qemu_chr_open_win_con },
2344 { .name = "serial", .open = qemu_chr_open_win },
2346 { .name = "file", .open = qemu_chr_open_file_out },
2347 { .name = "pipe", .open = qemu_chr_open_pipe },
2348 { .name = "pty", .open = qemu_chr_open_pty },
2349 { .name = "stdio", .open = qemu_chr_open_stdio },
2351 #ifdef CONFIG_BRLAPI
2352 { .name = "braille", .open = chr_baum_init },
2354 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2355 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2356 { .name = "tty", .open = qemu_chr_open_tty },
2358 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2359 { .name = "parport", .open = qemu_chr_open_pp },
2363 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2364 void (*init)(struct CharDriverState *s))
2366 CharDriverState *chr;
2369 if (qemu_opts_id(opts) == NULL) {
2370 fprintf(stderr, "chardev: no id specified\n");
2374 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2375 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2378 if (i == ARRAY_SIZE(backend_table)) {
2379 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2380 qemu_opt_get(opts, "backend"));
2384 chr = backend_table[i].open(opts);
2386 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2387 qemu_opt_get(opts, "backend"));
2392 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2394 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2396 if (qemu_opt_get_bool(opts, "mux", 0)) {
2397 CharDriverState *base = chr;
2398 int len = strlen(qemu_opts_id(opts)) + 6;
2399 base->label = qemu_malloc(len);
2400 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2401 chr = qemu_chr_open_mux(base);
2402 chr->filename = base->filename;
2403 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2405 chr->label = qemu_strdup(qemu_opts_id(opts));
2409 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2412 CharDriverState *chr;
2415 if (strstart(filename, "chardev:", &p)) {
2416 return qemu_chr_find(p);
2419 opts = qemu_chr_parse_compat(label, filename);
2423 chr = qemu_chr_open_opts(opts, init);
2424 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2425 monitor_init(chr, MONITOR_USE_READLINE);
2430 void qemu_chr_close(CharDriverState *chr)
2432 QTAILQ_REMOVE(&chardevs, chr, next);
2434 chr->chr_close(chr);
2435 qemu_free(chr->filename);
2436 qemu_free(chr->label);
2440 void qemu_chr_info(Monitor *mon)
2442 CharDriverState *chr;
2444 QTAILQ_FOREACH(chr, &chardevs, next) {
2445 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
2449 CharDriverState *qemu_chr_find(const char *name)
2451 CharDriverState *chr;
2453 QTAILQ_FOREACH(chr, &chardevs, next) {
2454 if (strcmp(chr->label, name) != 0)