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>
73 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
74 #include <freebsd/stdlib.h>
79 #include <linux/ppdev.h>
80 #include <linux/parport.h>
84 #include <sys/ethernet.h>
85 #include <sys/sockio.h>
86 #include <netinet/arp.h>
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_icmp.h> // must come after ip.h
91 #include <netinet/udp.h>
92 #include <netinet/tcp.h>
100 #include "qemu_socket.h"
102 /***********************************************************/
103 /* character device */
105 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
106 TAILQ_HEAD_INITIALIZER(chardevs);
107 static int initial_reset_issued;
109 static void qemu_chr_event(CharDriverState *s, int event)
113 s->chr_event(s->handler_opaque, event);
116 static void qemu_chr_reset_bh(void *opaque)
118 CharDriverState *s = opaque;
119 qemu_chr_event(s, CHR_EVENT_RESET);
120 qemu_bh_delete(s->bh);
124 void qemu_chr_reset(CharDriverState *s)
126 if (s->bh == NULL && initial_reset_issued) {
127 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
128 qemu_bh_schedule(s->bh);
132 void qemu_chr_initial_reset(void)
134 CharDriverState *chr;
136 initial_reset_issued = 1;
138 TAILQ_FOREACH(chr, &chardevs, next) {
143 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
145 return s->chr_write(s, buf, len);
148 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
152 return s->chr_ioctl(s, cmd, arg);
155 int qemu_chr_can_read(CharDriverState *s)
157 if (!s->chr_can_read)
159 return s->chr_can_read(s->handler_opaque);
162 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
164 s->chr_read(s->handler_opaque, buf, len);
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(void)
210 CharDriverState *chr;
212 chr = qemu_mallocz(sizeof(CharDriverState));
213 chr->chr_write = null_chr_write;
217 /* MUX driver for serial I/O splitting */
218 static int term_timestamps;
219 static int64_t term_timestamps_start;
221 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
222 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
224 IOCanRWHandler *chr_can_read[MAX_MUX];
225 IOReadHandler *chr_read[MAX_MUX];
226 IOEventHandler *chr_event[MAX_MUX];
227 void *ext_opaque[MAX_MUX];
228 CharDriverState *drv;
232 /* Intermediate input buffer allows to catch escape sequences even if the
233 currently active device is not accepting any input - but only until it
235 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
241 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
243 MuxDriver *d = chr->opaque;
245 if (!term_timestamps) {
246 ret = d->drv->chr_write(d->drv, buf, len);
251 for(i = 0; i < len; i++) {
252 ret += d->drv->chr_write(d->drv, buf+i, 1);
253 if (buf[i] == '\n') {
258 ti = qemu_get_clock(rt_clock);
259 if (term_timestamps_start == -1)
260 term_timestamps_start = ti;
261 ti -= term_timestamps_start;
263 snprintf(buf1, sizeof(buf1),
264 "[%02d:%02d:%02d.%03d] ",
269 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
276 static const char * const mux_help[] = {
277 "% h print this help\n\r",
278 "% x exit emulator\n\r",
279 "% s save disk data back to file (if -snapshot)\n\r",
280 "% t toggle console timestamps\n\r"
281 "% b send break (magic sysrq)\n\r",
282 "% c switch between console and monitor\n\r",
287 int term_escape_char = 0x01; /* ctrl-a is used for escape */
288 static void mux_print_help(CharDriverState *chr)
291 char ebuf[15] = "Escape-Char";
292 char cbuf[50] = "\n\r";
294 if (term_escape_char > 0 && term_escape_char < 26) {
295 snprintf(cbuf, sizeof(cbuf), "\n\r");
296 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
298 snprintf(cbuf, sizeof(cbuf),
299 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
302 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
303 for (i = 0; mux_help[i] != NULL; i++) {
304 for (j=0; mux_help[i][j] != '\0'; j++) {
305 if (mux_help[i][j] == '%')
306 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
308 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
313 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
315 if (d->chr_event[mux_nr])
316 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
319 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
321 if (d->term_got_escape) {
322 d->term_got_escape = 0;
323 if (ch == term_escape_char)
332 const char *term = "QEMU: Terminated\n\r";
333 chr->chr_write(chr,(uint8_t *)term,strlen(term));
340 for (i = 0; i < nb_drives; i++) {
341 bdrv_commit(drives_table[i].bdrv);
346 qemu_chr_event(chr, CHR_EVENT_BREAK);
349 /* Switch to the next registered device */
350 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_OUT);
352 if (chr->focus >= d->mux_cnt)
354 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_IN);
357 term_timestamps = !term_timestamps;
358 term_timestamps_start = -1;
361 } else if (ch == term_escape_char) {
362 d->term_got_escape = 1;
370 static void mux_chr_accept_input(CharDriverState *chr)
373 MuxDriver *d = chr->opaque;
375 while (d->prod[m] != d->cons[m] &&
376 d->chr_can_read[m] &&
377 d->chr_can_read[m](d->ext_opaque[m])) {
378 d->chr_read[m](d->ext_opaque[m],
379 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
383 static int mux_chr_can_read(void *opaque)
385 CharDriverState *chr = opaque;
386 MuxDriver *d = chr->opaque;
389 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
391 if (d->chr_can_read[m])
392 return d->chr_can_read[m](d->ext_opaque[m]);
396 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
398 CharDriverState *chr = opaque;
399 MuxDriver *d = chr->opaque;
403 mux_chr_accept_input (opaque);
405 for(i = 0; i < size; i++)
406 if (mux_proc_byte(chr, d, buf[i])) {
407 if (d->prod[m] == d->cons[m] &&
408 d->chr_can_read[m] &&
409 d->chr_can_read[m](d->ext_opaque[m]))
410 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
412 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
416 static void mux_chr_event(void *opaque, int event)
418 CharDriverState *chr = opaque;
419 MuxDriver *d = chr->opaque;
422 /* Send the event to all registered listeners */
423 for (i = 0; i < d->mux_cnt; i++)
424 mux_chr_send_event(d, i, event);
427 static void mux_chr_update_read_handler(CharDriverState *chr)
429 MuxDriver *d = chr->opaque;
431 if (d->mux_cnt >= MAX_MUX) {
432 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
435 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
436 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
437 d->chr_read[d->mux_cnt] = chr->chr_read;
438 d->chr_event[d->mux_cnt] = chr->chr_event;
439 /* Fix up the real driver with mux routines */
440 if (d->mux_cnt == 0) {
441 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
444 chr->focus = d->mux_cnt;
448 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
450 CharDriverState *chr;
453 chr = qemu_mallocz(sizeof(CharDriverState));
454 d = qemu_mallocz(sizeof(MuxDriver));
459 chr->chr_write = mux_chr_write;
460 chr->chr_update_read_handler = mux_chr_update_read_handler;
461 chr->chr_accept_input = mux_chr_accept_input;
467 int send_all(int fd, const void *buf, int len1)
473 ret = send(fd, buf, len, 0);
475 errno = WSAGetLastError();
476 if (errno != WSAEWOULDBLOCK) {
479 } else if (ret == 0) {
491 static int unix_write(int fd, const uint8_t *buf, int len1)
497 ret = write(fd, buf, len);
499 if (errno != EINTR && errno != EAGAIN)
501 } else if (ret == 0) {
511 int send_all(int fd, const void *buf, int len1)
513 return unix_write(fd, buf, len1);
524 #define STDIO_MAX_CLIENTS 1
525 static int stdio_nb_clients = 0;
527 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
529 FDCharDriver *s = chr->opaque;
530 return send_all(s->fd_out, buf, len);
533 static int fd_chr_read_poll(void *opaque)
535 CharDriverState *chr = opaque;
536 FDCharDriver *s = chr->opaque;
538 s->max_size = qemu_chr_can_read(chr);
542 static void fd_chr_read(void *opaque)
544 CharDriverState *chr = opaque;
545 FDCharDriver *s = chr->opaque;
550 if (len > s->max_size)
554 size = read(s->fd_in, buf, len);
556 /* FD has been closed. Remove it from the active list. */
557 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
561 qemu_chr_read(chr, buf, size);
565 static void fd_chr_update_read_handler(CharDriverState *chr)
567 FDCharDriver *s = chr->opaque;
570 if (nographic && s->fd_in == 0) {
572 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
573 fd_chr_read, NULL, chr);
578 static void fd_chr_close(struct CharDriverState *chr)
580 FDCharDriver *s = chr->opaque;
583 if (nographic && s->fd_in == 0) {
585 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
592 /* open a character device to a unix fd */
593 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
595 CharDriverState *chr;
598 chr = qemu_mallocz(sizeof(CharDriverState));
599 s = qemu_mallocz(sizeof(FDCharDriver));
603 chr->chr_write = fd_chr_write;
604 chr->chr_update_read_handler = fd_chr_update_read_handler;
605 chr->chr_close = fd_chr_close;
612 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
616 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
619 return qemu_chr_open_fd(-1, fd_out);
622 static CharDriverState *qemu_chr_open_pipe(const char *filename)
625 char filename_in[256], filename_out[256];
627 snprintf(filename_in, 256, "%s.in", filename);
628 snprintf(filename_out, 256, "%s.out", filename);
629 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
630 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
631 if (fd_in < 0 || fd_out < 0) {
636 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
640 return qemu_chr_open_fd(fd_in, fd_out);
644 /* for STDIO, we handle the case where several clients use it
647 #define TERM_FIFO_MAX_SIZE 1
649 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
650 static int term_fifo_size;
652 static int stdio_read_poll(void *opaque)
654 CharDriverState *chr = opaque;
656 /* try to flush the queue if needed */
657 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
658 qemu_chr_read(chr, term_fifo, 1);
661 /* see if we can absorb more chars */
662 if (term_fifo_size == 0)
668 static void stdio_read(void *opaque)
672 CharDriverState *chr = opaque;
674 size = read(0, buf, 1);
676 /* stdin has been closed. Remove it from the active list. */
677 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
681 if (qemu_chr_can_read(chr) > 0) {
682 qemu_chr_read(chr, buf, 1);
683 } else if (term_fifo_size == 0) {
684 term_fifo[term_fifo_size++] = buf[0];
689 /* init terminal so that we can grab keys */
690 static struct termios oldtty;
691 static int old_fd0_flags;
692 static int term_atexit_done;
694 static void term_exit(void)
696 tcsetattr (0, TCSANOW, &oldtty);
697 fcntl(0, F_SETFL, old_fd0_flags);
700 static void term_init(void)
706 old_fd0_flags = fcntl(0, F_GETFL);
708 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
709 |INLCR|IGNCR|ICRNL|IXON);
710 tty.c_oflag |= OPOST;
711 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
712 /* if graphical mode, we allow Ctrl-C handling */
714 tty.c_lflag &= ~ISIG;
715 tty.c_cflag &= ~(CSIZE|PARENB);
720 tcsetattr (0, TCSANOW, &tty);
722 if (!term_atexit_done++)
725 fcntl(0, F_SETFL, O_NONBLOCK);
728 static void qemu_chr_close_stdio(struct CharDriverState *chr)
732 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
736 static CharDriverState *qemu_chr_open_stdio(void)
738 CharDriverState *chr;
740 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
742 chr = qemu_chr_open_fd(0, 1);
743 chr->chr_close = qemu_chr_close_stdio;
744 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
752 /* Once Solaris has openpty(), this is going to be removed. */
753 int openpty(int *amaster, int *aslave, char *name,
754 struct termios *termp, struct winsize *winp)
757 int mfd = -1, sfd = -1;
759 *amaster = *aslave = -1;
761 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
765 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
768 if ((slave = ptsname(mfd)) == NULL)
771 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
774 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
775 (termp != NULL && tcgetattr(sfd, termp) < 0))
783 ioctl(sfd, TIOCSWINSZ, winp);
794 void cfmakeraw (struct termios *termios_p)
796 termios_p->c_iflag &=
797 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
798 termios_p->c_oflag &= ~OPOST;
799 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
800 termios_p->c_cflag &= ~(CSIZE|PARENB);
801 termios_p->c_cflag |= CS8;
803 termios_p->c_cc[VMIN] = 0;
804 termios_p->c_cc[VTIME] = 0;
808 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
809 || defined(__NetBSD__) || defined(__OpenBSD__)
819 static void pty_chr_update_read_handler(CharDriverState *chr);
820 static void pty_chr_state(CharDriverState *chr, int connected);
822 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
824 PtyCharDriver *s = chr->opaque;
827 /* guest sends data, check for (re-)connect */
828 pty_chr_update_read_handler(chr);
831 return send_all(s->fd, buf, len);
834 static int pty_chr_read_poll(void *opaque)
836 CharDriverState *chr = opaque;
837 PtyCharDriver *s = chr->opaque;
839 s->read_bytes = qemu_chr_can_read(chr);
840 return s->read_bytes;
843 static void pty_chr_read(void *opaque)
845 CharDriverState *chr = opaque;
846 PtyCharDriver *s = chr->opaque;
851 if (len > s->read_bytes)
855 size = read(s->fd, buf, len);
856 if ((size == -1 && errno == EIO) ||
858 pty_chr_state(chr, 0);
862 pty_chr_state(chr, 1);
863 qemu_chr_read(chr, buf, size);
867 static void pty_chr_update_read_handler(CharDriverState *chr)
869 PtyCharDriver *s = chr->opaque;
871 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
872 pty_chr_read, NULL, chr);
875 * Short timeout here: just need wait long enougth that qemu makes
876 * it through the poll loop once. When reconnected we want a
877 * short timeout so we notice it almost instantly. Otherwise
878 * read() gives us -EIO instantly, making pty_chr_state() reset the
879 * timeout to the normal (much longer) poll interval before the
882 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
885 static void pty_chr_state(CharDriverState *chr, int connected)
887 PtyCharDriver *s = chr->opaque;
890 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
893 /* (re-)connect poll interval for idle guests: once per second.
894 * We check more frequently in case the guests sends data to
895 * the virtual device linked to our pty. */
896 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
904 static void pty_chr_timer(void *opaque)
906 struct CharDriverState *chr = opaque;
907 PtyCharDriver *s = chr->opaque;
912 /* If we arrive here without polling being cleared due
913 * read returning -EIO, then we are (re-)connected */
914 pty_chr_state(chr, 1);
919 pty_chr_update_read_handler(chr);
922 static void pty_chr_close(struct CharDriverState *chr)
924 PtyCharDriver *s = chr->opaque;
926 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
931 static CharDriverState *qemu_chr_open_pty(void)
933 CharDriverState *chr;
937 #if defined(__OpenBSD__)
938 char pty_name[PATH_MAX];
939 #define q_ptsname(x) pty_name
941 char *pty_name = NULL;
942 #define q_ptsname(x) ptsname(x)
945 chr = qemu_mallocz(sizeof(CharDriverState));
946 s = qemu_mallocz(sizeof(PtyCharDriver));
948 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
952 /* Set raw attributes on the pty. */
953 tcgetattr(slave_fd, &tty);
955 tcsetattr(slave_fd, TCSAFLUSH, &tty);
958 len = strlen(q_ptsname(s->fd)) + 5;
959 chr->filename = qemu_malloc(len);
960 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
961 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
964 chr->chr_write = pty_chr_write;
965 chr->chr_update_read_handler = pty_chr_update_read_handler;
966 chr->chr_close = pty_chr_close;
968 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
973 static void tty_serial_init(int fd, int speed,
974 int parity, int data_bits, int stop_bits)
980 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
981 speed, parity, data_bits, stop_bits);
983 tcgetattr (fd, &tty);
986 if (speed <= 50 * MARGIN)
988 else if (speed <= 75 * MARGIN)
990 else if (speed <= 300 * MARGIN)
992 else if (speed <= 600 * MARGIN)
994 else if (speed <= 1200 * MARGIN)
996 else if (speed <= 2400 * MARGIN)
998 else if (speed <= 4800 * MARGIN)
1000 else if (speed <= 9600 * MARGIN)
1002 else if (speed <= 19200 * MARGIN)
1004 else if (speed <= 38400 * MARGIN)
1006 else if (speed <= 57600 * MARGIN)
1008 else if (speed <= 115200 * MARGIN)
1013 cfsetispeed(&tty, spd);
1014 cfsetospeed(&tty, spd);
1016 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1017 |INLCR|IGNCR|ICRNL|IXON);
1018 tty.c_oflag |= OPOST;
1019 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1020 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1041 tty.c_cflag |= PARENB;
1044 tty.c_cflag |= PARENB | PARODD;
1048 tty.c_cflag |= CSTOPB;
1050 tcsetattr (fd, TCSANOW, &tty);
1053 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1055 FDCharDriver *s = chr->opaque;
1058 case CHR_IOCTL_SERIAL_SET_PARAMS:
1060 QEMUSerialSetParams *ssp = arg;
1061 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1062 ssp->data_bits, ssp->stop_bits);
1065 case CHR_IOCTL_SERIAL_SET_BREAK:
1067 int enable = *(int *)arg;
1069 tcsendbreak(s->fd_in, 1);
1072 case CHR_IOCTL_SERIAL_GET_TIOCM:
1075 int *targ = (int *)arg;
1076 ioctl(s->fd_in, TIOCMGET, &sarg);
1078 if (sarg & TIOCM_CTS)
1079 *targ |= CHR_TIOCM_CTS;
1080 if (sarg & TIOCM_CAR)
1081 *targ |= CHR_TIOCM_CAR;
1082 if (sarg & TIOCM_DSR)
1083 *targ |= CHR_TIOCM_DSR;
1084 if (sarg & TIOCM_RI)
1085 *targ |= CHR_TIOCM_RI;
1086 if (sarg & TIOCM_DTR)
1087 *targ |= CHR_TIOCM_DTR;
1088 if (sarg & TIOCM_RTS)
1089 *targ |= CHR_TIOCM_RTS;
1092 case CHR_IOCTL_SERIAL_SET_TIOCM:
1094 int sarg = *(int *)arg;
1096 ioctl(s->fd_in, TIOCMGET, &targ);
1097 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1098 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1099 if (sarg & CHR_TIOCM_CTS)
1101 if (sarg & CHR_TIOCM_CAR)
1103 if (sarg & CHR_TIOCM_DSR)
1105 if (sarg & CHR_TIOCM_RI)
1107 if (sarg & CHR_TIOCM_DTR)
1109 if (sarg & CHR_TIOCM_RTS)
1111 ioctl(s->fd_in, TIOCMSET, &targ);
1120 static CharDriverState *qemu_chr_open_tty(const char *filename)
1122 CharDriverState *chr;
1125 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1126 tty_serial_init(fd, 115200, 'N', 8, 1);
1127 chr = qemu_chr_open_fd(fd, fd);
1132 chr->chr_ioctl = tty_serial_ioctl;
1133 qemu_chr_reset(chr);
1136 #else /* ! __linux__ && ! __sun__ */
1137 static CharDriverState *qemu_chr_open_pty(void)
1141 #endif /* __linux__ || __sun__ */
1143 #if defined(__linux__)
1147 } ParallelCharDriver;
1149 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1151 if (s->mode != mode) {
1153 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1160 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1162 ParallelCharDriver *drv = chr->opaque;
1167 case CHR_IOCTL_PP_READ_DATA:
1168 if (ioctl(fd, PPRDATA, &b) < 0)
1170 *(uint8_t *)arg = b;
1172 case CHR_IOCTL_PP_WRITE_DATA:
1173 b = *(uint8_t *)arg;
1174 if (ioctl(fd, PPWDATA, &b) < 0)
1177 case CHR_IOCTL_PP_READ_CONTROL:
1178 if (ioctl(fd, PPRCONTROL, &b) < 0)
1180 /* Linux gives only the lowest bits, and no way to know data
1181 direction! For better compatibility set the fixed upper
1183 *(uint8_t *)arg = b | 0xc0;
1185 case CHR_IOCTL_PP_WRITE_CONTROL:
1186 b = *(uint8_t *)arg;
1187 if (ioctl(fd, PPWCONTROL, &b) < 0)
1190 case CHR_IOCTL_PP_READ_STATUS:
1191 if (ioctl(fd, PPRSTATUS, &b) < 0)
1193 *(uint8_t *)arg = b;
1195 case CHR_IOCTL_PP_DATA_DIR:
1196 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1199 case CHR_IOCTL_PP_EPP_READ_ADDR:
1200 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1201 struct ParallelIOArg *parg = arg;
1202 int n = read(fd, parg->buffer, parg->count);
1203 if (n != parg->count) {
1208 case CHR_IOCTL_PP_EPP_READ:
1209 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1210 struct ParallelIOArg *parg = arg;
1211 int n = read(fd, parg->buffer, parg->count);
1212 if (n != parg->count) {
1217 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1218 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1219 struct ParallelIOArg *parg = arg;
1220 int n = write(fd, parg->buffer, parg->count);
1221 if (n != parg->count) {
1226 case CHR_IOCTL_PP_EPP_WRITE:
1227 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1228 struct ParallelIOArg *parg = arg;
1229 int n = write(fd, parg->buffer, parg->count);
1230 if (n != parg->count) {
1241 static void pp_close(CharDriverState *chr)
1243 ParallelCharDriver *drv = chr->opaque;
1246 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1247 ioctl(fd, PPRELEASE);
1252 static CharDriverState *qemu_chr_open_pp(const char *filename)
1254 CharDriverState *chr;
1255 ParallelCharDriver *drv;
1258 TFR(fd = open(filename, O_RDWR));
1262 if (ioctl(fd, PPCLAIM) < 0) {
1267 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1269 drv->mode = IEEE1284_MODE_COMPAT;
1271 chr = qemu_mallocz(sizeof(CharDriverState));
1272 chr->chr_write = null_chr_write;
1273 chr->chr_ioctl = pp_ioctl;
1274 chr->chr_close = pp_close;
1277 qemu_chr_reset(chr);
1281 #endif /* __linux__ */
1283 #if defined(__FreeBSD__)
1284 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1286 int fd = (int)chr->opaque;
1290 case CHR_IOCTL_PP_READ_DATA:
1291 if (ioctl(fd, PPIGDATA, &b) < 0)
1293 *(uint8_t *)arg = b;
1295 case CHR_IOCTL_PP_WRITE_DATA:
1296 b = *(uint8_t *)arg;
1297 if (ioctl(fd, PPISDATA, &b) < 0)
1300 case CHR_IOCTL_PP_READ_CONTROL:
1301 if (ioctl(fd, PPIGCTRL, &b) < 0)
1303 *(uint8_t *)arg = b;
1305 case CHR_IOCTL_PP_WRITE_CONTROL:
1306 b = *(uint8_t *)arg;
1307 if (ioctl(fd, PPISCTRL, &b) < 0)
1310 case CHR_IOCTL_PP_READ_STATUS:
1311 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1313 *(uint8_t *)arg = b;
1321 static CharDriverState *qemu_chr_open_pp(const char *filename)
1323 CharDriverState *chr;
1326 fd = open(filename, O_RDWR);
1330 chr = qemu_mallocz(sizeof(CharDriverState));
1331 chr->opaque = (void *)fd;
1332 chr->chr_write = null_chr_write;
1333 chr->chr_ioctl = pp_ioctl;
1342 HANDLE hcom, hrecv, hsend;
1343 OVERLAPPED orecv, osend;
1348 #define NSENDBUF 2048
1349 #define NRECVBUF 2048
1350 #define MAXCONNECT 1
1351 #define NTIMEOUT 5000
1353 static int win_chr_poll(void *opaque);
1354 static int win_chr_pipe_poll(void *opaque);
1356 static void win_chr_close(CharDriverState *chr)
1358 WinCharState *s = chr->opaque;
1361 CloseHandle(s->hsend);
1365 CloseHandle(s->hrecv);
1369 CloseHandle(s->hcom);
1373 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1375 qemu_del_polling_cb(win_chr_poll, chr);
1378 static int win_chr_init(CharDriverState *chr, const char *filename)
1380 WinCharState *s = chr->opaque;
1382 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1387 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1389 fprintf(stderr, "Failed CreateEvent\n");
1392 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1394 fprintf(stderr, "Failed CreateEvent\n");
1398 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1399 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1400 if (s->hcom == INVALID_HANDLE_VALUE) {
1401 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1406 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1407 fprintf(stderr, "Failed SetupComm\n");
1411 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1412 size = sizeof(COMMCONFIG);
1413 GetDefaultCommConfig(filename, &comcfg, &size);
1414 comcfg.dcb.DCBlength = sizeof(DCB);
1415 CommConfigDialog(filename, NULL, &comcfg);
1417 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1418 fprintf(stderr, "Failed SetCommState\n");
1422 if (!SetCommMask(s->hcom, EV_ERR)) {
1423 fprintf(stderr, "Failed SetCommMask\n");
1427 cto.ReadIntervalTimeout = MAXDWORD;
1428 if (!SetCommTimeouts(s->hcom, &cto)) {
1429 fprintf(stderr, "Failed SetCommTimeouts\n");
1433 if (!ClearCommError(s->hcom, &err, &comstat)) {
1434 fprintf(stderr, "Failed ClearCommError\n");
1437 qemu_add_polling_cb(win_chr_poll, chr);
1445 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1447 WinCharState *s = chr->opaque;
1448 DWORD len, ret, size, err;
1451 ZeroMemory(&s->osend, sizeof(s->osend));
1452 s->osend.hEvent = s->hsend;
1455 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1457 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1459 err = GetLastError();
1460 if (err == ERROR_IO_PENDING) {
1461 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1479 static int win_chr_read_poll(CharDriverState *chr)
1481 WinCharState *s = chr->opaque;
1483 s->max_size = qemu_chr_can_read(chr);
1487 static void win_chr_readfile(CharDriverState *chr)
1489 WinCharState *s = chr->opaque;
1494 ZeroMemory(&s->orecv, sizeof(s->orecv));
1495 s->orecv.hEvent = s->hrecv;
1496 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1498 err = GetLastError();
1499 if (err == ERROR_IO_PENDING) {
1500 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1505 qemu_chr_read(chr, buf, size);
1509 static void win_chr_read(CharDriverState *chr)
1511 WinCharState *s = chr->opaque;
1513 if (s->len > s->max_size)
1514 s->len = s->max_size;
1518 win_chr_readfile(chr);
1521 static int win_chr_poll(void *opaque)
1523 CharDriverState *chr = opaque;
1524 WinCharState *s = chr->opaque;
1528 ClearCommError(s->hcom, &comerr, &status);
1529 if (status.cbInQue > 0) {
1530 s->len = status.cbInQue;
1531 win_chr_read_poll(chr);
1538 static CharDriverState *qemu_chr_open_win(const char *filename)
1540 CharDriverState *chr;
1543 chr = qemu_mallocz(sizeof(CharDriverState));
1544 s = qemu_mallocz(sizeof(WinCharState));
1546 chr->chr_write = win_chr_write;
1547 chr->chr_close = win_chr_close;
1549 if (win_chr_init(chr, filename) < 0) {
1554 qemu_chr_reset(chr);
1558 static int win_chr_pipe_poll(void *opaque)
1560 CharDriverState *chr = opaque;
1561 WinCharState *s = chr->opaque;
1564 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1567 win_chr_read_poll(chr);
1574 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1576 WinCharState *s = chr->opaque;
1584 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1586 fprintf(stderr, "Failed CreateEvent\n");
1589 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1591 fprintf(stderr, "Failed CreateEvent\n");
1595 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1596 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1597 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1599 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1600 if (s->hcom == INVALID_HANDLE_VALUE) {
1601 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1606 ZeroMemory(&ov, sizeof(ov));
1607 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1608 ret = ConnectNamedPipe(s->hcom, &ov);
1610 fprintf(stderr, "Failed ConnectNamedPipe\n");
1614 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1616 fprintf(stderr, "Failed GetOverlappedResult\n");
1618 CloseHandle(ov.hEvent);
1625 CloseHandle(ov.hEvent);
1628 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1637 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
1639 CharDriverState *chr;
1642 chr = qemu_mallocz(sizeof(CharDriverState));
1643 s = qemu_mallocz(sizeof(WinCharState));
1645 chr->chr_write = win_chr_write;
1646 chr->chr_close = win_chr_close;
1648 if (win_chr_pipe_init(chr, filename) < 0) {
1653 qemu_chr_reset(chr);
1657 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1659 CharDriverState *chr;
1662 chr = qemu_mallocz(sizeof(CharDriverState));
1663 s = qemu_mallocz(sizeof(WinCharState));
1666 chr->chr_write = win_chr_write;
1667 qemu_chr_reset(chr);
1671 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1673 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1676 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
1680 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1681 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1682 if (fd_out == INVALID_HANDLE_VALUE)
1685 return qemu_chr_open_win_file(fd_out);
1687 #endif /* !_WIN32 */
1689 /***********************************************************/
1690 /* UDP Net console */
1694 struct sockaddr_in daddr;
1701 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1703 NetCharDriver *s = chr->opaque;
1705 return sendto(s->fd, buf, len, 0,
1706 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1709 static int udp_chr_read_poll(void *opaque)
1711 CharDriverState *chr = opaque;
1712 NetCharDriver *s = chr->opaque;
1714 s->max_size = qemu_chr_can_read(chr);
1716 /* If there were any stray characters in the queue process them
1719 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1720 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1722 s->max_size = qemu_chr_can_read(chr);
1727 static void udp_chr_read(void *opaque)
1729 CharDriverState *chr = opaque;
1730 NetCharDriver *s = chr->opaque;
1732 if (s->max_size == 0)
1734 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
1735 s->bufptr = s->bufcnt;
1740 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1741 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1743 s->max_size = qemu_chr_can_read(chr);
1747 static void udp_chr_update_read_handler(CharDriverState *chr)
1749 NetCharDriver *s = chr->opaque;
1752 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1753 udp_chr_read, NULL, chr);
1757 static CharDriverState *qemu_chr_open_udp(const char *def)
1759 CharDriverState *chr = NULL;
1760 NetCharDriver *s = NULL;
1762 struct sockaddr_in saddr;
1764 chr = qemu_mallocz(sizeof(CharDriverState));
1765 s = qemu_mallocz(sizeof(NetCharDriver));
1767 fd = socket(PF_INET, SOCK_DGRAM, 0);
1769 perror("socket(PF_INET, SOCK_DGRAM)");
1773 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1774 printf("Could not parse: %s\n", def);
1778 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1788 chr->chr_write = udp_chr_write;
1789 chr->chr_update_read_handler = udp_chr_update_read_handler;
1802 /***********************************************************/
1803 /* TCP Net console */
1814 static void tcp_chr_accept(void *opaque);
1816 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1818 TCPCharDriver *s = chr->opaque;
1820 return send_all(s->fd, buf, len);
1822 /* XXX: indicate an error ? */
1827 static int tcp_chr_read_poll(void *opaque)
1829 CharDriverState *chr = opaque;
1830 TCPCharDriver *s = chr->opaque;
1833 s->max_size = qemu_chr_can_read(chr);
1838 #define IAC_BREAK 243
1839 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1841 uint8_t *buf, int *size)
1843 /* Handle any telnet client's basic IAC options to satisfy char by
1844 * char mode with no echo. All IAC options will be removed from
1845 * the buf and the do_telnetopt variable will be used to track the
1846 * state of the width of the IAC information.
1848 * IAC commands come in sets of 3 bytes with the exception of the
1849 * "IAC BREAK" command and the double IAC.
1855 for (i = 0; i < *size; i++) {
1856 if (s->do_telnetopt > 1) {
1857 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1858 /* Double IAC means send an IAC */
1862 s->do_telnetopt = 1;
1864 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1865 /* Handle IAC break commands by sending a serial break */
1866 qemu_chr_event(chr, CHR_EVENT_BREAK);
1871 if (s->do_telnetopt >= 4) {
1872 s->do_telnetopt = 1;
1875 if ((unsigned char)buf[i] == IAC) {
1876 s->do_telnetopt = 2;
1887 static void tcp_chr_read(void *opaque)
1889 CharDriverState *chr = opaque;
1890 TCPCharDriver *s = chr->opaque;
1894 if (!s->connected || s->max_size <= 0)
1897 if (len > s->max_size)
1899 size = recv(s->fd, buf, len, 0);
1901 /* connection closed */
1903 if (s->listen_fd >= 0) {
1904 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1906 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1909 } else if (size > 0) {
1910 if (s->do_telnetopt)
1911 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1913 qemu_chr_read(chr, buf, size);
1917 static void tcp_chr_connect(void *opaque)
1919 CharDriverState *chr = opaque;
1920 TCPCharDriver *s = chr->opaque;
1923 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1924 tcp_chr_read, NULL, chr);
1925 qemu_chr_reset(chr);
1928 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1929 static void tcp_chr_telnet_init(int fd)
1932 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1933 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1934 send(fd, (char *)buf, 3, 0);
1935 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1936 send(fd, (char *)buf, 3, 0);
1937 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
1938 send(fd, (char *)buf, 3, 0);
1939 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
1940 send(fd, (char *)buf, 3, 0);
1943 static void socket_set_nodelay(int fd)
1946 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1949 static void tcp_chr_accept(void *opaque)
1951 CharDriverState *chr = opaque;
1952 TCPCharDriver *s = chr->opaque;
1953 struct sockaddr_in saddr;
1955 struct sockaddr_un uaddr;
1957 struct sockaddr *addr;
1964 len = sizeof(uaddr);
1965 addr = (struct sockaddr *)&uaddr;
1969 len = sizeof(saddr);
1970 addr = (struct sockaddr *)&saddr;
1972 fd = accept(s->listen_fd, addr, &len);
1973 if (fd < 0 && errno != EINTR) {
1975 } else if (fd >= 0) {
1976 if (s->do_telnetopt)
1977 tcp_chr_telnet_init(fd);
1981 socket_set_nonblock(fd);
1983 socket_set_nodelay(fd);
1985 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
1986 tcp_chr_connect(chr);
1989 static void tcp_chr_close(CharDriverState *chr)
1991 TCPCharDriver *s = chr->opaque;
1994 if (s->listen_fd >= 0)
1995 closesocket(s->listen_fd);
1999 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
2003 CharDriverState *chr = NULL;
2004 TCPCharDriver *s = NULL;
2005 int fd = -1, offset = 0;
2007 int is_waitconnect = 1;
2012 while((ptr = strchr(ptr,','))) {
2014 if (!strncmp(ptr,"server",6)) {
2016 } else if (!strncmp(ptr,"nowait",6)) {
2018 } else if (!strncmp(ptr,"nodelay",6)) {
2020 } else if (!strncmp(ptr,"to=",3)) {
2021 /* nothing, inet_listen() parses this one */;
2022 } else if (!strncmp(ptr,"ipv4",4)) {
2023 /* nothing, inet_connect() and inet_listen() parse this one */;
2024 } else if (!strncmp(ptr,"ipv6",4)) {
2025 /* nothing, inet_connect() and inet_listen() parse this one */;
2027 printf("Unknown option: %s\n", ptr);
2034 chr = qemu_mallocz(sizeof(CharDriverState));
2035 s = qemu_mallocz(sizeof(TCPCharDriver));
2038 chr->filename = qemu_malloc(256);
2040 pstrcpy(chr->filename, 256, "unix:");
2041 } else if (is_telnet) {
2042 pstrcpy(chr->filename, 256, "telnet:");
2044 pstrcpy(chr->filename, 256, "tcp:");
2046 offset = strlen(chr->filename);
2050 fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
2052 fd = unix_connect(host_str);
2056 fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
2059 fd = inet_connect(host_str, SOCK_STREAM);
2065 if (!is_waitconnect)
2066 socket_set_nonblock(fd);
2071 s->is_unix = is_unix;
2072 s->do_nodelay = do_nodelay && !is_unix;
2075 chr->chr_write = tcp_chr_write;
2076 chr->chr_close = tcp_chr_close;
2080 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2082 s->do_telnetopt = 1;
2086 socket_set_nodelay(fd);
2087 tcp_chr_connect(chr);
2090 if (is_listen && is_waitconnect) {
2091 printf("QEMU waiting for connection on: %s\n",
2092 chr->filename ? chr->filename : host_str);
2093 tcp_chr_accept(chr);
2094 socket_set_nonblock(s->listen_fd);
2106 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2109 CharDriverState *chr;
2111 if (!strcmp(filename, "vc")) {
2112 chr = text_console_init(0);
2114 if (strstart(filename, "vc:", &p)) {
2115 chr = text_console_init(p);
2117 if (!strcmp(filename, "null")) {
2118 chr = qemu_chr_open_null();
2120 if (strstart(filename, "tcp:", &p)) {
2121 chr = qemu_chr_open_tcp(p, 0, 0);
2123 if (strstart(filename, "telnet:", &p)) {
2124 chr = qemu_chr_open_tcp(p, 1, 0);
2126 if (strstart(filename, "udp:", &p)) {
2127 chr = qemu_chr_open_udp(p);
2129 if (strstart(filename, "mon:", &p)) {
2130 chr = qemu_chr_open(label, p, NULL);
2132 chr = qemu_chr_open_mux(chr);
2133 monitor_init(chr, MONITOR_USE_READLINE);
2135 printf("Unable to open driver: %s\n", p);
2137 } else if (!strcmp(filename, "msmouse")) {
2138 chr = qemu_chr_open_msmouse();
2141 if (strstart(filename, "unix:", &p)) {
2142 chr = qemu_chr_open_tcp(p, 0, 1);
2143 } else if (strstart(filename, "file:", &p)) {
2144 chr = qemu_chr_open_file_out(p);
2145 } else if (strstart(filename, "pipe:", &p)) {
2146 chr = qemu_chr_open_pipe(p);
2147 } else if (!strcmp(filename, "pty")) {
2148 chr = qemu_chr_open_pty();
2149 } else if (!strcmp(filename, "stdio")) {
2150 chr = qemu_chr_open_stdio();
2152 #if defined(__linux__)
2153 if (strstart(filename, "/dev/parport", NULL)) {
2154 chr = qemu_chr_open_pp(filename);
2156 #elif defined(__FreeBSD__)
2157 if (strstart(filename, "/dev/ppi", NULL)) {
2158 chr = qemu_chr_open_pp(filename);
2161 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2162 || defined(__NetBSD__) || defined(__OpenBSD__)
2163 if (strstart(filename, "/dev/", NULL)) {
2164 chr = qemu_chr_open_tty(filename);
2168 if (strstart(filename, "COM", NULL)) {
2169 chr = qemu_chr_open_win(filename);
2171 if (strstart(filename, "pipe:", &p)) {
2172 chr = qemu_chr_open_win_pipe(p);
2174 if (strstart(filename, "con:", NULL)) {
2175 chr = qemu_chr_open_win_con(filename);
2177 if (strstart(filename, "file:", &p)) {
2178 chr = qemu_chr_open_win_file_out(p);
2181 #ifdef CONFIG_BRLAPI
2182 if (!strcmp(filename, "braille")) {
2183 chr = chr_baum_init();
2192 chr->filename = qemu_strdup(filename);
2194 chr->label = qemu_strdup(label);
2195 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2200 void qemu_chr_close(CharDriverState *chr)
2202 TAILQ_REMOVE(&chardevs, chr, next);
2204 chr->chr_close(chr);
2205 qemu_free(chr->filename);
2206 qemu_free(chr->label);
2210 void qemu_chr_info(Monitor *mon)
2212 CharDriverState *chr;
2214 TAILQ_FOREACH(chr, &chardevs, next) {
2215 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);