4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
34 #include "hw/msmouse.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
54 #include <arpa/inet.h>
57 #include <sys/select.h>
62 #include <dev/ppbus/ppi.h>
63 #include <dev/ppbus/ppbconf.h>
64 #elif defined(__DragonFly__)
66 #include <dev/misc/ppi/ppi.h>
67 #include <bus/ppbus/ppbconf.h>
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
98 #include "qemu_socket.h"
100 #define READ_BUF_LEN 4096
102 /***********************************************************/
103 /* character device */
105 static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
106 QTAILQ_HEAD_INITIALIZER(chardevs);
108 static void qemu_chr_event(CharDriverState *s, int event)
112 s->chr_event(s->handler_opaque, event);
115 static void qemu_chr_generic_open_bh(void *opaque)
117 CharDriverState *s = opaque;
118 qemu_chr_event(s, CHR_EVENT_OPENED);
119 qemu_bh_delete(s->bh);
123 void qemu_chr_generic_open(CharDriverState *s)
126 s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s);
127 qemu_bh_schedule(s->bh);
131 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
133 return s->chr_write(s, buf, len);
136 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
140 return s->chr_ioctl(s, cmd, arg);
143 int qemu_chr_can_read(CharDriverState *s)
145 if (!s->chr_can_read)
147 return s->chr_can_read(s->handler_opaque);
150 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
152 s->chr_read(s->handler_opaque, buf, len);
155 int qemu_chr_get_msgfd(CharDriverState *s)
157 return s->get_msgfd ? s->get_msgfd(s) : -1;
160 void qemu_chr_accept_input(CharDriverState *s)
162 if (s->chr_accept_input)
163 s->chr_accept_input(s);
166 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
168 char buf[READ_BUF_LEN];
171 vsnprintf(buf, sizeof(buf), fmt, ap);
172 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
176 void qemu_chr_send_event(CharDriverState *s, int event)
178 if (s->chr_send_event)
179 s->chr_send_event(s, event);
182 void qemu_chr_add_handlers(CharDriverState *s,
183 IOCanRWHandler *fd_can_read,
184 IOReadHandler *fd_read,
185 IOEventHandler *fd_event,
188 s->chr_can_read = fd_can_read;
189 s->chr_read = fd_read;
190 s->chr_event = fd_event;
191 s->handler_opaque = opaque;
192 if (s->chr_update_read_handler)
193 s->chr_update_read_handler(s);
196 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
201 static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
203 CharDriverState *chr;
205 chr = qemu_mallocz(sizeof(CharDriverState));
206 chr->chr_write = null_chr_write;
210 /* MUX driver for serial I/O splitting */
212 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
213 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
215 IOCanRWHandler *chr_can_read[MAX_MUX];
216 IOReadHandler *chr_read[MAX_MUX];
217 IOEventHandler *chr_event[MAX_MUX];
218 void *ext_opaque[MAX_MUX];
219 CharDriverState *drv;
224 /* Intermediate input buffer allows to catch escape sequences even if the
225 currently active device is not accepting any input - but only until it
227 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
232 int64_t timestamps_start;
236 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
238 MuxDriver *d = chr->opaque;
240 if (!d->timestamps) {
241 ret = d->drv->chr_write(d->drv, buf, len);
246 for (i = 0; i < len; i++) {
252 ti = qemu_get_clock(rt_clock);
253 if (d->timestamps_start == -1)
254 d->timestamps_start = ti;
255 ti -= d->timestamps_start;
257 snprintf(buf1, sizeof(buf1),
258 "[%02d:%02d:%02d.%03d] ",
263 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
266 ret += d->drv->chr_write(d->drv, buf+i, 1);
267 if (buf[i] == '\n') {
275 static const char * const mux_help[] = {
276 "% h print this help\n\r",
277 "% x exit emulator\n\r",
278 "% s save disk data back to file (if -snapshot)\n\r",
279 "% t toggle console timestamps\n\r"
280 "% b send break (magic sysrq)\n\r",
281 "% c switch between console and monitor\n\r",
286 int term_escape_char = 0x01; /* ctrl-a is used for escape */
287 static void mux_print_help(CharDriverState *chr)
290 char ebuf[15] = "Escape-Char";
291 char cbuf[50] = "\n\r";
293 if (term_escape_char > 0 && term_escape_char < 26) {
294 snprintf(cbuf, sizeof(cbuf), "\n\r");
295 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
297 snprintf(cbuf, sizeof(cbuf),
298 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
301 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
302 for (i = 0; mux_help[i] != NULL; i++) {
303 for (j=0; mux_help[i][j] != '\0'; j++) {
304 if (mux_help[i][j] == '%')
305 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
307 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
312 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
314 if (d->chr_event[mux_nr])
315 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
318 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
320 if (d->term_got_escape) {
321 d->term_got_escape = 0;
322 if (ch == term_escape_char)
331 const char *term = "QEMU: Terminated\n\r";
332 chr->chr_write(chr,(uint8_t *)term,strlen(term));
339 QTAILQ_FOREACH(dinfo, &drives, next) {
340 bdrv_commit(dinfo->bdrv);
345 qemu_chr_event(chr, CHR_EVENT_BREAK);
348 /* Switch to the next registered device */
349 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
351 if (d->focus >= d->mux_cnt)
353 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
356 d->timestamps = !d->timestamps;
357 d->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)
372 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 if (d->focus != -1) {
445 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
447 d->focus = d->mux_cnt;
449 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
452 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
454 CharDriverState *chr;
457 chr = qemu_mallocz(sizeof(CharDriverState));
458 d = qemu_mallocz(sizeof(MuxDriver));
463 chr->chr_write = mux_chr_write;
464 chr->chr_update_read_handler = mux_chr_update_read_handler;
465 chr->chr_accept_input = mux_chr_accept_input;
471 int send_all(int fd, const void *buf, int len1)
477 ret = send(fd, buf, len, 0);
479 errno = WSAGetLastError();
480 if (errno != WSAEWOULDBLOCK) {
483 } else if (ret == 0) {
495 static int unix_write(int fd, const uint8_t *buf, int len1)
501 ret = write(fd, buf, len);
503 if (errno != EINTR && errno != EAGAIN)
505 } else if (ret == 0) {
515 int send_all(int fd, const void *buf, int len1)
517 return unix_write(fd, buf, len1);
528 #define STDIO_MAX_CLIENTS 1
529 static int stdio_nb_clients = 0;
531 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
533 FDCharDriver *s = chr->opaque;
534 return send_all(s->fd_out, buf, len);
537 static int fd_chr_read_poll(void *opaque)
539 CharDriverState *chr = opaque;
540 FDCharDriver *s = chr->opaque;
542 s->max_size = qemu_chr_can_read(chr);
546 static void fd_chr_read(void *opaque)
548 CharDriverState *chr = opaque;
549 FDCharDriver *s = chr->opaque;
551 uint8_t buf[READ_BUF_LEN];
554 if (len > s->max_size)
558 size = read(s->fd_in, buf, len);
560 /* FD has been closed. Remove it from the active list. */
561 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
562 qemu_chr_event(chr, CHR_EVENT_CLOSED);
566 qemu_chr_read(chr, buf, size);
570 static void fd_chr_update_read_handler(CharDriverState *chr)
572 FDCharDriver *s = chr->opaque;
575 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
577 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
578 fd_chr_read, NULL, chr);
583 static void fd_chr_close(struct CharDriverState *chr)
585 FDCharDriver *s = chr->opaque;
588 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
590 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
595 qemu_chr_event(chr, CHR_EVENT_CLOSED);
598 /* open a character device to a unix fd */
599 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
601 CharDriverState *chr;
604 chr = qemu_mallocz(sizeof(CharDriverState));
605 s = qemu_mallocz(sizeof(FDCharDriver));
609 chr->chr_write = fd_chr_write;
610 chr->chr_update_read_handler = fd_chr_update_read_handler;
611 chr->chr_close = fd_chr_close;
613 qemu_chr_generic_open(chr);
618 static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
622 TFR(fd_out = open(qemu_opt_get(opts, "path"),
623 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
626 return qemu_chr_open_fd(-1, fd_out);
629 static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
632 char filename_in[256], filename_out[256];
633 const char *filename = qemu_opt_get(opts, "path");
635 if (filename == NULL) {
636 fprintf(stderr, "chardev: pipe: no filename given\n");
640 snprintf(filename_in, 256, "%s.in", filename);
641 snprintf(filename_out, 256, "%s.out", filename);
642 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
643 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
644 if (fd_in < 0 || fd_out < 0) {
649 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
653 return qemu_chr_open_fd(fd_in, fd_out);
657 /* for STDIO, we handle the case where several clients use it
660 #define TERM_FIFO_MAX_SIZE 1
662 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
663 static int term_fifo_size;
665 static int stdio_read_poll(void *opaque)
667 CharDriverState *chr = opaque;
669 /* try to flush the queue if needed */
670 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
671 qemu_chr_read(chr, term_fifo, 1);
674 /* see if we can absorb more chars */
675 if (term_fifo_size == 0)
681 static void stdio_read(void *opaque)
685 CharDriverState *chr = opaque;
687 size = read(0, buf, 1);
689 /* stdin has been closed. Remove it from the active list. */
690 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
691 qemu_chr_event(chr, CHR_EVENT_CLOSED);
695 if (qemu_chr_can_read(chr) > 0) {
696 qemu_chr_read(chr, buf, 1);
697 } else if (term_fifo_size == 0) {
698 term_fifo[term_fifo_size++] = buf[0];
703 /* init terminal so that we can grab keys */
704 static struct termios oldtty;
705 static int old_fd0_flags;
706 static int term_atexit_done;
708 static void term_exit(void)
710 tcsetattr (0, TCSANOW, &oldtty);
711 fcntl(0, F_SETFL, old_fd0_flags);
714 static void term_init(QemuOpts *opts)
720 old_fd0_flags = fcntl(0, F_GETFL);
722 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
723 |INLCR|IGNCR|ICRNL|IXON);
724 tty.c_oflag |= OPOST;
725 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
726 /* if graphical mode, we allow Ctrl-C handling */
727 if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC))
728 tty.c_lflag &= ~ISIG;
729 tty.c_cflag &= ~(CSIZE|PARENB);
734 tcsetattr (0, TCSANOW, &tty);
736 if (!term_atexit_done++)
739 fcntl(0, F_SETFL, O_NONBLOCK);
742 static void qemu_chr_close_stdio(struct CharDriverState *chr)
746 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
750 static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
752 CharDriverState *chr;
754 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
756 chr = qemu_chr_open_fd(0, 1);
757 chr->chr_close = qemu_chr_close_stdio;
758 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
766 /* Once Solaris has openpty(), this is going to be removed. */
767 static int openpty(int *amaster, int *aslave, char *name,
768 struct termios *termp, struct winsize *winp)
771 int mfd = -1, sfd = -1;
773 *amaster = *aslave = -1;
775 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
779 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
782 if ((slave = ptsname(mfd)) == NULL)
785 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
788 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
789 (termp != NULL && tcgetattr(sfd, termp) < 0))
797 ioctl(sfd, TIOCSWINSZ, winp);
808 static void cfmakeraw (struct termios *termios_p)
810 termios_p->c_iflag &=
811 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
812 termios_p->c_oflag &= ~OPOST;
813 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
814 termios_p->c_cflag &= ~(CSIZE|PARENB);
815 termios_p->c_cflag |= CS8;
817 termios_p->c_cc[VMIN] = 0;
818 termios_p->c_cc[VTIME] = 0;
822 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
823 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
833 static void pty_chr_update_read_handler(CharDriverState *chr);
834 static void pty_chr_state(CharDriverState *chr, int connected);
836 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
838 PtyCharDriver *s = chr->opaque;
841 /* guest sends data, check for (re-)connect */
842 pty_chr_update_read_handler(chr);
845 return send_all(s->fd, buf, len);
848 static int pty_chr_read_poll(void *opaque)
850 CharDriverState *chr = opaque;
851 PtyCharDriver *s = chr->opaque;
853 s->read_bytes = qemu_chr_can_read(chr);
854 return s->read_bytes;
857 static void pty_chr_read(void *opaque)
859 CharDriverState *chr = opaque;
860 PtyCharDriver *s = chr->opaque;
862 uint8_t buf[READ_BUF_LEN];
865 if (len > s->read_bytes)
869 size = read(s->fd, buf, len);
870 if ((size == -1 && errno == EIO) ||
872 pty_chr_state(chr, 0);
876 pty_chr_state(chr, 1);
877 qemu_chr_read(chr, buf, size);
881 static void pty_chr_update_read_handler(CharDriverState *chr)
883 PtyCharDriver *s = chr->opaque;
885 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
886 pty_chr_read, NULL, chr);
889 * Short timeout here: just need wait long enougth that qemu makes
890 * it through the poll loop once. When reconnected we want a
891 * short timeout so we notice it almost instantly. Otherwise
892 * read() gives us -EIO instantly, making pty_chr_state() reset the
893 * timeout to the normal (much longer) poll interval before the
896 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
899 static void pty_chr_state(CharDriverState *chr, int connected)
901 PtyCharDriver *s = chr->opaque;
904 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
907 /* (re-)connect poll interval for idle guests: once per second.
908 * We check more frequently in case the guests sends data to
909 * the virtual device linked to our pty. */
910 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
913 qemu_chr_generic_open(chr);
918 static void pty_chr_timer(void *opaque)
920 struct CharDriverState *chr = opaque;
921 PtyCharDriver *s = chr->opaque;
926 /* If we arrive here without polling being cleared due
927 * read returning -EIO, then we are (re-)connected */
928 pty_chr_state(chr, 1);
933 pty_chr_update_read_handler(chr);
936 static void pty_chr_close(struct CharDriverState *chr)
938 PtyCharDriver *s = chr->opaque;
940 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
942 qemu_del_timer(s->timer);
943 qemu_free_timer(s->timer);
945 qemu_chr_event(chr, CHR_EVENT_CLOSED);
948 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
950 CharDriverState *chr;
954 #if defined(__OpenBSD__) || defined(__DragonFly__)
955 char pty_name[PATH_MAX];
956 #define q_ptsname(x) pty_name
958 char *pty_name = NULL;
959 #define q_ptsname(x) ptsname(x)
962 chr = qemu_mallocz(sizeof(CharDriverState));
963 s = qemu_mallocz(sizeof(PtyCharDriver));
965 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
969 /* Set raw attributes on the pty. */
970 tcgetattr(slave_fd, &tty);
972 tcsetattr(slave_fd, TCSAFLUSH, &tty);
975 len = strlen(q_ptsname(s->fd)) + 5;
976 chr->filename = qemu_malloc(len);
977 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
978 qemu_opt_set(opts, "path", q_ptsname(s->fd));
979 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
982 chr->chr_write = pty_chr_write;
983 chr->chr_update_read_handler = pty_chr_update_read_handler;
984 chr->chr_close = pty_chr_close;
986 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
991 static void tty_serial_init(int fd, int speed,
992 int parity, int data_bits, int stop_bits)
998 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
999 speed, parity, data_bits, stop_bits);
1001 tcgetattr (fd, &tty);
1003 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1004 speed = speed * 10 / 11;
1021 /* Non-Posix values follow. They may be unsupported on some systems. */
1023 check_speed(115200);
1025 check_speed(230400);
1028 check_speed(460800);
1031 check_speed(500000);
1034 check_speed(576000);
1037 check_speed(921600);
1040 check_speed(1000000);
1043 check_speed(1152000);
1046 check_speed(1500000);
1049 check_speed(2000000);
1052 check_speed(2500000);
1055 check_speed(3000000);
1058 check_speed(3500000);
1061 check_speed(4000000);
1066 cfsetispeed(&tty, spd);
1067 cfsetospeed(&tty, spd);
1069 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1070 |INLCR|IGNCR|ICRNL|IXON);
1071 tty.c_oflag |= OPOST;
1072 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1073 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1094 tty.c_cflag |= PARENB;
1097 tty.c_cflag |= PARENB | PARODD;
1101 tty.c_cflag |= CSTOPB;
1103 tcsetattr (fd, TCSANOW, &tty);
1106 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1108 FDCharDriver *s = chr->opaque;
1111 case CHR_IOCTL_SERIAL_SET_PARAMS:
1113 QEMUSerialSetParams *ssp = arg;
1114 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1115 ssp->data_bits, ssp->stop_bits);
1118 case CHR_IOCTL_SERIAL_SET_BREAK:
1120 int enable = *(int *)arg;
1122 tcsendbreak(s->fd_in, 1);
1125 case CHR_IOCTL_SERIAL_GET_TIOCM:
1128 int *targ = (int *)arg;
1129 ioctl(s->fd_in, TIOCMGET, &sarg);
1131 if (sarg & TIOCM_CTS)
1132 *targ |= CHR_TIOCM_CTS;
1133 if (sarg & TIOCM_CAR)
1134 *targ |= CHR_TIOCM_CAR;
1135 if (sarg & TIOCM_DSR)
1136 *targ |= CHR_TIOCM_DSR;
1137 if (sarg & TIOCM_RI)
1138 *targ |= CHR_TIOCM_RI;
1139 if (sarg & TIOCM_DTR)
1140 *targ |= CHR_TIOCM_DTR;
1141 if (sarg & TIOCM_RTS)
1142 *targ |= CHR_TIOCM_RTS;
1145 case CHR_IOCTL_SERIAL_SET_TIOCM:
1147 int sarg = *(int *)arg;
1149 ioctl(s->fd_in, TIOCMGET, &targ);
1150 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1151 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1152 if (sarg & CHR_TIOCM_CTS)
1154 if (sarg & CHR_TIOCM_CAR)
1156 if (sarg & CHR_TIOCM_DSR)
1158 if (sarg & CHR_TIOCM_RI)
1160 if (sarg & CHR_TIOCM_DTR)
1162 if (sarg & CHR_TIOCM_RTS)
1164 ioctl(s->fd_in, TIOCMSET, &targ);
1173 static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
1175 const char *filename = qemu_opt_get(opts, "path");
1176 CharDriverState *chr;
1179 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1180 tty_serial_init(fd, 115200, 'N', 8, 1);
1181 chr = qemu_chr_open_fd(fd, fd);
1186 chr->chr_ioctl = tty_serial_ioctl;
1187 qemu_chr_generic_open(chr);
1190 #else /* ! __linux__ && ! __sun__ */
1191 static CharDriverState *qemu_chr_open_pty(void)
1195 #endif /* __linux__ || __sun__ */
1197 #if defined(__linux__)
1201 } ParallelCharDriver;
1203 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1205 if (s->mode != mode) {
1207 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1214 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1216 ParallelCharDriver *drv = chr->opaque;
1221 case CHR_IOCTL_PP_READ_DATA:
1222 if (ioctl(fd, PPRDATA, &b) < 0)
1224 *(uint8_t *)arg = b;
1226 case CHR_IOCTL_PP_WRITE_DATA:
1227 b = *(uint8_t *)arg;
1228 if (ioctl(fd, PPWDATA, &b) < 0)
1231 case CHR_IOCTL_PP_READ_CONTROL:
1232 if (ioctl(fd, PPRCONTROL, &b) < 0)
1234 /* Linux gives only the lowest bits, and no way to know data
1235 direction! For better compatibility set the fixed upper
1237 *(uint8_t *)arg = b | 0xc0;
1239 case CHR_IOCTL_PP_WRITE_CONTROL:
1240 b = *(uint8_t *)arg;
1241 if (ioctl(fd, PPWCONTROL, &b) < 0)
1244 case CHR_IOCTL_PP_READ_STATUS:
1245 if (ioctl(fd, PPRSTATUS, &b) < 0)
1247 *(uint8_t *)arg = b;
1249 case CHR_IOCTL_PP_DATA_DIR:
1250 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1253 case CHR_IOCTL_PP_EPP_READ_ADDR:
1254 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1255 struct ParallelIOArg *parg = arg;
1256 int n = read(fd, parg->buffer, parg->count);
1257 if (n != parg->count) {
1262 case CHR_IOCTL_PP_EPP_READ:
1263 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1264 struct ParallelIOArg *parg = arg;
1265 int n = read(fd, parg->buffer, parg->count);
1266 if (n != parg->count) {
1271 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1272 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1273 struct ParallelIOArg *parg = arg;
1274 int n = write(fd, parg->buffer, parg->count);
1275 if (n != parg->count) {
1280 case CHR_IOCTL_PP_EPP_WRITE:
1281 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1282 struct ParallelIOArg *parg = arg;
1283 int n = write(fd, parg->buffer, parg->count);
1284 if (n != parg->count) {
1295 static void pp_close(CharDriverState *chr)
1297 ParallelCharDriver *drv = chr->opaque;
1300 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1301 ioctl(fd, PPRELEASE);
1304 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1307 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1309 const char *filename = qemu_opt_get(opts, "path");
1310 CharDriverState *chr;
1311 ParallelCharDriver *drv;
1314 TFR(fd = open(filename, O_RDWR));
1318 if (ioctl(fd, PPCLAIM) < 0) {
1323 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1325 drv->mode = IEEE1284_MODE_COMPAT;
1327 chr = qemu_mallocz(sizeof(CharDriverState));
1328 chr->chr_write = null_chr_write;
1329 chr->chr_ioctl = pp_ioctl;
1330 chr->chr_close = pp_close;
1333 qemu_chr_generic_open(chr);
1337 #endif /* __linux__ */
1339 #if defined(__FreeBSD__) || defined(__DragonFly__)
1340 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1342 int fd = (int)chr->opaque;
1346 case CHR_IOCTL_PP_READ_DATA:
1347 if (ioctl(fd, PPIGDATA, &b) < 0)
1349 *(uint8_t *)arg = b;
1351 case CHR_IOCTL_PP_WRITE_DATA:
1352 b = *(uint8_t *)arg;
1353 if (ioctl(fd, PPISDATA, &b) < 0)
1356 case CHR_IOCTL_PP_READ_CONTROL:
1357 if (ioctl(fd, PPIGCTRL, &b) < 0)
1359 *(uint8_t *)arg = b;
1361 case CHR_IOCTL_PP_WRITE_CONTROL:
1362 b = *(uint8_t *)arg;
1363 if (ioctl(fd, PPISCTRL, &b) < 0)
1366 case CHR_IOCTL_PP_READ_STATUS:
1367 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1369 *(uint8_t *)arg = b;
1377 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1379 const char *filename = qemu_opt_get(opts, "path");
1380 CharDriverState *chr;
1383 fd = open(filename, O_RDWR);
1387 chr = qemu_mallocz(sizeof(CharDriverState));
1388 chr->opaque = (void *)fd;
1389 chr->chr_write = null_chr_write;
1390 chr->chr_ioctl = pp_ioctl;
1399 HANDLE hcom, hrecv, hsend;
1400 OVERLAPPED orecv, osend;
1405 #define NSENDBUF 2048
1406 #define NRECVBUF 2048
1407 #define MAXCONNECT 1
1408 #define NTIMEOUT 5000
1410 static int win_chr_poll(void *opaque);
1411 static int win_chr_pipe_poll(void *opaque);
1413 static void win_chr_close(CharDriverState *chr)
1415 WinCharState *s = chr->opaque;
1418 CloseHandle(s->hsend);
1422 CloseHandle(s->hrecv);
1426 CloseHandle(s->hcom);
1430 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1432 qemu_del_polling_cb(win_chr_poll, chr);
1434 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1437 static int win_chr_init(CharDriverState *chr, const char *filename)
1439 WinCharState *s = chr->opaque;
1441 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1446 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1448 fprintf(stderr, "Failed CreateEvent\n");
1451 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1453 fprintf(stderr, "Failed CreateEvent\n");
1457 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1458 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1459 if (s->hcom == INVALID_HANDLE_VALUE) {
1460 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1465 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1466 fprintf(stderr, "Failed SetupComm\n");
1470 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1471 size = sizeof(COMMCONFIG);
1472 GetDefaultCommConfig(filename, &comcfg, &size);
1473 comcfg.dcb.DCBlength = sizeof(DCB);
1474 CommConfigDialog(filename, NULL, &comcfg);
1476 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1477 fprintf(stderr, "Failed SetCommState\n");
1481 if (!SetCommMask(s->hcom, EV_ERR)) {
1482 fprintf(stderr, "Failed SetCommMask\n");
1486 cto.ReadIntervalTimeout = MAXDWORD;
1487 if (!SetCommTimeouts(s->hcom, &cto)) {
1488 fprintf(stderr, "Failed SetCommTimeouts\n");
1492 if (!ClearCommError(s->hcom, &err, &comstat)) {
1493 fprintf(stderr, "Failed ClearCommError\n");
1496 qemu_add_polling_cb(win_chr_poll, chr);
1504 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1506 WinCharState *s = chr->opaque;
1507 DWORD len, ret, size, err;
1510 ZeroMemory(&s->osend, sizeof(s->osend));
1511 s->osend.hEvent = s->hsend;
1514 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1516 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1518 err = GetLastError();
1519 if (err == ERROR_IO_PENDING) {
1520 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1538 static int win_chr_read_poll(CharDriverState *chr)
1540 WinCharState *s = chr->opaque;
1542 s->max_size = qemu_chr_can_read(chr);
1546 static void win_chr_readfile(CharDriverState *chr)
1548 WinCharState *s = chr->opaque;
1550 uint8_t buf[READ_BUF_LEN];
1553 ZeroMemory(&s->orecv, sizeof(s->orecv));
1554 s->orecv.hEvent = s->hrecv;
1555 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1557 err = GetLastError();
1558 if (err == ERROR_IO_PENDING) {
1559 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1564 qemu_chr_read(chr, buf, size);
1568 static void win_chr_read(CharDriverState *chr)
1570 WinCharState *s = chr->opaque;
1572 if (s->len > s->max_size)
1573 s->len = s->max_size;
1577 win_chr_readfile(chr);
1580 static int win_chr_poll(void *opaque)
1582 CharDriverState *chr = opaque;
1583 WinCharState *s = chr->opaque;
1587 ClearCommError(s->hcom, &comerr, &status);
1588 if (status.cbInQue > 0) {
1589 s->len = status.cbInQue;
1590 win_chr_read_poll(chr);
1597 static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1599 const char *filename = qemu_opt_get(opts, "path");
1600 CharDriverState *chr;
1603 chr = qemu_mallocz(sizeof(CharDriverState));
1604 s = qemu_mallocz(sizeof(WinCharState));
1606 chr->chr_write = win_chr_write;
1607 chr->chr_close = win_chr_close;
1609 if (win_chr_init(chr, filename) < 0) {
1614 qemu_chr_generic_open(chr);
1618 static int win_chr_pipe_poll(void *opaque)
1620 CharDriverState *chr = opaque;
1621 WinCharState *s = chr->opaque;
1624 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1627 win_chr_read_poll(chr);
1634 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1636 WinCharState *s = chr->opaque;
1644 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1646 fprintf(stderr, "Failed CreateEvent\n");
1649 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1651 fprintf(stderr, "Failed CreateEvent\n");
1655 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1656 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1657 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1659 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1660 if (s->hcom == INVALID_HANDLE_VALUE) {
1661 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1666 ZeroMemory(&ov, sizeof(ov));
1667 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1668 ret = ConnectNamedPipe(s->hcom, &ov);
1670 fprintf(stderr, "Failed ConnectNamedPipe\n");
1674 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1676 fprintf(stderr, "Failed GetOverlappedResult\n");
1678 CloseHandle(ov.hEvent);
1685 CloseHandle(ov.hEvent);
1688 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1697 static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
1699 const char *filename = qemu_opt_get(opts, "path");
1700 CharDriverState *chr;
1703 chr = qemu_mallocz(sizeof(CharDriverState));
1704 s = qemu_mallocz(sizeof(WinCharState));
1706 chr->chr_write = win_chr_write;
1707 chr->chr_close = win_chr_close;
1709 if (win_chr_pipe_init(chr, filename) < 0) {
1714 qemu_chr_generic_open(chr);
1718 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1720 CharDriverState *chr;
1723 chr = qemu_mallocz(sizeof(CharDriverState));
1724 s = qemu_mallocz(sizeof(WinCharState));
1727 chr->chr_write = win_chr_write;
1728 qemu_chr_generic_open(chr);
1732 static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
1734 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1737 static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
1739 const char *file_out = qemu_opt_get(opts, "path");
1742 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1743 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1744 if (fd_out == INVALID_HANDLE_VALUE)
1747 return qemu_chr_open_win_file(fd_out);
1749 #endif /* !_WIN32 */
1751 /***********************************************************/
1752 /* UDP Net console */
1756 uint8_t buf[READ_BUF_LEN];
1762 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1764 NetCharDriver *s = chr->opaque;
1766 return send(s->fd, (const void *)buf, len, 0);
1769 static int udp_chr_read_poll(void *opaque)
1771 CharDriverState *chr = opaque;
1772 NetCharDriver *s = chr->opaque;
1774 s->max_size = qemu_chr_can_read(chr);
1776 /* If there were any stray characters in the queue process them
1779 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1780 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1782 s->max_size = qemu_chr_can_read(chr);
1787 static void udp_chr_read(void *opaque)
1789 CharDriverState *chr = opaque;
1790 NetCharDriver *s = chr->opaque;
1792 if (s->max_size == 0)
1794 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1795 s->bufptr = s->bufcnt;
1800 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1801 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1803 s->max_size = qemu_chr_can_read(chr);
1807 static void udp_chr_update_read_handler(CharDriverState *chr)
1809 NetCharDriver *s = chr->opaque;
1812 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1813 udp_chr_read, NULL, chr);
1817 static void udp_chr_close(CharDriverState *chr)
1819 NetCharDriver *s = chr->opaque;
1821 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1825 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1828 static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
1830 CharDriverState *chr = NULL;
1831 NetCharDriver *s = NULL;
1834 chr = qemu_mallocz(sizeof(CharDriverState));
1835 s = qemu_mallocz(sizeof(NetCharDriver));
1837 fd = inet_dgram_opts(opts);
1839 fprintf(stderr, "inet_dgram_opts failed\n");
1847 chr->chr_write = udp_chr_write;
1848 chr->chr_update_read_handler = udp_chr_update_read_handler;
1849 chr->chr_close = udp_chr_close;
1862 /***********************************************************/
1863 /* TCP Net console */
1875 static void tcp_chr_accept(void *opaque);
1877 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1879 TCPCharDriver *s = chr->opaque;
1881 return send_all(s->fd, buf, len);
1883 /* XXX: indicate an error ? */
1888 static int tcp_chr_read_poll(void *opaque)
1890 CharDriverState *chr = opaque;
1891 TCPCharDriver *s = chr->opaque;
1894 s->max_size = qemu_chr_can_read(chr);
1899 #define IAC_BREAK 243
1900 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1902 uint8_t *buf, int *size)
1904 /* Handle any telnet client's basic IAC options to satisfy char by
1905 * char mode with no echo. All IAC options will be removed from
1906 * the buf and the do_telnetopt variable will be used to track the
1907 * state of the width of the IAC information.
1909 * IAC commands come in sets of 3 bytes with the exception of the
1910 * "IAC BREAK" command and the double IAC.
1916 for (i = 0; i < *size; i++) {
1917 if (s->do_telnetopt > 1) {
1918 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1919 /* Double IAC means send an IAC */
1923 s->do_telnetopt = 1;
1925 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1926 /* Handle IAC break commands by sending a serial break */
1927 qemu_chr_event(chr, CHR_EVENT_BREAK);
1932 if (s->do_telnetopt >= 4) {
1933 s->do_telnetopt = 1;
1936 if ((unsigned char)buf[i] == IAC) {
1937 s->do_telnetopt = 2;
1948 static int tcp_get_msgfd(CharDriverState *chr)
1950 TCPCharDriver *s = chr->opaque;
1956 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1958 TCPCharDriver *s = chr->opaque;
1959 struct cmsghdr *cmsg;
1961 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1964 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1965 cmsg->cmsg_level != SOL_SOCKET ||
1966 cmsg->cmsg_type != SCM_RIGHTS)
1969 fd = *((int *)CMSG_DATA(cmsg));
1979 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1981 TCPCharDriver *s = chr->opaque;
1982 struct msghdr msg = { NULL, };
1983 struct iovec iov[1];
1985 struct cmsghdr cmsg;
1986 char control[CMSG_SPACE(sizeof(int))];
1990 iov[0].iov_base = buf;
1991 iov[0].iov_len = len;
1995 msg.msg_control = &msg_control;
1996 msg.msg_controllen = sizeof(msg_control);
1998 ret = recvmsg(s->fd, &msg, 0);
1999 if (ret > 0 && s->is_unix)
2000 unix_process_msgfd(chr, &msg);
2005 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2007 TCPCharDriver *s = chr->opaque;
2008 return recv(s->fd, buf, len, 0);
2012 static void tcp_chr_read(void *opaque)
2014 CharDriverState *chr = opaque;
2015 TCPCharDriver *s = chr->opaque;
2016 uint8_t buf[READ_BUF_LEN];
2019 if (!s->connected || s->max_size <= 0)
2022 if (len > s->max_size)
2024 size = tcp_chr_recv(chr, (void *)buf, len);
2026 /* connection closed */
2028 if (s->listen_fd >= 0) {
2029 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2031 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2034 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2035 } else if (size > 0) {
2036 if (s->do_telnetopt)
2037 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2039 qemu_chr_read(chr, buf, size);
2040 if (s->msgfd != -1) {
2047 static void tcp_chr_connect(void *opaque)
2049 CharDriverState *chr = opaque;
2050 TCPCharDriver *s = chr->opaque;
2053 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2054 tcp_chr_read, NULL, chr);
2055 qemu_chr_generic_open(chr);
2058 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2059 static void tcp_chr_telnet_init(int fd)
2062 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2063 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2064 send(fd, (char *)buf, 3, 0);
2065 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2066 send(fd, (char *)buf, 3, 0);
2067 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2068 send(fd, (char *)buf, 3, 0);
2069 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2070 send(fd, (char *)buf, 3, 0);
2073 static void socket_set_nodelay(int fd)
2076 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2079 static void tcp_chr_accept(void *opaque)
2081 CharDriverState *chr = opaque;
2082 TCPCharDriver *s = chr->opaque;
2083 struct sockaddr_in saddr;
2085 struct sockaddr_un uaddr;
2087 struct sockaddr *addr;
2094 len = sizeof(uaddr);
2095 addr = (struct sockaddr *)&uaddr;
2099 len = sizeof(saddr);
2100 addr = (struct sockaddr *)&saddr;
2102 fd = accept(s->listen_fd, addr, &len);
2103 if (fd < 0 && errno != EINTR) {
2105 } else if (fd >= 0) {
2106 if (s->do_telnetopt)
2107 tcp_chr_telnet_init(fd);
2111 socket_set_nonblock(fd);
2113 socket_set_nodelay(fd);
2115 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2116 tcp_chr_connect(chr);
2119 static void tcp_chr_close(CharDriverState *chr)
2121 TCPCharDriver *s = chr->opaque;
2123 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2126 if (s->listen_fd >= 0) {
2127 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2128 closesocket(s->listen_fd);
2131 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2134 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2136 CharDriverState *chr = NULL;
2137 TCPCharDriver *s = NULL;
2145 is_listen = qemu_opt_get_bool(opts, "server", 0);
2146 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2147 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2148 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2149 is_unix = qemu_opt_get(opts, "path") != NULL;
2153 chr = qemu_mallocz(sizeof(CharDriverState));
2154 s = qemu_mallocz(sizeof(TCPCharDriver));
2158 fd = unix_listen_opts(opts);
2160 fd = unix_connect_opts(opts);
2164 fd = inet_listen_opts(opts, 0);
2166 fd = inet_connect_opts(opts);
2172 if (!is_waitconnect)
2173 socket_set_nonblock(fd);
2179 s->is_unix = is_unix;
2180 s->do_nodelay = do_nodelay && !is_unix;
2183 chr->chr_write = tcp_chr_write;
2184 chr->chr_close = tcp_chr_close;
2185 chr->get_msgfd = tcp_get_msgfd;
2189 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2191 s->do_telnetopt = 1;
2196 socket_set_nodelay(fd);
2197 tcp_chr_connect(chr);
2200 /* for "info chardev" monitor command */
2201 chr->filename = qemu_malloc(256);
2203 snprintf(chr->filename, 256, "unix:%s%s",
2204 qemu_opt_get(opts, "path"),
2205 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2206 } else if (is_telnet) {
2207 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2208 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2209 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2211 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2212 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2213 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2216 if (is_listen && is_waitconnect) {
2217 printf("QEMU waiting for connection on: %s\n",
2219 tcp_chr_accept(chr);
2220 socket_set_nonblock(s->listen_fd);
2232 static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2234 char host[65], port[33], width[8], height[8];
2239 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2243 if (strstart(filename, "mon:", &p)) {
2245 qemu_opt_set(opts, "mux", "on");
2248 if (strcmp(filename, "null") == 0 ||
2249 strcmp(filename, "pty") == 0 ||
2250 strcmp(filename, "msmouse") == 0 ||
2251 strcmp(filename, "braille") == 0 ||
2252 strcmp(filename, "stdio") == 0) {
2253 qemu_opt_set(opts, "backend", filename);
2256 if (strstart(filename, "vc", &p)) {
2257 qemu_opt_set(opts, "backend", "vc");
2259 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2261 qemu_opt_set(opts, "width", width);
2262 qemu_opt_set(opts, "height", height);
2263 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2265 qemu_opt_set(opts, "cols", width);
2266 qemu_opt_set(opts, "rows", height);
2273 if (strcmp(filename, "con:") == 0) {
2274 qemu_opt_set(opts, "backend", "console");
2277 if (strstart(filename, "COM", NULL)) {
2278 qemu_opt_set(opts, "backend", "serial");
2279 qemu_opt_set(opts, "path", filename);
2282 if (strstart(filename, "file:", &p)) {
2283 qemu_opt_set(opts, "backend", "file");
2284 qemu_opt_set(opts, "path", p);
2287 if (strstart(filename, "pipe:", &p)) {
2288 qemu_opt_set(opts, "backend", "pipe");
2289 qemu_opt_set(opts, "path", p);
2292 if (strstart(filename, "tcp:", &p) ||
2293 strstart(filename, "telnet:", &p)) {
2294 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2296 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2299 qemu_opt_set(opts, "backend", "socket");
2300 qemu_opt_set(opts, "host", host);
2301 qemu_opt_set(opts, "port", port);
2302 if (p[pos] == ',') {
2303 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2306 if (strstart(filename, "telnet:", &p))
2307 qemu_opt_set(opts, "telnet", "on");
2310 if (strstart(filename, "udp:", &p)) {
2311 qemu_opt_set(opts, "backend", "udp");
2312 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2314 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2315 fprintf(stderr, "udp #1\n");
2319 qemu_opt_set(opts, "host", host);
2320 qemu_opt_set(opts, "port", port);
2321 if (p[pos] == '@') {
2323 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2325 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2326 fprintf(stderr, "udp #2\n");
2330 qemu_opt_set(opts, "localaddr", host);
2331 qemu_opt_set(opts, "localport", port);
2335 if (strstart(filename, "unix:", &p)) {
2336 qemu_opt_set(opts, "backend", "socket");
2337 if (qemu_opts_do_parse(opts, p, "path") != 0)
2341 if (strstart(filename, "/dev/parport", NULL) ||
2342 strstart(filename, "/dev/ppi", NULL)) {
2343 qemu_opt_set(opts, "backend", "parport");
2344 qemu_opt_set(opts, "path", filename);
2347 if (strstart(filename, "/dev/", NULL)) {
2348 qemu_opt_set(opts, "backend", "tty");
2349 qemu_opt_set(opts, "path", filename);
2354 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
2355 qemu_opts_del(opts);
2359 static const struct {
2361 CharDriverState *(*open)(QemuOpts *opts);
2362 } backend_table[] = {
2363 { .name = "null", .open = qemu_chr_open_null },
2364 { .name = "socket", .open = qemu_chr_open_socket },
2365 { .name = "udp", .open = qemu_chr_open_udp },
2366 { .name = "msmouse", .open = qemu_chr_open_msmouse },
2367 { .name = "vc", .open = text_console_init },
2369 { .name = "file", .open = qemu_chr_open_win_file_out },
2370 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2371 { .name = "console", .open = qemu_chr_open_win_con },
2372 { .name = "serial", .open = qemu_chr_open_win },
2374 { .name = "file", .open = qemu_chr_open_file_out },
2375 { .name = "pipe", .open = qemu_chr_open_pipe },
2376 { .name = "pty", .open = qemu_chr_open_pty },
2377 { .name = "stdio", .open = qemu_chr_open_stdio },
2379 #ifdef CONFIG_BRLAPI
2380 { .name = "braille", .open = chr_baum_init },
2382 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2383 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2384 { .name = "tty", .open = qemu_chr_open_tty },
2386 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2387 { .name = "parport", .open = qemu_chr_open_pp },
2391 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2392 void (*init)(struct CharDriverState *s))
2394 CharDriverState *chr;
2397 if (qemu_opts_id(opts) == NULL) {
2398 fprintf(stderr, "chardev: no id specified\n");
2402 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2403 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2406 if (i == ARRAY_SIZE(backend_table)) {
2407 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2408 qemu_opt_get(opts, "backend"));
2412 chr = backend_table[i].open(opts);
2414 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2415 qemu_opt_get(opts, "backend"));
2420 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2422 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2424 if (qemu_opt_get_bool(opts, "mux", 0)) {
2425 CharDriverState *base = chr;
2426 int len = strlen(qemu_opts_id(opts)) + 6;
2427 base->label = qemu_malloc(len);
2428 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2429 chr = qemu_chr_open_mux(base);
2430 chr->filename = base->filename;
2431 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2433 chr->label = qemu_strdup(qemu_opts_id(opts));
2437 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2440 CharDriverState *chr;
2443 if (strstart(filename, "chardev:", &p)) {
2444 return qemu_chr_find(p);
2447 opts = qemu_chr_parse_compat(label, filename);
2451 chr = qemu_chr_open_opts(opts, init);
2452 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2453 monitor_init(chr, MONITOR_USE_READLINE);
2458 void qemu_chr_close(CharDriverState *chr)
2460 QTAILQ_REMOVE(&chardevs, chr, next);
2462 chr->chr_close(chr);
2463 qemu_free(chr->filename);
2464 qemu_free(chr->label);
2468 void qemu_chr_info(Monitor *mon)
2470 CharDriverState *chr;
2472 QTAILQ_FOREACH(chr, &chardevs, next) {
2473 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
2477 CharDriverState *qemu_chr_find(const char *name)
2479 CharDriverState *chr;
2481 QTAILQ_FOREACH(chr, &chardevs, next) {
2482 if (strcmp(chr->label, name) != 0)