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 if (chr->focus != -1) {
460 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_OUT);
462 chr->focus = d->mux_cnt;
464 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_IN);
467 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
469 CharDriverState *chr;
472 chr = qemu_mallocz(sizeof(CharDriverState));
473 d = qemu_mallocz(sizeof(MuxDriver));
478 chr->chr_write = mux_chr_write;
479 chr->chr_update_read_handler = mux_chr_update_read_handler;
480 chr->chr_accept_input = mux_chr_accept_input;
486 int send_all(int fd, const void *buf, int len1)
492 ret = send(fd, buf, len, 0);
494 errno = WSAGetLastError();
495 if (errno != WSAEWOULDBLOCK) {
498 } else if (ret == 0) {
510 static int unix_write(int fd, const uint8_t *buf, int len1)
516 ret = write(fd, buf, len);
518 if (errno != EINTR && errno != EAGAIN)
520 } else if (ret == 0) {
530 int send_all(int fd, const void *buf, int len1)
532 return unix_write(fd, buf, len1);
543 #define STDIO_MAX_CLIENTS 1
544 static int stdio_nb_clients = 0;
546 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
548 FDCharDriver *s = chr->opaque;
549 return send_all(s->fd_out, buf, len);
552 static int fd_chr_read_poll(void *opaque)
554 CharDriverState *chr = opaque;
555 FDCharDriver *s = chr->opaque;
557 s->max_size = qemu_chr_can_read(chr);
561 static void fd_chr_read(void *opaque)
563 CharDriverState *chr = opaque;
564 FDCharDriver *s = chr->opaque;
569 if (len > s->max_size)
573 size = read(s->fd_in, buf, len);
575 /* FD has been closed. Remove it from the active list. */
576 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
577 qemu_chr_event(chr, CHR_EVENT_CLOSED);
581 qemu_chr_read(chr, buf, size);
585 static void fd_chr_update_read_handler(CharDriverState *chr)
587 FDCharDriver *s = chr->opaque;
590 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
592 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
593 fd_chr_read, NULL, chr);
598 static void fd_chr_close(struct CharDriverState *chr)
600 FDCharDriver *s = chr->opaque;
603 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
605 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
610 qemu_chr_event(chr, CHR_EVENT_CLOSED);
613 /* open a character device to a unix fd */
614 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
616 CharDriverState *chr;
619 chr = qemu_mallocz(sizeof(CharDriverState));
620 s = qemu_mallocz(sizeof(FDCharDriver));
624 chr->chr_write = fd_chr_write;
625 chr->chr_update_read_handler = fd_chr_update_read_handler;
626 chr->chr_close = fd_chr_close;
633 static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
637 TFR(fd_out = open(qemu_opt_get(opts, "path"),
638 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
641 return qemu_chr_open_fd(-1, fd_out);
644 static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
647 char filename_in[256], filename_out[256];
648 const char *filename = qemu_opt_get(opts, "path");
650 if (filename == NULL) {
651 fprintf(stderr, "chardev: pipe: no filename given\n");
655 snprintf(filename_in, 256, "%s.in", filename);
656 snprintf(filename_out, 256, "%s.out", filename);
657 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
658 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
659 if (fd_in < 0 || fd_out < 0) {
664 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
668 return qemu_chr_open_fd(fd_in, fd_out);
672 /* for STDIO, we handle the case where several clients use it
675 #define TERM_FIFO_MAX_SIZE 1
677 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
678 static int term_fifo_size;
680 static int stdio_read_poll(void *opaque)
682 CharDriverState *chr = opaque;
684 /* try to flush the queue if needed */
685 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
686 qemu_chr_read(chr, term_fifo, 1);
689 /* see if we can absorb more chars */
690 if (term_fifo_size == 0)
696 static void stdio_read(void *opaque)
700 CharDriverState *chr = opaque;
702 size = read(0, buf, 1);
704 /* stdin has been closed. Remove it from the active list. */
705 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
706 qemu_chr_event(chr, CHR_EVENT_CLOSED);
710 if (qemu_chr_can_read(chr) > 0) {
711 qemu_chr_read(chr, buf, 1);
712 } else if (term_fifo_size == 0) {
713 term_fifo[term_fifo_size++] = buf[0];
718 /* init terminal so that we can grab keys */
719 static struct termios oldtty;
720 static int old_fd0_flags;
721 static int term_atexit_done;
723 static void term_exit(void)
725 tcsetattr (0, TCSANOW, &oldtty);
726 fcntl(0, F_SETFL, old_fd0_flags);
729 static void term_init(void)
735 old_fd0_flags = fcntl(0, F_GETFL);
737 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
738 |INLCR|IGNCR|ICRNL|IXON);
739 tty.c_oflag |= OPOST;
740 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
741 /* if graphical mode, we allow Ctrl-C handling */
742 if (display_type == DT_NOGRAPHIC)
743 tty.c_lflag &= ~ISIG;
744 tty.c_cflag &= ~(CSIZE|PARENB);
749 tcsetattr (0, TCSANOW, &tty);
751 if (!term_atexit_done++)
754 fcntl(0, F_SETFL, O_NONBLOCK);
757 static void qemu_chr_close_stdio(struct CharDriverState *chr)
761 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
765 static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
767 CharDriverState *chr;
769 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
771 chr = qemu_chr_open_fd(0, 1);
772 chr->chr_close = qemu_chr_close_stdio;
773 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
781 /* Once Solaris has openpty(), this is going to be removed. */
782 static int openpty(int *amaster, int *aslave, char *name,
783 struct termios *termp, struct winsize *winp)
786 int mfd = -1, sfd = -1;
788 *amaster = *aslave = -1;
790 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
794 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
797 if ((slave = ptsname(mfd)) == NULL)
800 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
803 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
804 (termp != NULL && tcgetattr(sfd, termp) < 0))
812 ioctl(sfd, TIOCSWINSZ, winp);
823 static void cfmakeraw (struct termios *termios_p)
825 termios_p->c_iflag &=
826 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
827 termios_p->c_oflag &= ~OPOST;
828 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
829 termios_p->c_cflag &= ~(CSIZE|PARENB);
830 termios_p->c_cflag |= CS8;
832 termios_p->c_cc[VMIN] = 0;
833 termios_p->c_cc[VTIME] = 0;
837 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
838 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
848 static void pty_chr_update_read_handler(CharDriverState *chr);
849 static void pty_chr_state(CharDriverState *chr, int connected);
851 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
853 PtyCharDriver *s = chr->opaque;
856 /* guest sends data, check for (re-)connect */
857 pty_chr_update_read_handler(chr);
860 return send_all(s->fd, buf, len);
863 static int pty_chr_read_poll(void *opaque)
865 CharDriverState *chr = opaque;
866 PtyCharDriver *s = chr->opaque;
868 s->read_bytes = qemu_chr_can_read(chr);
869 return s->read_bytes;
872 static void pty_chr_read(void *opaque)
874 CharDriverState *chr = opaque;
875 PtyCharDriver *s = chr->opaque;
880 if (len > s->read_bytes)
884 size = read(s->fd, buf, len);
885 if ((size == -1 && errno == EIO) ||
887 pty_chr_state(chr, 0);
891 pty_chr_state(chr, 1);
892 qemu_chr_read(chr, buf, size);
896 static void pty_chr_update_read_handler(CharDriverState *chr)
898 PtyCharDriver *s = chr->opaque;
900 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
901 pty_chr_read, NULL, chr);
904 * Short timeout here: just need wait long enougth that qemu makes
905 * it through the poll loop once. When reconnected we want a
906 * short timeout so we notice it almost instantly. Otherwise
907 * read() gives us -EIO instantly, making pty_chr_state() reset the
908 * timeout to the normal (much longer) poll interval before the
911 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
914 static void pty_chr_state(CharDriverState *chr, int connected)
916 PtyCharDriver *s = chr->opaque;
919 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
922 /* (re-)connect poll interval for idle guests: once per second.
923 * We check more frequently in case the guests sends data to
924 * the virtual device linked to our pty. */
925 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
933 static void pty_chr_timer(void *opaque)
935 struct CharDriverState *chr = opaque;
936 PtyCharDriver *s = chr->opaque;
941 /* If we arrive here without polling being cleared due
942 * read returning -EIO, then we are (re-)connected */
943 pty_chr_state(chr, 1);
948 pty_chr_update_read_handler(chr);
951 static void pty_chr_close(struct CharDriverState *chr)
953 PtyCharDriver *s = chr->opaque;
955 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
957 qemu_del_timer(s->timer);
958 qemu_free_timer(s->timer);
960 qemu_chr_event(chr, CHR_EVENT_CLOSED);
963 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
965 CharDriverState *chr;
969 #if defined(__OpenBSD__) || defined(__DragonFly__)
970 char pty_name[PATH_MAX];
971 #define q_ptsname(x) pty_name
973 char *pty_name = NULL;
974 #define q_ptsname(x) ptsname(x)
977 chr = qemu_mallocz(sizeof(CharDriverState));
978 s = qemu_mallocz(sizeof(PtyCharDriver));
980 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
984 /* Set raw attributes on the pty. */
985 tcgetattr(slave_fd, &tty);
987 tcsetattr(slave_fd, TCSAFLUSH, &tty);
990 len = strlen(q_ptsname(s->fd)) + 5;
991 chr->filename = qemu_malloc(len);
992 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
993 qemu_opt_set(opts, "path", q_ptsname(s->fd));
994 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
997 chr->chr_write = pty_chr_write;
998 chr->chr_update_read_handler = pty_chr_update_read_handler;
999 chr->chr_close = pty_chr_close;
1001 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
1006 static void tty_serial_init(int fd, int speed,
1007 int parity, int data_bits, int stop_bits)
1013 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1014 speed, parity, data_bits, stop_bits);
1016 tcgetattr (fd, &tty);
1019 if (speed <= 50 * MARGIN)
1021 else if (speed <= 75 * MARGIN)
1023 else if (speed <= 300 * MARGIN)
1025 else if (speed <= 600 * MARGIN)
1027 else if (speed <= 1200 * MARGIN)
1029 else if (speed <= 2400 * MARGIN)
1031 else if (speed <= 4800 * MARGIN)
1033 else if (speed <= 9600 * MARGIN)
1035 else if (speed <= 19200 * MARGIN)
1037 else if (speed <= 38400 * MARGIN)
1039 else if (speed <= 57600 * MARGIN)
1041 else if (speed <= 115200 * MARGIN)
1046 cfsetispeed(&tty, spd);
1047 cfsetospeed(&tty, spd);
1049 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1050 |INLCR|IGNCR|ICRNL|IXON);
1051 tty.c_oflag |= OPOST;
1052 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1053 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1074 tty.c_cflag |= PARENB;
1077 tty.c_cflag |= PARENB | PARODD;
1081 tty.c_cflag |= CSTOPB;
1083 tcsetattr (fd, TCSANOW, &tty);
1086 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1088 FDCharDriver *s = chr->opaque;
1091 case CHR_IOCTL_SERIAL_SET_PARAMS:
1093 QEMUSerialSetParams *ssp = arg;
1094 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1095 ssp->data_bits, ssp->stop_bits);
1098 case CHR_IOCTL_SERIAL_SET_BREAK:
1100 int enable = *(int *)arg;
1102 tcsendbreak(s->fd_in, 1);
1105 case CHR_IOCTL_SERIAL_GET_TIOCM:
1108 int *targ = (int *)arg;
1109 ioctl(s->fd_in, TIOCMGET, &sarg);
1111 if (sarg & TIOCM_CTS)
1112 *targ |= CHR_TIOCM_CTS;
1113 if (sarg & TIOCM_CAR)
1114 *targ |= CHR_TIOCM_CAR;
1115 if (sarg & TIOCM_DSR)
1116 *targ |= CHR_TIOCM_DSR;
1117 if (sarg & TIOCM_RI)
1118 *targ |= CHR_TIOCM_RI;
1119 if (sarg & TIOCM_DTR)
1120 *targ |= CHR_TIOCM_DTR;
1121 if (sarg & TIOCM_RTS)
1122 *targ |= CHR_TIOCM_RTS;
1125 case CHR_IOCTL_SERIAL_SET_TIOCM:
1127 int sarg = *(int *)arg;
1129 ioctl(s->fd_in, TIOCMGET, &targ);
1130 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1131 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1132 if (sarg & CHR_TIOCM_CTS)
1134 if (sarg & CHR_TIOCM_CAR)
1136 if (sarg & CHR_TIOCM_DSR)
1138 if (sarg & CHR_TIOCM_RI)
1140 if (sarg & CHR_TIOCM_DTR)
1142 if (sarg & CHR_TIOCM_RTS)
1144 ioctl(s->fd_in, TIOCMSET, &targ);
1153 static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
1155 const char *filename = qemu_opt_get(opts, "path");
1156 CharDriverState *chr;
1159 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1160 tty_serial_init(fd, 115200, 'N', 8, 1);
1161 chr = qemu_chr_open_fd(fd, fd);
1166 chr->chr_ioctl = tty_serial_ioctl;
1167 qemu_chr_reset(chr);
1170 #else /* ! __linux__ && ! __sun__ */
1171 static CharDriverState *qemu_chr_open_pty(void)
1175 #endif /* __linux__ || __sun__ */
1177 #if defined(__linux__)
1181 } ParallelCharDriver;
1183 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1185 if (s->mode != mode) {
1187 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1194 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1196 ParallelCharDriver *drv = chr->opaque;
1201 case CHR_IOCTL_PP_READ_DATA:
1202 if (ioctl(fd, PPRDATA, &b) < 0)
1204 *(uint8_t *)arg = b;
1206 case CHR_IOCTL_PP_WRITE_DATA:
1207 b = *(uint8_t *)arg;
1208 if (ioctl(fd, PPWDATA, &b) < 0)
1211 case CHR_IOCTL_PP_READ_CONTROL:
1212 if (ioctl(fd, PPRCONTROL, &b) < 0)
1214 /* Linux gives only the lowest bits, and no way to know data
1215 direction! For better compatibility set the fixed upper
1217 *(uint8_t *)arg = b | 0xc0;
1219 case CHR_IOCTL_PP_WRITE_CONTROL:
1220 b = *(uint8_t *)arg;
1221 if (ioctl(fd, PPWCONTROL, &b) < 0)
1224 case CHR_IOCTL_PP_READ_STATUS:
1225 if (ioctl(fd, PPRSTATUS, &b) < 0)
1227 *(uint8_t *)arg = b;
1229 case CHR_IOCTL_PP_DATA_DIR:
1230 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1233 case CHR_IOCTL_PP_EPP_READ_ADDR:
1234 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1235 struct ParallelIOArg *parg = arg;
1236 int n = read(fd, parg->buffer, parg->count);
1237 if (n != parg->count) {
1242 case CHR_IOCTL_PP_EPP_READ:
1243 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1244 struct ParallelIOArg *parg = arg;
1245 int n = read(fd, parg->buffer, parg->count);
1246 if (n != parg->count) {
1251 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1252 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1253 struct ParallelIOArg *parg = arg;
1254 int n = write(fd, parg->buffer, parg->count);
1255 if (n != parg->count) {
1260 case CHR_IOCTL_PP_EPP_WRITE:
1261 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1262 struct ParallelIOArg *parg = arg;
1263 int n = write(fd, parg->buffer, parg->count);
1264 if (n != parg->count) {
1275 static void pp_close(CharDriverState *chr)
1277 ParallelCharDriver *drv = chr->opaque;
1280 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1281 ioctl(fd, PPRELEASE);
1284 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1287 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1289 const char *filename = qemu_opt_get(opts, "path");
1290 CharDriverState *chr;
1291 ParallelCharDriver *drv;
1294 TFR(fd = open(filename, O_RDWR));
1298 if (ioctl(fd, PPCLAIM) < 0) {
1303 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1305 drv->mode = IEEE1284_MODE_COMPAT;
1307 chr = qemu_mallocz(sizeof(CharDriverState));
1308 chr->chr_write = null_chr_write;
1309 chr->chr_ioctl = pp_ioctl;
1310 chr->chr_close = pp_close;
1313 qemu_chr_reset(chr);
1317 #endif /* __linux__ */
1319 #if defined(__FreeBSD__) || defined(__DragonFly__)
1320 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1322 int fd = (int)chr->opaque;
1326 case CHR_IOCTL_PP_READ_DATA:
1327 if (ioctl(fd, PPIGDATA, &b) < 0)
1329 *(uint8_t *)arg = b;
1331 case CHR_IOCTL_PP_WRITE_DATA:
1332 b = *(uint8_t *)arg;
1333 if (ioctl(fd, PPISDATA, &b) < 0)
1336 case CHR_IOCTL_PP_READ_CONTROL:
1337 if (ioctl(fd, PPIGCTRL, &b) < 0)
1339 *(uint8_t *)arg = b;
1341 case CHR_IOCTL_PP_WRITE_CONTROL:
1342 b = *(uint8_t *)arg;
1343 if (ioctl(fd, PPISCTRL, &b) < 0)
1346 case CHR_IOCTL_PP_READ_STATUS:
1347 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1349 *(uint8_t *)arg = b;
1357 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1359 const char *filename = qemu_opt_get(opts, "path");
1360 CharDriverState *chr;
1363 fd = open(filename, O_RDWR);
1367 chr = qemu_mallocz(sizeof(CharDriverState));
1368 chr->opaque = (void *)fd;
1369 chr->chr_write = null_chr_write;
1370 chr->chr_ioctl = pp_ioctl;
1379 HANDLE hcom, hrecv, hsend;
1380 OVERLAPPED orecv, osend;
1385 #define NSENDBUF 2048
1386 #define NRECVBUF 2048
1387 #define MAXCONNECT 1
1388 #define NTIMEOUT 5000
1390 static int win_chr_poll(void *opaque);
1391 static int win_chr_pipe_poll(void *opaque);
1393 static void win_chr_close(CharDriverState *chr)
1395 WinCharState *s = chr->opaque;
1398 CloseHandle(s->hsend);
1402 CloseHandle(s->hrecv);
1406 CloseHandle(s->hcom);
1410 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1412 qemu_del_polling_cb(win_chr_poll, chr);
1414 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1417 static int win_chr_init(CharDriverState *chr, const char *filename)
1419 WinCharState *s = chr->opaque;
1421 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1426 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1428 fprintf(stderr, "Failed CreateEvent\n");
1431 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1433 fprintf(stderr, "Failed CreateEvent\n");
1437 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1438 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1439 if (s->hcom == INVALID_HANDLE_VALUE) {
1440 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1445 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1446 fprintf(stderr, "Failed SetupComm\n");
1450 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1451 size = sizeof(COMMCONFIG);
1452 GetDefaultCommConfig(filename, &comcfg, &size);
1453 comcfg.dcb.DCBlength = sizeof(DCB);
1454 CommConfigDialog(filename, NULL, &comcfg);
1456 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1457 fprintf(stderr, "Failed SetCommState\n");
1461 if (!SetCommMask(s->hcom, EV_ERR)) {
1462 fprintf(stderr, "Failed SetCommMask\n");
1466 cto.ReadIntervalTimeout = MAXDWORD;
1467 if (!SetCommTimeouts(s->hcom, &cto)) {
1468 fprintf(stderr, "Failed SetCommTimeouts\n");
1472 if (!ClearCommError(s->hcom, &err, &comstat)) {
1473 fprintf(stderr, "Failed ClearCommError\n");
1476 qemu_add_polling_cb(win_chr_poll, chr);
1484 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1486 WinCharState *s = chr->opaque;
1487 DWORD len, ret, size, err;
1490 ZeroMemory(&s->osend, sizeof(s->osend));
1491 s->osend.hEvent = s->hsend;
1494 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1496 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1498 err = GetLastError();
1499 if (err == ERROR_IO_PENDING) {
1500 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1518 static int win_chr_read_poll(CharDriverState *chr)
1520 WinCharState *s = chr->opaque;
1522 s->max_size = qemu_chr_can_read(chr);
1526 static void win_chr_readfile(CharDriverState *chr)
1528 WinCharState *s = chr->opaque;
1533 ZeroMemory(&s->orecv, sizeof(s->orecv));
1534 s->orecv.hEvent = s->hrecv;
1535 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1537 err = GetLastError();
1538 if (err == ERROR_IO_PENDING) {
1539 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1544 qemu_chr_read(chr, buf, size);
1548 static void win_chr_read(CharDriverState *chr)
1550 WinCharState *s = chr->opaque;
1552 if (s->len > s->max_size)
1553 s->len = s->max_size;
1557 win_chr_readfile(chr);
1560 static int win_chr_poll(void *opaque)
1562 CharDriverState *chr = opaque;
1563 WinCharState *s = chr->opaque;
1567 ClearCommError(s->hcom, &comerr, &status);
1568 if (status.cbInQue > 0) {
1569 s->len = status.cbInQue;
1570 win_chr_read_poll(chr);
1577 static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1579 const char *filename = qemu_opt_get(opts, "path");
1580 CharDriverState *chr;
1583 chr = qemu_mallocz(sizeof(CharDriverState));
1584 s = qemu_mallocz(sizeof(WinCharState));
1586 chr->chr_write = win_chr_write;
1587 chr->chr_close = win_chr_close;
1589 if (win_chr_init(chr, filename) < 0) {
1594 qemu_chr_reset(chr);
1598 static int win_chr_pipe_poll(void *opaque)
1600 CharDriverState *chr = opaque;
1601 WinCharState *s = chr->opaque;
1604 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1607 win_chr_read_poll(chr);
1614 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1616 WinCharState *s = chr->opaque;
1624 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1626 fprintf(stderr, "Failed CreateEvent\n");
1629 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1631 fprintf(stderr, "Failed CreateEvent\n");
1635 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1636 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1637 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1639 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1640 if (s->hcom == INVALID_HANDLE_VALUE) {
1641 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1646 ZeroMemory(&ov, sizeof(ov));
1647 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1648 ret = ConnectNamedPipe(s->hcom, &ov);
1650 fprintf(stderr, "Failed ConnectNamedPipe\n");
1654 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1656 fprintf(stderr, "Failed GetOverlappedResult\n");
1658 CloseHandle(ov.hEvent);
1665 CloseHandle(ov.hEvent);
1668 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1677 static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
1679 const char *filename = qemu_opt_get(opts, "path");
1680 CharDriverState *chr;
1683 chr = qemu_mallocz(sizeof(CharDriverState));
1684 s = qemu_mallocz(sizeof(WinCharState));
1686 chr->chr_write = win_chr_write;
1687 chr->chr_close = win_chr_close;
1689 if (win_chr_pipe_init(chr, filename) < 0) {
1694 qemu_chr_reset(chr);
1698 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1700 CharDriverState *chr;
1703 chr = qemu_mallocz(sizeof(CharDriverState));
1704 s = qemu_mallocz(sizeof(WinCharState));
1707 chr->chr_write = win_chr_write;
1708 qemu_chr_reset(chr);
1712 static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
1714 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1717 static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
1719 const char *file_out = qemu_opt_get(opts, "path");
1722 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1723 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1724 if (fd_out == INVALID_HANDLE_VALUE)
1727 return qemu_chr_open_win_file(fd_out);
1729 #endif /* !_WIN32 */
1731 /***********************************************************/
1732 /* UDP Net console */
1742 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1744 NetCharDriver *s = chr->opaque;
1746 return send(s->fd, (const void *)buf, len, 0);
1749 static int udp_chr_read_poll(void *opaque)
1751 CharDriverState *chr = opaque;
1752 NetCharDriver *s = chr->opaque;
1754 s->max_size = qemu_chr_can_read(chr);
1756 /* If there were any stray characters in the queue process them
1759 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1760 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1762 s->max_size = qemu_chr_can_read(chr);
1767 static void udp_chr_read(void *opaque)
1769 CharDriverState *chr = opaque;
1770 NetCharDriver *s = chr->opaque;
1772 if (s->max_size == 0)
1774 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1775 s->bufptr = s->bufcnt;
1780 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1781 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1783 s->max_size = qemu_chr_can_read(chr);
1787 static void udp_chr_update_read_handler(CharDriverState *chr)
1789 NetCharDriver *s = chr->opaque;
1792 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1793 udp_chr_read, NULL, chr);
1797 static void udp_chr_close(CharDriverState *chr)
1799 NetCharDriver *s = chr->opaque;
1801 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1805 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1808 static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
1810 CharDriverState *chr = NULL;
1811 NetCharDriver *s = NULL;
1814 chr = qemu_mallocz(sizeof(CharDriverState));
1815 s = qemu_mallocz(sizeof(NetCharDriver));
1817 fd = inet_dgram_opts(opts);
1819 fprintf(stderr, "inet_dgram_opts failed\n");
1827 chr->chr_write = udp_chr_write;
1828 chr->chr_update_read_handler = udp_chr_update_read_handler;
1829 chr->chr_close = udp_chr_close;
1842 /***********************************************************/
1843 /* TCP Net console */
1855 static void tcp_chr_accept(void *opaque);
1857 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1859 TCPCharDriver *s = chr->opaque;
1861 return send_all(s->fd, buf, len);
1863 /* XXX: indicate an error ? */
1868 static int tcp_chr_read_poll(void *opaque)
1870 CharDriverState *chr = opaque;
1871 TCPCharDriver *s = chr->opaque;
1874 s->max_size = qemu_chr_can_read(chr);
1879 #define IAC_BREAK 243
1880 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1882 uint8_t *buf, int *size)
1884 /* Handle any telnet client's basic IAC options to satisfy char by
1885 * char mode with no echo. All IAC options will be removed from
1886 * the buf and the do_telnetopt variable will be used to track the
1887 * state of the width of the IAC information.
1889 * IAC commands come in sets of 3 bytes with the exception of the
1890 * "IAC BREAK" command and the double IAC.
1896 for (i = 0; i < *size; i++) {
1897 if (s->do_telnetopt > 1) {
1898 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1899 /* Double IAC means send an IAC */
1903 s->do_telnetopt = 1;
1905 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1906 /* Handle IAC break commands by sending a serial break */
1907 qemu_chr_event(chr, CHR_EVENT_BREAK);
1912 if (s->do_telnetopt >= 4) {
1913 s->do_telnetopt = 1;
1916 if ((unsigned char)buf[i] == IAC) {
1917 s->do_telnetopt = 2;
1928 static int tcp_get_msgfd(CharDriverState *chr)
1930 TCPCharDriver *s = chr->opaque;
1936 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1938 TCPCharDriver *s = chr->opaque;
1939 struct cmsghdr *cmsg;
1941 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1944 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1945 cmsg->cmsg_level != SOL_SOCKET ||
1946 cmsg->cmsg_type != SCM_RIGHTS)
1949 fd = *((int *)CMSG_DATA(cmsg));
1959 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1961 TCPCharDriver *s = chr->opaque;
1962 struct msghdr msg = { NULL, };
1963 struct iovec iov[1];
1965 struct cmsghdr cmsg;
1966 char control[CMSG_SPACE(sizeof(int))];
1970 iov[0].iov_base = buf;
1971 iov[0].iov_len = len;
1975 msg.msg_control = &msg_control;
1976 msg.msg_controllen = sizeof(msg_control);
1978 ret = recvmsg(s->fd, &msg, 0);
1979 if (ret > 0 && s->is_unix)
1980 unix_process_msgfd(chr, &msg);
1985 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1987 TCPCharDriver *s = chr->opaque;
1988 return recv(s->fd, buf, len, 0);
1992 static void tcp_chr_read(void *opaque)
1994 CharDriverState *chr = opaque;
1995 TCPCharDriver *s = chr->opaque;
1999 if (!s->connected || s->max_size <= 0)
2002 if (len > s->max_size)
2004 size = tcp_chr_recv(chr, (void *)buf, len);
2006 /* connection closed */
2008 if (s->listen_fd >= 0) {
2009 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2011 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2014 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2015 } else if (size > 0) {
2016 if (s->do_telnetopt)
2017 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2019 qemu_chr_read(chr, buf, size);
2020 if (s->msgfd != -1) {
2027 static void tcp_chr_connect(void *opaque)
2029 CharDriverState *chr = opaque;
2030 TCPCharDriver *s = chr->opaque;
2033 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2034 tcp_chr_read, NULL, chr);
2035 qemu_chr_reset(chr);
2038 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2039 static void tcp_chr_telnet_init(int fd)
2042 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2043 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2044 send(fd, (char *)buf, 3, 0);
2045 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2046 send(fd, (char *)buf, 3, 0);
2047 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2048 send(fd, (char *)buf, 3, 0);
2049 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2050 send(fd, (char *)buf, 3, 0);
2053 static void socket_set_nodelay(int fd)
2056 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2059 static void tcp_chr_accept(void *opaque)
2061 CharDriverState *chr = opaque;
2062 TCPCharDriver *s = chr->opaque;
2063 struct sockaddr_in saddr;
2065 struct sockaddr_un uaddr;
2067 struct sockaddr *addr;
2074 len = sizeof(uaddr);
2075 addr = (struct sockaddr *)&uaddr;
2079 len = sizeof(saddr);
2080 addr = (struct sockaddr *)&saddr;
2082 fd = accept(s->listen_fd, addr, &len);
2083 if (fd < 0 && errno != EINTR) {
2085 } else if (fd >= 0) {
2086 if (s->do_telnetopt)
2087 tcp_chr_telnet_init(fd);
2091 socket_set_nonblock(fd);
2093 socket_set_nodelay(fd);
2095 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2096 tcp_chr_connect(chr);
2099 static void tcp_chr_close(CharDriverState *chr)
2101 TCPCharDriver *s = chr->opaque;
2103 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2106 if (s->listen_fd >= 0) {
2107 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2108 closesocket(s->listen_fd);
2111 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2114 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2116 CharDriverState *chr = NULL;
2117 TCPCharDriver *s = NULL;
2125 is_listen = qemu_opt_get_bool(opts, "server", 0);
2126 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2127 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2128 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2129 is_unix = qemu_opt_get(opts, "path") != NULL;
2133 chr = qemu_mallocz(sizeof(CharDriverState));
2134 s = qemu_mallocz(sizeof(TCPCharDriver));
2138 fd = unix_listen_opts(opts);
2140 fd = unix_connect_opts(opts);
2144 fd = inet_listen_opts(opts, 0);
2146 fd = inet_connect_opts(opts);
2152 if (!is_waitconnect)
2153 socket_set_nonblock(fd);
2159 s->is_unix = is_unix;
2160 s->do_nodelay = do_nodelay && !is_unix;
2163 chr->chr_write = tcp_chr_write;
2164 chr->chr_close = tcp_chr_close;
2165 chr->get_msgfd = tcp_get_msgfd;
2169 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2171 s->do_telnetopt = 1;
2176 socket_set_nodelay(fd);
2177 tcp_chr_connect(chr);
2180 /* for "info chardev" monitor command */
2181 chr->filename = qemu_malloc(256);
2183 snprintf(chr->filename, 256, "unix:%s%s",
2184 qemu_opt_get(opts, "path"),
2185 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2186 } else if (is_telnet) {
2187 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2188 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2189 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2191 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2192 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2193 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2196 if (is_listen && is_waitconnect) {
2197 printf("QEMU waiting for connection on: %s\n",
2199 tcp_chr_accept(chr);
2200 socket_set_nonblock(s->listen_fd);
2212 static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2214 char host[65], port[33], width[8], height[8];
2219 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2223 if (strstart(filename, "mon:", &p)) {
2225 qemu_opt_set(opts, "mux", "on");
2228 if (strcmp(filename, "null") == 0 ||
2229 strcmp(filename, "pty") == 0 ||
2230 strcmp(filename, "msmouse") == 0 ||
2231 strcmp(filename, "braille") == 0 ||
2232 strcmp(filename, "stdio") == 0) {
2233 qemu_opt_set(opts, "backend", filename);
2236 if (strstart(filename, "vc", &p)) {
2237 qemu_opt_set(opts, "backend", "vc");
2239 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2241 qemu_opt_set(opts, "width", width);
2242 qemu_opt_set(opts, "height", height);
2243 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2245 qemu_opt_set(opts, "cols", width);
2246 qemu_opt_set(opts, "rows", height);
2253 if (strcmp(filename, "con:") == 0) {
2254 qemu_opt_set(opts, "backend", "console");
2257 if (strstart(filename, "COM", NULL)) {
2258 qemu_opt_set(opts, "backend", "serial");
2259 qemu_opt_set(opts, "path", filename);
2262 if (strstart(filename, "file:", &p)) {
2263 qemu_opt_set(opts, "backend", "file");
2264 qemu_opt_set(opts, "path", p);
2267 if (strstart(filename, "pipe:", &p)) {
2268 qemu_opt_set(opts, "backend", "pipe");
2269 qemu_opt_set(opts, "path", p);
2272 if (strstart(filename, "tcp:", &p) ||
2273 strstart(filename, "telnet:", &p)) {
2274 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2276 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2279 qemu_opt_set(opts, "backend", "socket");
2280 qemu_opt_set(opts, "host", host);
2281 qemu_opt_set(opts, "port", port);
2282 if (p[pos] == ',') {
2283 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2286 if (strstart(filename, "telnet:", &p))
2287 qemu_opt_set(opts, "telnet", "on");
2290 if (strstart(filename, "udp:", &p)) {
2291 qemu_opt_set(opts, "backend", "udp");
2292 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2294 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2295 fprintf(stderr, "udp #1\n");
2299 qemu_opt_set(opts, "host", host);
2300 qemu_opt_set(opts, "port", port);
2301 if (p[pos] == '@') {
2303 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2305 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2306 fprintf(stderr, "udp #2\n");
2310 qemu_opt_set(opts, "localaddr", host);
2311 qemu_opt_set(opts, "localport", port);
2315 if (strstart(filename, "unix:", &p)) {
2316 qemu_opt_set(opts, "backend", "socket");
2317 if (qemu_opts_do_parse(opts, p, "path") != 0)
2321 if (strstart(filename, "/dev/parport", NULL) ||
2322 strstart(filename, "/dev/ppi", NULL)) {
2323 qemu_opt_set(opts, "backend", "parport");
2324 qemu_opt_set(opts, "path", filename);
2327 if (strstart(filename, "/dev/", NULL)) {
2328 qemu_opt_set(opts, "backend", "tty");
2329 qemu_opt_set(opts, "path", filename);
2334 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
2335 qemu_opts_del(opts);
2339 static const struct {
2341 CharDriverState *(*open)(QemuOpts *opts);
2342 } backend_table[] = {
2343 { .name = "null", .open = qemu_chr_open_null },
2344 { .name = "socket", .open = qemu_chr_open_socket },
2345 { .name = "udp", .open = qemu_chr_open_udp },
2346 { .name = "msmouse", .open = qemu_chr_open_msmouse },
2347 { .name = "vc", .open = text_console_init },
2349 { .name = "file", .open = qemu_chr_open_win_file_out },
2350 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2351 { .name = "console", .open = qemu_chr_open_win_con },
2352 { .name = "serial", .open = qemu_chr_open_win },
2354 { .name = "file", .open = qemu_chr_open_file_out },
2355 { .name = "pipe", .open = qemu_chr_open_pipe },
2356 { .name = "pty", .open = qemu_chr_open_pty },
2357 { .name = "stdio", .open = qemu_chr_open_stdio },
2359 #ifdef CONFIG_BRLAPI
2360 { .name = "braille", .open = chr_baum_init },
2362 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2363 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2364 { .name = "tty", .open = qemu_chr_open_tty },
2366 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2367 { .name = "parport", .open = qemu_chr_open_pp },
2371 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2372 void (*init)(struct CharDriverState *s))
2374 CharDriverState *chr;
2377 if (qemu_opts_id(opts) == NULL) {
2378 fprintf(stderr, "chardev: no id specified\n");
2382 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2383 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2386 if (i == ARRAY_SIZE(backend_table)) {
2387 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2388 qemu_opt_get(opts, "backend"));
2392 chr = backend_table[i].open(opts);
2394 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2395 qemu_opt_get(opts, "backend"));
2400 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2402 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2404 if (qemu_opt_get_bool(opts, "mux", 0)) {
2405 CharDriverState *base = chr;
2406 int len = strlen(qemu_opts_id(opts)) + 6;
2407 base->label = qemu_malloc(len);
2408 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2409 chr = qemu_chr_open_mux(base);
2410 chr->filename = base->filename;
2411 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2413 chr->label = qemu_strdup(qemu_opts_id(opts));
2417 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2420 CharDriverState *chr;
2423 if (strstart(filename, "chardev:", &p)) {
2424 return qemu_chr_find(p);
2427 opts = qemu_chr_parse_compat(label, filename);
2431 chr = qemu_chr_open_opts(opts, init);
2432 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2433 monitor_init(chr, MONITOR_USE_READLINE);
2438 void qemu_chr_close(CharDriverState *chr)
2440 TAILQ_REMOVE(&chardevs, chr, next);
2442 chr->chr_close(chr);
2443 qemu_free(chr->filename);
2444 qemu_free(chr->label);
2448 void qemu_chr_info(Monitor *mon)
2450 CharDriverState *chr;
2452 TAILQ_FOREACH(chr, &chardevs, next) {
2453 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
2457 CharDriverState *qemu_chr_find(const char *name)
2459 CharDriverState *chr;
2461 TAILQ_FOREACH(chr, &chardevs, next) {
2462 if (strcmp(chr->label, name) != 0)