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"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
33 #include "hw/msmouse.h"
44 #include <sys/times.h>
48 #include <sys/ioctl.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
54 #include <net/if_tap.h>
57 #include <linux/if_tun.h>
59 #include <arpa/inet.h>
62 #include <sys/select.h>
67 #include <dev/ppbus/ppi.h>
68 #include <dev/ppbus/ppbconf.h>
72 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
73 #include <freebsd/stdlib.h>
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
99 #include "qemu_socket.h"
101 /***********************************************************/
102 /* character device */
104 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
105 TAILQ_HEAD_INITIALIZER(chardevs);
106 static int initial_reset_issued;
108 static void qemu_chr_event(CharDriverState *s, int event)
112 s->chr_event(s->handler_opaque, event);
115 static void qemu_chr_reset_bh(void *opaque)
117 CharDriverState *s = opaque;
118 qemu_chr_event(s, CHR_EVENT_RESET);
119 qemu_bh_delete(s->bh);
123 void qemu_chr_reset(CharDriverState *s)
125 if (s->bh == NULL && initial_reset_issued) {
126 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
127 qemu_bh_schedule(s->bh);
131 void qemu_chr_initial_reset(void)
133 CharDriverState *chr;
135 initial_reset_issued = 1;
137 TAILQ_FOREACH(chr, &chardevs, next) {
142 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
144 return s->chr_write(s, buf, len);
147 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
151 return s->chr_ioctl(s, cmd, arg);
154 int qemu_chr_can_read(CharDriverState *s)
156 if (!s->chr_can_read)
158 return s->chr_can_read(s->handler_opaque);
161 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
163 s->chr_read(s->handler_opaque, buf, len);
166 void qemu_chr_accept_input(CharDriverState *s)
168 if (s->chr_accept_input)
169 s->chr_accept_input(s);
172 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
177 vsnprintf(buf, sizeof(buf), fmt, ap);
178 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
182 void qemu_chr_send_event(CharDriverState *s, int event)
184 if (s->chr_send_event)
185 s->chr_send_event(s, event);
188 void qemu_chr_add_handlers(CharDriverState *s,
189 IOCanRWHandler *fd_can_read,
190 IOReadHandler *fd_read,
191 IOEventHandler *fd_event,
194 s->chr_can_read = fd_can_read;
195 s->chr_read = fd_read;
196 s->chr_event = fd_event;
197 s->handler_opaque = opaque;
198 if (s->chr_update_read_handler)
199 s->chr_update_read_handler(s);
202 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
207 static CharDriverState *qemu_chr_open_null(void)
209 CharDriverState *chr;
211 chr = qemu_mallocz(sizeof(CharDriverState));
212 chr->chr_write = null_chr_write;
216 /* MUX driver for serial I/O splitting */
217 static int term_timestamps;
218 static int64_t term_timestamps_start;
220 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
221 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
223 IOCanRWHandler *chr_can_read[MAX_MUX];
224 IOReadHandler *chr_read[MAX_MUX];
225 IOEventHandler *chr_event[MAX_MUX];
226 void *ext_opaque[MAX_MUX];
227 CharDriverState *drv;
228 unsigned char buffer[MUX_BUFFER_SIZE];
237 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
239 MuxDriver *d = chr->opaque;
241 if (!term_timestamps) {
242 ret = d->drv->chr_write(d->drv, buf, len);
247 for(i = 0; i < len; i++) {
248 ret += d->drv->chr_write(d->drv, buf+i, 1);
249 if (buf[i] == '\n') {
254 ti = qemu_get_clock(rt_clock);
255 if (term_timestamps_start == -1)
256 term_timestamps_start = ti;
257 ti -= term_timestamps_start;
259 snprintf(buf1, sizeof(buf1),
260 "[%02d:%02d:%02d.%03d] ",
265 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
272 static const char * const mux_help[] = {
273 "% h print this help\n\r",
274 "% x exit emulator\n\r",
275 "% s save disk data back to file (if -snapshot)\n\r",
276 "% t toggle console timestamps\n\r"
277 "% b send break (magic sysrq)\n\r",
278 "% c switch between console and monitor\n\r",
283 int term_escape_char = 0x01; /* ctrl-a is used for escape */
284 static void mux_print_help(CharDriverState *chr)
287 char ebuf[15] = "Escape-Char";
288 char cbuf[50] = "\n\r";
290 if (term_escape_char > 0 && term_escape_char < 26) {
291 snprintf(cbuf, sizeof(cbuf), "\n\r");
292 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
294 snprintf(cbuf, sizeof(cbuf),
295 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
298 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
299 for (i = 0; mux_help[i] != NULL; i++) {
300 for (j=0; mux_help[i][j] != '\0'; j++) {
301 if (mux_help[i][j] == '%')
302 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
304 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
309 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
311 if (d->term_got_escape) {
312 d->term_got_escape = 0;
313 if (ch == term_escape_char)
322 const char *term = "QEMU: Terminated\n\r";
323 chr->chr_write(chr,(uint8_t *)term,strlen(term));
330 for (i = 0; i < nb_drives; i++) {
331 bdrv_commit(drives_table[i].bdrv);
336 qemu_chr_event(chr, CHR_EVENT_BREAK);
339 /* Switch to the next registered device */
341 if (chr->focus >= d->mux_cnt)
345 term_timestamps = !term_timestamps;
346 term_timestamps_start = -1;
349 } else if (ch == term_escape_char) {
350 d->term_got_escape = 1;
358 static void mux_chr_accept_input(CharDriverState *chr)
361 MuxDriver *d = chr->opaque;
363 while (d->prod != d->cons &&
364 d->chr_can_read[m] &&
365 d->chr_can_read[m](d->ext_opaque[m])) {
366 d->chr_read[m](d->ext_opaque[m],
367 &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
371 static int mux_chr_can_read(void *opaque)
373 CharDriverState *chr = opaque;
374 MuxDriver *d = chr->opaque;
376 if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
378 if (d->chr_can_read[chr->focus])
379 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
383 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
385 CharDriverState *chr = opaque;
386 MuxDriver *d = chr->opaque;
390 mux_chr_accept_input (opaque);
392 for(i = 0; i < size; i++)
393 if (mux_proc_byte(chr, d, buf[i])) {
394 if (d->prod == d->cons &&
395 d->chr_can_read[m] &&
396 d->chr_can_read[m](d->ext_opaque[m]))
397 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
399 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
403 static void mux_chr_event(void *opaque, int event)
405 CharDriverState *chr = opaque;
406 MuxDriver *d = chr->opaque;
409 /* Send the event to all registered listeners */
410 for (i = 0; i < d->mux_cnt; i++)
412 d->chr_event[i](d->ext_opaque[i], event);
415 static void mux_chr_update_read_handler(CharDriverState *chr)
417 MuxDriver *d = chr->opaque;
419 if (d->mux_cnt >= MAX_MUX) {
420 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
423 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
424 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
425 d->chr_read[d->mux_cnt] = chr->chr_read;
426 d->chr_event[d->mux_cnt] = chr->chr_event;
427 /* Fix up the real driver with mux routines */
428 if (d->mux_cnt == 0) {
429 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
432 chr->focus = d->mux_cnt;
436 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
438 CharDriverState *chr;
441 chr = qemu_mallocz(sizeof(CharDriverState));
442 d = qemu_mallocz(sizeof(MuxDriver));
447 chr->chr_write = mux_chr_write;
448 chr->chr_update_read_handler = mux_chr_update_read_handler;
449 chr->chr_accept_input = mux_chr_accept_input;
455 int send_all(int fd, const void *buf, int len1)
461 ret = send(fd, buf, len, 0);
463 errno = WSAGetLastError();
464 if (errno != WSAEWOULDBLOCK) {
467 } else if (ret == 0) {
479 static int unix_write(int fd, const uint8_t *buf, int len1)
485 ret = write(fd, buf, len);
487 if (errno != EINTR && errno != EAGAIN)
489 } else if (ret == 0) {
499 int send_all(int fd, const void *buf, int len1)
501 return unix_write(fd, buf, len1);
512 #define STDIO_MAX_CLIENTS 1
513 static int stdio_nb_clients = 0;
515 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
517 FDCharDriver *s = chr->opaque;
518 return send_all(s->fd_out, buf, len);
521 static int fd_chr_read_poll(void *opaque)
523 CharDriverState *chr = opaque;
524 FDCharDriver *s = chr->opaque;
526 s->max_size = qemu_chr_can_read(chr);
530 static void fd_chr_read(void *opaque)
532 CharDriverState *chr = opaque;
533 FDCharDriver *s = chr->opaque;
538 if (len > s->max_size)
542 size = read(s->fd_in, buf, len);
544 /* FD has been closed. Remove it from the active list. */
545 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
549 qemu_chr_read(chr, buf, size);
553 static void fd_chr_update_read_handler(CharDriverState *chr)
555 FDCharDriver *s = chr->opaque;
558 if (nographic && s->fd_in == 0) {
560 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
561 fd_chr_read, NULL, chr);
566 static void fd_chr_close(struct CharDriverState *chr)
568 FDCharDriver *s = chr->opaque;
571 if (nographic && s->fd_in == 0) {
573 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
580 /* open a character device to a unix fd */
581 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
583 CharDriverState *chr;
586 chr = qemu_mallocz(sizeof(CharDriverState));
587 s = qemu_mallocz(sizeof(FDCharDriver));
591 chr->chr_write = fd_chr_write;
592 chr->chr_update_read_handler = fd_chr_update_read_handler;
593 chr->chr_close = fd_chr_close;
600 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
604 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
607 return qemu_chr_open_fd(-1, fd_out);
610 static CharDriverState *qemu_chr_open_pipe(const char *filename)
613 char filename_in[256], filename_out[256];
615 snprintf(filename_in, 256, "%s.in", filename);
616 snprintf(filename_out, 256, "%s.out", filename);
617 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
618 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
619 if (fd_in < 0 || fd_out < 0) {
624 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
628 return qemu_chr_open_fd(fd_in, fd_out);
632 /* for STDIO, we handle the case where several clients use it
635 #define TERM_FIFO_MAX_SIZE 1
637 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
638 static int term_fifo_size;
640 static int stdio_read_poll(void *opaque)
642 CharDriverState *chr = opaque;
644 /* try to flush the queue if needed */
645 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
646 qemu_chr_read(chr, term_fifo, 1);
649 /* see if we can absorb more chars */
650 if (term_fifo_size == 0)
656 static void stdio_read(void *opaque)
660 CharDriverState *chr = opaque;
662 size = read(0, buf, 1);
664 /* stdin has been closed. Remove it from the active list. */
665 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
669 if (qemu_chr_can_read(chr) > 0) {
670 qemu_chr_read(chr, buf, 1);
671 } else if (term_fifo_size == 0) {
672 term_fifo[term_fifo_size++] = buf[0];
677 /* init terminal so that we can grab keys */
678 static struct termios oldtty;
679 static int old_fd0_flags;
680 static int term_atexit_done;
682 static void term_exit(void)
684 tcsetattr (0, TCSANOW, &oldtty);
685 fcntl(0, F_SETFL, old_fd0_flags);
688 static void term_init(void)
694 old_fd0_flags = fcntl(0, F_GETFL);
696 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
697 |INLCR|IGNCR|ICRNL|IXON);
698 tty.c_oflag |= OPOST;
699 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
700 /* if graphical mode, we allow Ctrl-C handling */
702 tty.c_lflag &= ~ISIG;
703 tty.c_cflag &= ~(CSIZE|PARENB);
708 tcsetattr (0, TCSANOW, &tty);
710 if (!term_atexit_done++)
713 fcntl(0, F_SETFL, O_NONBLOCK);
716 static void qemu_chr_close_stdio(struct CharDriverState *chr)
720 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
724 static CharDriverState *qemu_chr_open_stdio(void)
726 CharDriverState *chr;
728 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
730 chr = qemu_chr_open_fd(0, 1);
731 chr->chr_close = qemu_chr_close_stdio;
732 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
740 /* Once Solaris has openpty(), this is going to be removed. */
741 int openpty(int *amaster, int *aslave, char *name,
742 struct termios *termp, struct winsize *winp)
745 int mfd = -1, sfd = -1;
747 *amaster = *aslave = -1;
749 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
753 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
756 if ((slave = ptsname(mfd)) == NULL)
759 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
762 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
763 (termp != NULL && tcgetattr(sfd, termp) < 0))
771 ioctl(sfd, TIOCSWINSZ, winp);
782 void cfmakeraw (struct termios *termios_p)
784 termios_p->c_iflag &=
785 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
786 termios_p->c_oflag &= ~OPOST;
787 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
788 termios_p->c_cflag &= ~(CSIZE|PARENB);
789 termios_p->c_cflag |= CS8;
791 termios_p->c_cc[VMIN] = 0;
792 termios_p->c_cc[VTIME] = 0;
796 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
797 || defined(__NetBSD__) || defined(__OpenBSD__)
807 static void pty_chr_update_read_handler(CharDriverState *chr);
808 static void pty_chr_state(CharDriverState *chr, int connected);
810 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
812 PtyCharDriver *s = chr->opaque;
815 /* guest sends data, check for (re-)connect */
816 pty_chr_update_read_handler(chr);
819 return send_all(s->fd, buf, len);
822 static int pty_chr_read_poll(void *opaque)
824 CharDriverState *chr = opaque;
825 PtyCharDriver *s = chr->opaque;
827 s->read_bytes = qemu_chr_can_read(chr);
828 return s->read_bytes;
831 static void pty_chr_read(void *opaque)
833 CharDriverState *chr = opaque;
834 PtyCharDriver *s = chr->opaque;
839 if (len > s->read_bytes)
843 size = read(s->fd, buf, len);
844 if ((size == -1 && errno == EIO) ||
846 pty_chr_state(chr, 0);
850 pty_chr_state(chr, 1);
851 qemu_chr_read(chr, buf, size);
855 static void pty_chr_update_read_handler(CharDriverState *chr)
857 PtyCharDriver *s = chr->opaque;
859 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
860 pty_chr_read, NULL, chr);
863 * Short timeout here: just need wait long enougth that qemu makes
864 * it through the poll loop once. When reconnected we want a
865 * short timeout so we notice it almost instantly. Otherwise
866 * read() gives us -EIO instantly, making pty_chr_state() reset the
867 * timeout to the normal (much longer) poll interval before the
870 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
873 static void pty_chr_state(CharDriverState *chr, int connected)
875 PtyCharDriver *s = chr->opaque;
878 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
881 /* (re-)connect poll interval for idle guests: once per second.
882 * We check more frequently in case the guests sends data to
883 * the virtual device linked to our pty. */
884 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
892 static void pty_chr_timer(void *opaque)
894 struct CharDriverState *chr = opaque;
895 PtyCharDriver *s = chr->opaque;
900 /* If we arrive here without polling being cleared due
901 * read returning -EIO, then we are (re-)connected */
902 pty_chr_state(chr, 1);
907 pty_chr_update_read_handler(chr);
910 static void pty_chr_close(struct CharDriverState *chr)
912 PtyCharDriver *s = chr->opaque;
914 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
919 static CharDriverState *qemu_chr_open_pty(void)
921 CharDriverState *chr;
925 #if defined(__OpenBSD__)
926 char pty_name[PATH_MAX];
927 #define q_ptsname(x) pty_name
929 char *pty_name = NULL;
930 #define q_ptsname(x) ptsname(x)
933 chr = qemu_mallocz(sizeof(CharDriverState));
934 s = qemu_mallocz(sizeof(PtyCharDriver));
936 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
940 /* Set raw attributes on the pty. */
941 tcgetattr(slave_fd, &tty);
943 tcsetattr(slave_fd, TCSAFLUSH, &tty);
946 len = strlen(q_ptsname(s->fd)) + 5;
947 chr->filename = qemu_malloc(len);
948 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
949 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
952 chr->chr_write = pty_chr_write;
953 chr->chr_update_read_handler = pty_chr_update_read_handler;
954 chr->chr_close = pty_chr_close;
956 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
961 static void tty_serial_init(int fd, int speed,
962 int parity, int data_bits, int stop_bits)
968 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
969 speed, parity, data_bits, stop_bits);
971 tcgetattr (fd, &tty);
974 if (speed <= 50 * MARGIN)
976 else if (speed <= 75 * MARGIN)
978 else if (speed <= 300 * MARGIN)
980 else if (speed <= 600 * MARGIN)
982 else if (speed <= 1200 * MARGIN)
984 else if (speed <= 2400 * MARGIN)
986 else if (speed <= 4800 * MARGIN)
988 else if (speed <= 9600 * MARGIN)
990 else if (speed <= 19200 * MARGIN)
992 else if (speed <= 38400 * MARGIN)
994 else if (speed <= 57600 * MARGIN)
996 else if (speed <= 115200 * MARGIN)
1001 cfsetispeed(&tty, spd);
1002 cfsetospeed(&tty, spd);
1004 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1005 |INLCR|IGNCR|ICRNL|IXON);
1006 tty.c_oflag |= OPOST;
1007 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1008 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1029 tty.c_cflag |= PARENB;
1032 tty.c_cflag |= PARENB | PARODD;
1036 tty.c_cflag |= CSTOPB;
1038 tcsetattr (fd, TCSANOW, &tty);
1041 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1043 FDCharDriver *s = chr->opaque;
1046 case CHR_IOCTL_SERIAL_SET_PARAMS:
1048 QEMUSerialSetParams *ssp = arg;
1049 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1050 ssp->data_bits, ssp->stop_bits);
1053 case CHR_IOCTL_SERIAL_SET_BREAK:
1055 int enable = *(int *)arg;
1057 tcsendbreak(s->fd_in, 1);
1060 case CHR_IOCTL_SERIAL_GET_TIOCM:
1063 int *targ = (int *)arg;
1064 ioctl(s->fd_in, TIOCMGET, &sarg);
1066 if (sarg & TIOCM_CTS)
1067 *targ |= CHR_TIOCM_CTS;
1068 if (sarg & TIOCM_CAR)
1069 *targ |= CHR_TIOCM_CAR;
1070 if (sarg & TIOCM_DSR)
1071 *targ |= CHR_TIOCM_DSR;
1072 if (sarg & TIOCM_RI)
1073 *targ |= CHR_TIOCM_RI;
1074 if (sarg & TIOCM_DTR)
1075 *targ |= CHR_TIOCM_DTR;
1076 if (sarg & TIOCM_RTS)
1077 *targ |= CHR_TIOCM_RTS;
1080 case CHR_IOCTL_SERIAL_SET_TIOCM:
1082 int sarg = *(int *)arg;
1084 ioctl(s->fd_in, TIOCMGET, &targ);
1085 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1086 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1087 if (sarg & CHR_TIOCM_CTS)
1089 if (sarg & CHR_TIOCM_CAR)
1091 if (sarg & CHR_TIOCM_DSR)
1093 if (sarg & CHR_TIOCM_RI)
1095 if (sarg & CHR_TIOCM_DTR)
1097 if (sarg & CHR_TIOCM_RTS)
1099 ioctl(s->fd_in, TIOCMSET, &targ);
1108 static CharDriverState *qemu_chr_open_tty(const char *filename)
1110 CharDriverState *chr;
1113 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1114 tty_serial_init(fd, 115200, 'N', 8, 1);
1115 chr = qemu_chr_open_fd(fd, fd);
1120 chr->chr_ioctl = tty_serial_ioctl;
1121 qemu_chr_reset(chr);
1124 #else /* ! __linux__ && ! __sun__ */
1125 static CharDriverState *qemu_chr_open_pty(void)
1129 #endif /* __linux__ || __sun__ */
1131 #if defined(__linux__)
1135 } ParallelCharDriver;
1137 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1139 if (s->mode != mode) {
1141 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1148 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1150 ParallelCharDriver *drv = chr->opaque;
1155 case CHR_IOCTL_PP_READ_DATA:
1156 if (ioctl(fd, PPRDATA, &b) < 0)
1158 *(uint8_t *)arg = b;
1160 case CHR_IOCTL_PP_WRITE_DATA:
1161 b = *(uint8_t *)arg;
1162 if (ioctl(fd, PPWDATA, &b) < 0)
1165 case CHR_IOCTL_PP_READ_CONTROL:
1166 if (ioctl(fd, PPRCONTROL, &b) < 0)
1168 /* Linux gives only the lowest bits, and no way to know data
1169 direction! For better compatibility set the fixed upper
1171 *(uint8_t *)arg = b | 0xc0;
1173 case CHR_IOCTL_PP_WRITE_CONTROL:
1174 b = *(uint8_t *)arg;
1175 if (ioctl(fd, PPWCONTROL, &b) < 0)
1178 case CHR_IOCTL_PP_READ_STATUS:
1179 if (ioctl(fd, PPRSTATUS, &b) < 0)
1181 *(uint8_t *)arg = b;
1183 case CHR_IOCTL_PP_DATA_DIR:
1184 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1187 case CHR_IOCTL_PP_EPP_READ_ADDR:
1188 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1189 struct ParallelIOArg *parg = arg;
1190 int n = read(fd, parg->buffer, parg->count);
1191 if (n != parg->count) {
1196 case CHR_IOCTL_PP_EPP_READ:
1197 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1198 struct ParallelIOArg *parg = arg;
1199 int n = read(fd, parg->buffer, parg->count);
1200 if (n != parg->count) {
1205 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1206 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1207 struct ParallelIOArg *parg = arg;
1208 int n = write(fd, parg->buffer, parg->count);
1209 if (n != parg->count) {
1214 case CHR_IOCTL_PP_EPP_WRITE:
1215 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1216 struct ParallelIOArg *parg = arg;
1217 int n = write(fd, parg->buffer, parg->count);
1218 if (n != parg->count) {
1229 static void pp_close(CharDriverState *chr)
1231 ParallelCharDriver *drv = chr->opaque;
1234 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1235 ioctl(fd, PPRELEASE);
1240 static CharDriverState *qemu_chr_open_pp(const char *filename)
1242 CharDriverState *chr;
1243 ParallelCharDriver *drv;
1246 TFR(fd = open(filename, O_RDWR));
1250 if (ioctl(fd, PPCLAIM) < 0) {
1255 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1257 drv->mode = IEEE1284_MODE_COMPAT;
1259 chr = qemu_mallocz(sizeof(CharDriverState));
1260 chr->chr_write = null_chr_write;
1261 chr->chr_ioctl = pp_ioctl;
1262 chr->chr_close = pp_close;
1265 qemu_chr_reset(chr);
1269 #endif /* __linux__ */
1271 #if defined(__FreeBSD__)
1272 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1274 int fd = (int)chr->opaque;
1278 case CHR_IOCTL_PP_READ_DATA:
1279 if (ioctl(fd, PPIGDATA, &b) < 0)
1281 *(uint8_t *)arg = b;
1283 case CHR_IOCTL_PP_WRITE_DATA:
1284 b = *(uint8_t *)arg;
1285 if (ioctl(fd, PPISDATA, &b) < 0)
1288 case CHR_IOCTL_PP_READ_CONTROL:
1289 if (ioctl(fd, PPIGCTRL, &b) < 0)
1291 *(uint8_t *)arg = b;
1293 case CHR_IOCTL_PP_WRITE_CONTROL:
1294 b = *(uint8_t *)arg;
1295 if (ioctl(fd, PPISCTRL, &b) < 0)
1298 case CHR_IOCTL_PP_READ_STATUS:
1299 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1301 *(uint8_t *)arg = b;
1309 static CharDriverState *qemu_chr_open_pp(const char *filename)
1311 CharDriverState *chr;
1314 fd = open(filename, O_RDWR);
1318 chr = qemu_mallocz(sizeof(CharDriverState));
1319 chr->opaque = (void *)fd;
1320 chr->chr_write = null_chr_write;
1321 chr->chr_ioctl = pp_ioctl;
1330 HANDLE hcom, hrecv, hsend;
1331 OVERLAPPED orecv, osend;
1336 #define NSENDBUF 2048
1337 #define NRECVBUF 2048
1338 #define MAXCONNECT 1
1339 #define NTIMEOUT 5000
1341 static int win_chr_poll(void *opaque);
1342 static int win_chr_pipe_poll(void *opaque);
1344 static void win_chr_close(CharDriverState *chr)
1346 WinCharState *s = chr->opaque;
1349 CloseHandle(s->hsend);
1353 CloseHandle(s->hrecv);
1357 CloseHandle(s->hcom);
1361 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1363 qemu_del_polling_cb(win_chr_poll, chr);
1366 static int win_chr_init(CharDriverState *chr, const char *filename)
1368 WinCharState *s = chr->opaque;
1370 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1375 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1377 fprintf(stderr, "Failed CreateEvent\n");
1380 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1382 fprintf(stderr, "Failed CreateEvent\n");
1386 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1387 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1388 if (s->hcom == INVALID_HANDLE_VALUE) {
1389 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1394 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1395 fprintf(stderr, "Failed SetupComm\n");
1399 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1400 size = sizeof(COMMCONFIG);
1401 GetDefaultCommConfig(filename, &comcfg, &size);
1402 comcfg.dcb.DCBlength = sizeof(DCB);
1403 CommConfigDialog(filename, NULL, &comcfg);
1405 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1406 fprintf(stderr, "Failed SetCommState\n");
1410 if (!SetCommMask(s->hcom, EV_ERR)) {
1411 fprintf(stderr, "Failed SetCommMask\n");
1415 cto.ReadIntervalTimeout = MAXDWORD;
1416 if (!SetCommTimeouts(s->hcom, &cto)) {
1417 fprintf(stderr, "Failed SetCommTimeouts\n");
1421 if (!ClearCommError(s->hcom, &err, &comstat)) {
1422 fprintf(stderr, "Failed ClearCommError\n");
1425 qemu_add_polling_cb(win_chr_poll, chr);
1433 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1435 WinCharState *s = chr->opaque;
1436 DWORD len, ret, size, err;
1439 ZeroMemory(&s->osend, sizeof(s->osend));
1440 s->osend.hEvent = s->hsend;
1443 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1445 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1447 err = GetLastError();
1448 if (err == ERROR_IO_PENDING) {
1449 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1467 static int win_chr_read_poll(CharDriverState *chr)
1469 WinCharState *s = chr->opaque;
1471 s->max_size = qemu_chr_can_read(chr);
1475 static void win_chr_readfile(CharDriverState *chr)
1477 WinCharState *s = chr->opaque;
1482 ZeroMemory(&s->orecv, sizeof(s->orecv));
1483 s->orecv.hEvent = s->hrecv;
1484 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1486 err = GetLastError();
1487 if (err == ERROR_IO_PENDING) {
1488 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1493 qemu_chr_read(chr, buf, size);
1497 static void win_chr_read(CharDriverState *chr)
1499 WinCharState *s = chr->opaque;
1501 if (s->len > s->max_size)
1502 s->len = s->max_size;
1506 win_chr_readfile(chr);
1509 static int win_chr_poll(void *opaque)
1511 CharDriverState *chr = opaque;
1512 WinCharState *s = chr->opaque;
1516 ClearCommError(s->hcom, &comerr, &status);
1517 if (status.cbInQue > 0) {
1518 s->len = status.cbInQue;
1519 win_chr_read_poll(chr);
1526 static CharDriverState *qemu_chr_open_win(const char *filename)
1528 CharDriverState *chr;
1531 chr = qemu_mallocz(sizeof(CharDriverState));
1532 s = qemu_mallocz(sizeof(WinCharState));
1534 chr->chr_write = win_chr_write;
1535 chr->chr_close = win_chr_close;
1537 if (win_chr_init(chr, filename) < 0) {
1542 qemu_chr_reset(chr);
1546 static int win_chr_pipe_poll(void *opaque)
1548 CharDriverState *chr = opaque;
1549 WinCharState *s = chr->opaque;
1552 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1555 win_chr_read_poll(chr);
1562 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1564 WinCharState *s = chr->opaque;
1572 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1574 fprintf(stderr, "Failed CreateEvent\n");
1577 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1579 fprintf(stderr, "Failed CreateEvent\n");
1583 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1584 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1585 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1587 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1588 if (s->hcom == INVALID_HANDLE_VALUE) {
1589 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1594 ZeroMemory(&ov, sizeof(ov));
1595 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1596 ret = ConnectNamedPipe(s->hcom, &ov);
1598 fprintf(stderr, "Failed ConnectNamedPipe\n");
1602 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1604 fprintf(stderr, "Failed GetOverlappedResult\n");
1606 CloseHandle(ov.hEvent);
1613 CloseHandle(ov.hEvent);
1616 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1625 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1627 CharDriverState *chr;
1630 chr = qemu_mallocz(sizeof(CharDriverState));
1631 s = qemu_mallocz(sizeof(WinCharState));
1633 chr->chr_write = win_chr_write;
1634 chr->chr_close = win_chr_close;
1636 if (win_chr_pipe_init(chr, filename) < 0) {
1641 qemu_chr_reset(chr);
1645 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1647 CharDriverState *chr;
1650 chr = qemu_mallocz(sizeof(CharDriverState));
1651 s = qemu_mallocz(sizeof(WinCharState));
1654 chr->chr_write = win_chr_write;
1655 qemu_chr_reset(chr);
1659 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1661 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1664 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1668 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1669 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1670 if (fd_out == INVALID_HANDLE_VALUE)
1673 return qemu_chr_open_win_file(fd_out);
1675 #endif /* !_WIN32 */
1677 /***********************************************************/
1678 /* UDP Net console */
1682 struct sockaddr_in daddr;
1689 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1691 NetCharDriver *s = chr->opaque;
1693 return sendto(s->fd, buf, len, 0,
1694 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1697 static int udp_chr_read_poll(void *opaque)
1699 CharDriverState *chr = opaque;
1700 NetCharDriver *s = chr->opaque;
1702 s->max_size = qemu_chr_can_read(chr);
1704 /* If there were any stray characters in the queue process them
1707 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1708 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1710 s->max_size = qemu_chr_can_read(chr);
1715 static void udp_chr_read(void *opaque)
1717 CharDriverState *chr = opaque;
1718 NetCharDriver *s = chr->opaque;
1720 if (s->max_size == 0)
1722 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1723 s->bufptr = s->bufcnt;
1728 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1729 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1731 s->max_size = qemu_chr_can_read(chr);
1735 static void udp_chr_update_read_handler(CharDriverState *chr)
1737 NetCharDriver *s = chr->opaque;
1740 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1741 udp_chr_read, NULL, chr);
1745 static CharDriverState *qemu_chr_open_udp(const char *def)
1747 CharDriverState *chr = NULL;
1748 NetCharDriver *s = NULL;
1750 struct sockaddr_in saddr;
1752 chr = qemu_mallocz(sizeof(CharDriverState));
1753 s = qemu_mallocz(sizeof(NetCharDriver));
1755 fd = socket(PF_INET, SOCK_DGRAM, 0);
1757 perror("socket(PF_INET, SOCK_DGRAM)");
1761 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1762 printf("Could not parse: %s\n", def);
1766 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1776 chr->chr_write = udp_chr_write;
1777 chr->chr_update_read_handler = udp_chr_update_read_handler;
1790 /***********************************************************/
1791 /* TCP Net console */
1802 static void tcp_chr_accept(void *opaque);
1804 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1806 TCPCharDriver *s = chr->opaque;
1808 return send_all(s->fd, buf, len);
1810 /* XXX: indicate an error ? */
1815 static int tcp_chr_read_poll(void *opaque)
1817 CharDriverState *chr = opaque;
1818 TCPCharDriver *s = chr->opaque;
1821 s->max_size = qemu_chr_can_read(chr);
1826 #define IAC_BREAK 243
1827 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1829 uint8_t *buf, int *size)
1831 /* Handle any telnet client's basic IAC options to satisfy char by
1832 * char mode with no echo. All IAC options will be removed from
1833 * the buf and the do_telnetopt variable will be used to track the
1834 * state of the width of the IAC information.
1836 * IAC commands come in sets of 3 bytes with the exception of the
1837 * "IAC BREAK" command and the double IAC.
1843 for (i = 0; i < *size; i++) {
1844 if (s->do_telnetopt > 1) {
1845 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1846 /* Double IAC means send an IAC */
1850 s->do_telnetopt = 1;
1852 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1853 /* Handle IAC break commands by sending a serial break */
1854 qemu_chr_event(chr, CHR_EVENT_BREAK);
1859 if (s->do_telnetopt >= 4) {
1860 s->do_telnetopt = 1;
1863 if ((unsigned char)buf[i] == IAC) {
1864 s->do_telnetopt = 2;
1875 static void tcp_chr_read(void *opaque)
1877 CharDriverState *chr = opaque;
1878 TCPCharDriver *s = chr->opaque;
1882 if (!s->connected || s->max_size <= 0)
1885 if (len > s->max_size)
1887 size = recv(s->fd, buf, len, 0);
1889 /* connection closed */
1891 if (s->listen_fd >= 0) {
1892 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1894 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1897 } else if (size > 0) {
1898 if (s->do_telnetopt)
1899 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1901 qemu_chr_read(chr, buf, size);
1905 static void tcp_chr_connect(void *opaque)
1907 CharDriverState *chr = opaque;
1908 TCPCharDriver *s = chr->opaque;
1911 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1912 tcp_chr_read, NULL, chr);
1913 qemu_chr_reset(chr);
1916 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1917 static void tcp_chr_telnet_init(int fd)
1920 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1921 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1922 send(fd, (char *)buf, 3, 0);
1923 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1924 send(fd, (char *)buf, 3, 0);
1925 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1926 send(fd, (char *)buf, 3, 0);
1927 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1928 send(fd, (char *)buf, 3, 0);
1931 static void socket_set_nodelay(int fd)
1934 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1937 static void tcp_chr_accept(void *opaque)
1939 CharDriverState *chr = opaque;
1940 TCPCharDriver *s = chr->opaque;
1941 struct sockaddr_in saddr;
1943 struct sockaddr_un uaddr;
1945 struct sockaddr *addr;
1952 len = sizeof(uaddr);
1953 addr = (struct sockaddr *)&uaddr;
1957 len = sizeof(saddr);
1958 addr = (struct sockaddr *)&saddr;
1960 fd = accept(s->listen_fd, addr, &len);
1961 if (fd < 0 && errno != EINTR) {
1963 } else if (fd >= 0) {
1964 if (s->do_telnetopt)
1965 tcp_chr_telnet_init(fd);
1969 socket_set_nonblock(fd);
1971 socket_set_nodelay(fd);
1973 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
1974 tcp_chr_connect(chr);
1977 static void tcp_chr_close(CharDriverState *chr)
1979 TCPCharDriver *s = chr->opaque;
1982 if (s->listen_fd >= 0)
1983 closesocket(s->listen_fd);
1987 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
1991 CharDriverState *chr = NULL;
1992 TCPCharDriver *s = NULL;
1993 int fd = -1, offset = 0;
1995 int is_waitconnect = 1;
2000 while((ptr = strchr(ptr,','))) {
2002 if (!strncmp(ptr,"server",6)) {
2004 } else if (!strncmp(ptr,"nowait",6)) {
2006 } else if (!strncmp(ptr,"nodelay",6)) {
2008 } else if (!strncmp(ptr,"to=",3)) {
2009 /* nothing, inet_listen() parses this one */;
2010 } else if (!strncmp(ptr,"ipv4",4)) {
2011 /* nothing, inet_connect() and inet_listen() parse this one */;
2012 } else if (!strncmp(ptr,"ipv6",4)) {
2013 /* nothing, inet_connect() and inet_listen() parse this one */;
2015 printf("Unknown option: %s\n", ptr);
2022 chr = qemu_mallocz(sizeof(CharDriverState));
2023 s = qemu_mallocz(sizeof(TCPCharDriver));
2026 chr->filename = qemu_malloc(256);
2028 pstrcpy(chr->filename, 256, "unix:");
2029 } else if (is_telnet) {
2030 pstrcpy(chr->filename, 256, "telnet:");
2032 pstrcpy(chr->filename, 256, "tcp:");
2034 offset = strlen(chr->filename);
2038 fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
2040 fd = unix_connect(host_str);
2044 fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
2047 fd = inet_connect(host_str, SOCK_STREAM);
2053 if (!is_waitconnect)
2054 socket_set_nonblock(fd);
2059 s->is_unix = is_unix;
2060 s->do_nodelay = do_nodelay && !is_unix;
2063 chr->chr_write = tcp_chr_write;
2064 chr->chr_close = tcp_chr_close;
2068 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2070 s->do_telnetopt = 1;
2074 socket_set_nodelay(fd);
2075 tcp_chr_connect(chr);
2078 if (is_listen && is_waitconnect) {
2079 printf("QEMU waiting for connection on: %s\n",
2080 chr->filename ? chr->filename : host_str);
2081 tcp_chr_accept(chr);
2082 socket_set_nonblock(s->listen_fd);
2094 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2097 CharDriverState *chr;
2099 if (!strcmp(filename, "vc")) {
2100 chr = text_console_init(0);
2102 if (strstart(filename, "vc:", &p)) {
2103 chr = text_console_init(p);
2105 if (!strcmp(filename, "null")) {
2106 chr = qemu_chr_open_null();
2108 if (strstart(filename, "tcp:", &p)) {
2109 chr = qemu_chr_open_tcp(p, 0, 0);
2111 if (strstart(filename, "telnet:", &p)) {
2112 chr = qemu_chr_open_tcp(p, 1, 0);
2114 if (strstart(filename, "udp:", &p)) {
2115 chr = qemu_chr_open_udp(p);
2117 if (strstart(filename, "mon:", &p)) {
2118 chr = qemu_chr_open(label, p, NULL);
2120 chr = qemu_chr_open_mux(chr);
2121 monitor_init(chr, !nographic);
2123 printf("Unable to open driver: %s\n", p);
2125 } else if (!strcmp(filename, "msmouse")) {
2126 chr = qemu_chr_open_msmouse();
2129 if (strstart(filename, "unix:", &p)) {
2130 chr = qemu_chr_open_tcp(p, 0, 1);
2131 } else if (strstart(filename, "file:", &p)) {
2132 chr = qemu_chr_open_file_out(p);
2133 } else if (strstart(filename, "pipe:", &p)) {
2134 chr = qemu_chr_open_pipe(p);
2135 } else if (!strcmp(filename, "pty")) {
2136 chr = qemu_chr_open_pty();
2137 } else if (!strcmp(filename, "stdio")) {
2138 chr = qemu_chr_open_stdio();
2140 #if defined(__linux__)
2141 if (strstart(filename, "/dev/parport", NULL)) {
2142 chr = qemu_chr_open_pp(filename);
2144 #elif defined(__FreeBSD__)
2145 if (strstart(filename, "/dev/ppi", NULL)) {
2146 chr = qemu_chr_open_pp(filename);
2149 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2150 || defined(__NetBSD__) || defined(__OpenBSD__)
2151 if (strstart(filename, "/dev/", NULL)) {
2152 chr = qemu_chr_open_tty(filename);
2156 if (strstart(filename, "COM", NULL)) {
2157 chr = qemu_chr_open_win(filename);
2159 if (strstart(filename, "pipe:", &p)) {
2160 chr = qemu_chr_open_win_pipe(p);
2162 if (strstart(filename, "con:", NULL)) {
2163 chr = qemu_chr_open_win_con(filename);
2165 if (strstart(filename, "file:", &p)) {
2166 chr = qemu_chr_open_win_file_out(p);
2169 #ifdef CONFIG_BRLAPI
2170 if (!strcmp(filename, "braille")) {
2171 chr = chr_baum_init();
2180 chr->filename = qemu_strdup(filename);
2182 chr->label = qemu_strdup(label);
2183 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2188 void qemu_chr_close(CharDriverState *chr)
2190 TAILQ_REMOVE(&chardevs, chr, next);
2192 chr->chr_close(chr);
2193 qemu_free(chr->filename);
2194 qemu_free(chr->label);
2198 void qemu_chr_info(void)
2200 CharDriverState *chr;
2202 TAILQ_FOREACH(chr, &chardevs, next) {
2203 term_printf("%s: filename=%s\n", chr->label, chr->filename);