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_reset_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_reset(CharDriverState
*s
)
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 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
140 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
142 return s
->chr_write(s
, buf
, len
);
145 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
149 return s
->chr_ioctl(s
, cmd
, arg
);
152 int qemu_chr_can_read(CharDriverState
*s
)
154 if (!s
->chr_can_read
)
156 return s
->chr_can_read(s
->handler_opaque
);
159 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
161 s
->chr_read(s
->handler_opaque
, buf
, len
);
164 int qemu_chr_get_msgfd(CharDriverState
*s
)
166 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
169 void qemu_chr_accept_input(CharDriverState
*s
)
171 if (s
->chr_accept_input
)
172 s
->chr_accept_input(s
);
175 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
177 char buf
[READ_BUF_LEN
];
180 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
181 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
185 void qemu_chr_send_event(CharDriverState
*s
, int event
)
187 if (s
->chr_send_event
)
188 s
->chr_send_event(s
, event
);
191 void qemu_chr_add_handlers(CharDriverState
*s
,
192 IOCanRWHandler
*fd_can_read
,
193 IOReadHandler
*fd_read
,
194 IOEventHandler
*fd_event
,
197 s
->chr_can_read
= fd_can_read
;
198 s
->chr_read
= fd_read
;
199 s
->chr_event
= fd_event
;
200 s
->handler_opaque
= opaque
;
201 if (s
->chr_update_read_handler
)
202 s
->chr_update_read_handler(s
);
205 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
210 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
212 CharDriverState
*chr
;
214 chr
= qemu_mallocz(sizeof(CharDriverState
));
215 chr
->chr_write
= null_chr_write
;
219 /* MUX driver for serial I/O splitting */
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
;
233 /* Intermediate input buffer allows to catch escape sequences even if the
234 currently active device is not accepting any input - but only until it
236 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
241 int64_t timestamps_start
;
245 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
247 MuxDriver
*d
= chr
->opaque
;
249 if (!d
->timestamps
) {
250 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
255 for (i
= 0; i
< len
; i
++) {
261 ti
= qemu_get_clock(rt_clock
);
262 if (d
->timestamps_start
== -1)
263 d
->timestamps_start
= ti
;
264 ti
-= d
->timestamps_start
;
266 snprintf(buf1
, sizeof(buf1
),
267 "[%02d:%02d:%02d.%03d] ",
272 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
275 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
276 if (buf
[i
] == '\n') {
284 static const char * const mux_help
[] = {
285 "% h print this help\n\r",
286 "% x exit emulator\n\r",
287 "% s save disk data back to file (if -snapshot)\n\r",
288 "% t toggle console timestamps\n\r"
289 "% b send break (magic sysrq)\n\r",
290 "% c switch between console and monitor\n\r",
295 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
296 static void mux_print_help(CharDriverState
*chr
)
299 char ebuf
[15] = "Escape-Char";
300 char cbuf
[50] = "\n\r";
302 if (term_escape_char
> 0 && term_escape_char
< 26) {
303 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
304 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
306 snprintf(cbuf
, sizeof(cbuf
),
307 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
310 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
311 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
312 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
313 if (mux_help
[i
][j
] == '%')
314 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
316 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
321 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
323 if (d
->chr_event
[mux_nr
])
324 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
327 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
329 if (d
->term_got_escape
) {
330 d
->term_got_escape
= 0;
331 if (ch
== term_escape_char
)
340 const char *term
= "QEMU: Terminated\n\r";
341 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
348 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
349 bdrv_commit(dinfo
->bdrv
);
354 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
357 /* Switch to the next registered device */
358 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
360 if (d
->focus
>= d
->mux_cnt
)
362 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
365 d
->timestamps
= !d
->timestamps
;
366 d
->timestamps_start
= -1;
370 } else if (ch
== term_escape_char
) {
371 d
->term_got_escape
= 1;
379 static void mux_chr_accept_input(CharDriverState
*chr
)
381 MuxDriver
*d
= chr
->opaque
;
384 while (d
->prod
[m
] != d
->cons
[m
] &&
385 d
->chr_can_read
[m
] &&
386 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
387 d
->chr_read
[m
](d
->ext_opaque
[m
],
388 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
392 static int mux_chr_can_read(void *opaque
)
394 CharDriverState
*chr
= opaque
;
395 MuxDriver
*d
= chr
->opaque
;
398 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
400 if (d
->chr_can_read
[m
])
401 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
405 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
407 CharDriverState
*chr
= opaque
;
408 MuxDriver
*d
= chr
->opaque
;
412 mux_chr_accept_input (opaque
);
414 for(i
= 0; i
< size
; i
++)
415 if (mux_proc_byte(chr
, d
, buf
[i
])) {
416 if (d
->prod
[m
] == d
->cons
[m
] &&
417 d
->chr_can_read
[m
] &&
418 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
419 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
421 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
425 static void mux_chr_event(void *opaque
, int event
)
427 CharDriverState
*chr
= opaque
;
428 MuxDriver
*d
= chr
->opaque
;
431 /* Send the event to all registered listeners */
432 for (i
= 0; i
< d
->mux_cnt
; i
++)
433 mux_chr_send_event(d
, i
, event
);
436 static void mux_chr_update_read_handler(CharDriverState
*chr
)
438 MuxDriver
*d
= chr
->opaque
;
440 if (d
->mux_cnt
>= MAX_MUX
) {
441 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
444 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
445 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
446 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
447 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
448 /* Fix up the real driver with mux routines */
449 if (d
->mux_cnt
== 0) {
450 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
453 if (d
->focus
!= -1) {
454 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
456 d
->focus
= d
->mux_cnt
;
458 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
461 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
463 CharDriverState
*chr
;
466 chr
= qemu_mallocz(sizeof(CharDriverState
));
467 d
= qemu_mallocz(sizeof(MuxDriver
));
472 chr
->chr_write
= mux_chr_write
;
473 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
474 chr
->chr_accept_input
= mux_chr_accept_input
;
480 int send_all(int fd
, const void *buf
, int len1
)
486 ret
= send(fd
, buf
, len
, 0);
488 errno
= WSAGetLastError();
489 if (errno
!= WSAEWOULDBLOCK
) {
492 } else if (ret
== 0) {
504 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
510 ret
= write(fd
, buf
, len
);
512 if (errno
!= EINTR
&& errno
!= EAGAIN
)
514 } else if (ret
== 0) {
524 int send_all(int fd
, const void *buf
, int len1
)
526 return unix_write(fd
, buf
, len1
);
537 #define STDIO_MAX_CLIENTS 1
538 static int stdio_nb_clients
= 0;
540 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
542 FDCharDriver
*s
= chr
->opaque
;
543 return send_all(s
->fd_out
, buf
, len
);
546 static int fd_chr_read_poll(void *opaque
)
548 CharDriverState
*chr
= opaque
;
549 FDCharDriver
*s
= chr
->opaque
;
551 s
->max_size
= qemu_chr_can_read(chr
);
555 static void fd_chr_read(void *opaque
)
557 CharDriverState
*chr
= opaque
;
558 FDCharDriver
*s
= chr
->opaque
;
560 uint8_t buf
[READ_BUF_LEN
];
563 if (len
> s
->max_size
)
567 size
= read(s
->fd_in
, buf
, len
);
569 /* FD has been closed. Remove it from the active list. */
570 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
571 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
575 qemu_chr_read(chr
, buf
, size
);
579 static void fd_chr_update_read_handler(CharDriverState
*chr
)
581 FDCharDriver
*s
= chr
->opaque
;
584 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
586 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
587 fd_chr_read
, NULL
, chr
);
592 static void fd_chr_close(struct CharDriverState
*chr
)
594 FDCharDriver
*s
= chr
->opaque
;
597 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
599 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
604 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
607 /* open a character device to a unix fd */
608 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
610 CharDriverState
*chr
;
613 chr
= qemu_mallocz(sizeof(CharDriverState
));
614 s
= qemu_mallocz(sizeof(FDCharDriver
));
618 chr
->chr_write
= fd_chr_write
;
619 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
620 chr
->chr_close
= fd_chr_close
;
627 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
631 TFR(fd_out
= open(qemu_opt_get(opts
, "path"),
632 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
635 return qemu_chr_open_fd(-1, fd_out
);
638 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
641 char filename_in
[256], filename_out
[256];
642 const char *filename
= qemu_opt_get(opts
, "path");
644 if (filename
== NULL
) {
645 fprintf(stderr
, "chardev: pipe: no filename given\n");
649 snprintf(filename_in
, 256, "%s.in", filename
);
650 snprintf(filename_out
, 256, "%s.out", filename
);
651 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
652 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
653 if (fd_in
< 0 || fd_out
< 0) {
658 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
662 return qemu_chr_open_fd(fd_in
, fd_out
);
666 /* for STDIO, we handle the case where several clients use it
669 #define TERM_FIFO_MAX_SIZE 1
671 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
672 static int term_fifo_size
;
674 static int stdio_read_poll(void *opaque
)
676 CharDriverState
*chr
= opaque
;
678 /* try to flush the queue if needed */
679 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
680 qemu_chr_read(chr
, term_fifo
, 1);
683 /* see if we can absorb more chars */
684 if (term_fifo_size
== 0)
690 static void stdio_read(void *opaque
)
694 CharDriverState
*chr
= opaque
;
696 size
= read(0, buf
, 1);
698 /* stdin has been closed. Remove it from the active list. */
699 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
700 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
704 if (qemu_chr_can_read(chr
) > 0) {
705 qemu_chr_read(chr
, buf
, 1);
706 } else if (term_fifo_size
== 0) {
707 term_fifo
[term_fifo_size
++] = buf
[0];
712 /* init terminal so that we can grab keys */
713 static struct termios oldtty
;
714 static int old_fd0_flags
;
715 static int term_atexit_done
;
717 static void term_exit(void)
719 tcsetattr (0, TCSANOW
, &oldtty
);
720 fcntl(0, F_SETFL
, old_fd0_flags
);
723 static void term_init(QemuOpts
*opts
)
729 old_fd0_flags
= fcntl(0, F_GETFL
);
731 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
732 |INLCR
|IGNCR
|ICRNL
|IXON
);
733 tty
.c_oflag
|= OPOST
;
734 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
735 /* if graphical mode, we allow Ctrl-C handling */
736 if (!qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
))
737 tty
.c_lflag
&= ~ISIG
;
738 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
743 tcsetattr (0, TCSANOW
, &tty
);
745 if (!term_atexit_done
++)
748 fcntl(0, F_SETFL
, O_NONBLOCK
);
751 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
755 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
759 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
761 CharDriverState
*chr
;
763 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
765 chr
= qemu_chr_open_fd(0, 1);
766 chr
->chr_close
= qemu_chr_close_stdio
;
767 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
775 /* Once Solaris has openpty(), this is going to be removed. */
776 static int openpty(int *amaster
, int *aslave
, char *name
,
777 struct termios
*termp
, struct winsize
*winp
)
780 int mfd
= -1, sfd
= -1;
782 *amaster
= *aslave
= -1;
784 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
788 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
791 if ((slave
= ptsname(mfd
)) == NULL
)
794 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
797 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
798 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
806 ioctl(sfd
, TIOCSWINSZ
, winp
);
817 static void cfmakeraw (struct termios
*termios_p
)
819 termios_p
->c_iflag
&=
820 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
821 termios_p
->c_oflag
&= ~OPOST
;
822 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
823 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
824 termios_p
->c_cflag
|= CS8
;
826 termios_p
->c_cc
[VMIN
] = 0;
827 termios_p
->c_cc
[VTIME
] = 0;
831 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
832 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
842 static void pty_chr_update_read_handler(CharDriverState
*chr
);
843 static void pty_chr_state(CharDriverState
*chr
, int connected
);
845 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
847 PtyCharDriver
*s
= chr
->opaque
;
850 /* guest sends data, check for (re-)connect */
851 pty_chr_update_read_handler(chr
);
854 return send_all(s
->fd
, buf
, len
);
857 static int pty_chr_read_poll(void *opaque
)
859 CharDriverState
*chr
= opaque
;
860 PtyCharDriver
*s
= chr
->opaque
;
862 s
->read_bytes
= qemu_chr_can_read(chr
);
863 return s
->read_bytes
;
866 static void pty_chr_read(void *opaque
)
868 CharDriverState
*chr
= opaque
;
869 PtyCharDriver
*s
= chr
->opaque
;
871 uint8_t buf
[READ_BUF_LEN
];
874 if (len
> s
->read_bytes
)
878 size
= read(s
->fd
, buf
, len
);
879 if ((size
== -1 && errno
== EIO
) ||
881 pty_chr_state(chr
, 0);
885 pty_chr_state(chr
, 1);
886 qemu_chr_read(chr
, buf
, size
);
890 static void pty_chr_update_read_handler(CharDriverState
*chr
)
892 PtyCharDriver
*s
= chr
->opaque
;
894 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
895 pty_chr_read
, NULL
, chr
);
898 * Short timeout here: just need wait long enougth that qemu makes
899 * it through the poll loop once. When reconnected we want a
900 * short timeout so we notice it almost instantly. Otherwise
901 * read() gives us -EIO instantly, making pty_chr_state() reset the
902 * timeout to the normal (much longer) poll interval before the
905 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
908 static void pty_chr_state(CharDriverState
*chr
, int connected
)
910 PtyCharDriver
*s
= chr
->opaque
;
913 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
916 /* (re-)connect poll interval for idle guests: once per second.
917 * We check more frequently in case the guests sends data to
918 * the virtual device linked to our pty. */
919 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
927 static void pty_chr_timer(void *opaque
)
929 struct CharDriverState
*chr
= opaque
;
930 PtyCharDriver
*s
= chr
->opaque
;
935 /* If we arrive here without polling being cleared due
936 * read returning -EIO, then we are (re-)connected */
937 pty_chr_state(chr
, 1);
942 pty_chr_update_read_handler(chr
);
945 static void pty_chr_close(struct CharDriverState
*chr
)
947 PtyCharDriver
*s
= chr
->opaque
;
949 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
951 qemu_del_timer(s
->timer
);
952 qemu_free_timer(s
->timer
);
954 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
957 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
959 CharDriverState
*chr
;
963 #if defined(__OpenBSD__) || defined(__DragonFly__)
964 char pty_name
[PATH_MAX
];
965 #define q_ptsname(x) pty_name
967 char *pty_name
= NULL
;
968 #define q_ptsname(x) ptsname(x)
971 chr
= qemu_mallocz(sizeof(CharDriverState
));
972 s
= qemu_mallocz(sizeof(PtyCharDriver
));
974 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
978 /* Set raw attributes on the pty. */
979 tcgetattr(slave_fd
, &tty
);
981 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
984 len
= strlen(q_ptsname(s
->fd
)) + 5;
985 chr
->filename
= qemu_malloc(len
);
986 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
987 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
988 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
991 chr
->chr_write
= pty_chr_write
;
992 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
993 chr
->chr_close
= pty_chr_close
;
995 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1000 static void tty_serial_init(int fd
, int speed
,
1001 int parity
, int data_bits
, int stop_bits
)
1007 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1008 speed
, parity
, data_bits
, stop_bits
);
1010 tcgetattr (fd
, &tty
);
1012 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1013 speed
= speed
* 10 / 11;
1030 /* Non-Posix values follow. They may be unsupported on some systems. */
1032 check_speed(115200);
1034 check_speed(230400);
1037 check_speed(460800);
1040 check_speed(500000);
1043 check_speed(576000);
1046 check_speed(921600);
1049 check_speed(1000000);
1052 check_speed(1152000);
1055 check_speed(1500000);
1058 check_speed(2000000);
1061 check_speed(2500000);
1064 check_speed(3000000);
1067 check_speed(3500000);
1070 check_speed(4000000);
1075 cfsetispeed(&tty
, spd
);
1076 cfsetospeed(&tty
, spd
);
1078 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1079 |INLCR
|IGNCR
|ICRNL
|IXON
);
1080 tty
.c_oflag
|= OPOST
;
1081 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1082 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1103 tty
.c_cflag
|= PARENB
;
1106 tty
.c_cflag
|= PARENB
| PARODD
;
1110 tty
.c_cflag
|= CSTOPB
;
1112 tcsetattr (fd
, TCSANOW
, &tty
);
1115 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1117 FDCharDriver
*s
= chr
->opaque
;
1120 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1122 QEMUSerialSetParams
*ssp
= arg
;
1123 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1124 ssp
->data_bits
, ssp
->stop_bits
);
1127 case CHR_IOCTL_SERIAL_SET_BREAK
:
1129 int enable
= *(int *)arg
;
1131 tcsendbreak(s
->fd_in
, 1);
1134 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1137 int *targ
= (int *)arg
;
1138 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1140 if (sarg
& TIOCM_CTS
)
1141 *targ
|= CHR_TIOCM_CTS
;
1142 if (sarg
& TIOCM_CAR
)
1143 *targ
|= CHR_TIOCM_CAR
;
1144 if (sarg
& TIOCM_DSR
)
1145 *targ
|= CHR_TIOCM_DSR
;
1146 if (sarg
& TIOCM_RI
)
1147 *targ
|= CHR_TIOCM_RI
;
1148 if (sarg
& TIOCM_DTR
)
1149 *targ
|= CHR_TIOCM_DTR
;
1150 if (sarg
& TIOCM_RTS
)
1151 *targ
|= CHR_TIOCM_RTS
;
1154 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1156 int sarg
= *(int *)arg
;
1158 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1159 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1160 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1161 if (sarg
& CHR_TIOCM_CTS
)
1163 if (sarg
& CHR_TIOCM_CAR
)
1165 if (sarg
& CHR_TIOCM_DSR
)
1167 if (sarg
& CHR_TIOCM_RI
)
1169 if (sarg
& CHR_TIOCM_DTR
)
1171 if (sarg
& CHR_TIOCM_RTS
)
1173 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1182 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1184 const char *filename
= qemu_opt_get(opts
, "path");
1185 CharDriverState
*chr
;
1188 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1189 tty_serial_init(fd
, 115200, 'N', 8, 1);
1190 chr
= qemu_chr_open_fd(fd
, fd
);
1195 chr
->chr_ioctl
= tty_serial_ioctl
;
1196 qemu_chr_reset(chr
);
1199 #else /* ! __linux__ && ! __sun__ */
1200 static CharDriverState
*qemu_chr_open_pty(void)
1204 #endif /* __linux__ || __sun__ */
1206 #if defined(__linux__)
1210 } ParallelCharDriver
;
1212 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1214 if (s
->mode
!= mode
) {
1216 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1223 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1225 ParallelCharDriver
*drv
= chr
->opaque
;
1230 case CHR_IOCTL_PP_READ_DATA
:
1231 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1233 *(uint8_t *)arg
= b
;
1235 case CHR_IOCTL_PP_WRITE_DATA
:
1236 b
= *(uint8_t *)arg
;
1237 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1240 case CHR_IOCTL_PP_READ_CONTROL
:
1241 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1243 /* Linux gives only the lowest bits, and no way to know data
1244 direction! For better compatibility set the fixed upper
1246 *(uint8_t *)arg
= b
| 0xc0;
1248 case CHR_IOCTL_PP_WRITE_CONTROL
:
1249 b
= *(uint8_t *)arg
;
1250 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1253 case CHR_IOCTL_PP_READ_STATUS
:
1254 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1256 *(uint8_t *)arg
= b
;
1258 case CHR_IOCTL_PP_DATA_DIR
:
1259 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1262 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1263 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
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_READ
:
1272 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1273 struct ParallelIOArg
*parg
= arg
;
1274 int n
= read(fd
, parg
->buffer
, parg
->count
);
1275 if (n
!= parg
->count
) {
1280 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1281 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1282 struct ParallelIOArg
*parg
= arg
;
1283 int n
= write(fd
, parg
->buffer
, parg
->count
);
1284 if (n
!= parg
->count
) {
1289 case CHR_IOCTL_PP_EPP_WRITE
:
1290 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1291 struct ParallelIOArg
*parg
= arg
;
1292 int n
= write(fd
, parg
->buffer
, parg
->count
);
1293 if (n
!= parg
->count
) {
1304 static void pp_close(CharDriverState
*chr
)
1306 ParallelCharDriver
*drv
= chr
->opaque
;
1309 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1310 ioctl(fd
, PPRELEASE
);
1313 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1316 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1318 const char *filename
= qemu_opt_get(opts
, "path");
1319 CharDriverState
*chr
;
1320 ParallelCharDriver
*drv
;
1323 TFR(fd
= open(filename
, O_RDWR
));
1327 if (ioctl(fd
, PPCLAIM
) < 0) {
1332 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1334 drv
->mode
= IEEE1284_MODE_COMPAT
;
1336 chr
= qemu_mallocz(sizeof(CharDriverState
));
1337 chr
->chr_write
= null_chr_write
;
1338 chr
->chr_ioctl
= pp_ioctl
;
1339 chr
->chr_close
= pp_close
;
1342 qemu_chr_reset(chr
);
1346 #endif /* __linux__ */
1348 #if defined(__FreeBSD__) || defined(__DragonFly__)
1349 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1351 int fd
= (int)chr
->opaque
;
1355 case CHR_IOCTL_PP_READ_DATA
:
1356 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1358 *(uint8_t *)arg
= b
;
1360 case CHR_IOCTL_PP_WRITE_DATA
:
1361 b
= *(uint8_t *)arg
;
1362 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1365 case CHR_IOCTL_PP_READ_CONTROL
:
1366 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1368 *(uint8_t *)arg
= b
;
1370 case CHR_IOCTL_PP_WRITE_CONTROL
:
1371 b
= *(uint8_t *)arg
;
1372 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1375 case CHR_IOCTL_PP_READ_STATUS
:
1376 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1378 *(uint8_t *)arg
= b
;
1386 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1388 const char *filename
= qemu_opt_get(opts
, "path");
1389 CharDriverState
*chr
;
1392 fd
= open(filename
, O_RDWR
);
1396 chr
= qemu_mallocz(sizeof(CharDriverState
));
1397 chr
->opaque
= (void *)fd
;
1398 chr
->chr_write
= null_chr_write
;
1399 chr
->chr_ioctl
= pp_ioctl
;
1408 HANDLE hcom
, hrecv
, hsend
;
1409 OVERLAPPED orecv
, osend
;
1414 #define NSENDBUF 2048
1415 #define NRECVBUF 2048
1416 #define MAXCONNECT 1
1417 #define NTIMEOUT 5000
1419 static int win_chr_poll(void *opaque
);
1420 static int win_chr_pipe_poll(void *opaque
);
1422 static void win_chr_close(CharDriverState
*chr
)
1424 WinCharState
*s
= chr
->opaque
;
1427 CloseHandle(s
->hsend
);
1431 CloseHandle(s
->hrecv
);
1435 CloseHandle(s
->hcom
);
1439 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1441 qemu_del_polling_cb(win_chr_poll
, chr
);
1443 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1446 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1448 WinCharState
*s
= chr
->opaque
;
1450 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1455 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1457 fprintf(stderr
, "Failed CreateEvent\n");
1460 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1462 fprintf(stderr
, "Failed CreateEvent\n");
1466 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1467 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1468 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1469 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1474 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1475 fprintf(stderr
, "Failed SetupComm\n");
1479 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1480 size
= sizeof(COMMCONFIG
);
1481 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1482 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1483 CommConfigDialog(filename
, NULL
, &comcfg
);
1485 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1486 fprintf(stderr
, "Failed SetCommState\n");
1490 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1491 fprintf(stderr
, "Failed SetCommMask\n");
1495 cto
.ReadIntervalTimeout
= MAXDWORD
;
1496 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1497 fprintf(stderr
, "Failed SetCommTimeouts\n");
1501 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1502 fprintf(stderr
, "Failed ClearCommError\n");
1505 qemu_add_polling_cb(win_chr_poll
, chr
);
1513 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1515 WinCharState
*s
= chr
->opaque
;
1516 DWORD len
, ret
, size
, err
;
1519 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1520 s
->osend
.hEvent
= s
->hsend
;
1523 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1525 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1527 err
= GetLastError();
1528 if (err
== ERROR_IO_PENDING
) {
1529 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1547 static int win_chr_read_poll(CharDriverState
*chr
)
1549 WinCharState
*s
= chr
->opaque
;
1551 s
->max_size
= qemu_chr_can_read(chr
);
1555 static void win_chr_readfile(CharDriverState
*chr
)
1557 WinCharState
*s
= chr
->opaque
;
1559 uint8_t buf
[READ_BUF_LEN
];
1562 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1563 s
->orecv
.hEvent
= s
->hrecv
;
1564 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1566 err
= GetLastError();
1567 if (err
== ERROR_IO_PENDING
) {
1568 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1573 qemu_chr_read(chr
, buf
, size
);
1577 static void win_chr_read(CharDriverState
*chr
)
1579 WinCharState
*s
= chr
->opaque
;
1581 if (s
->len
> s
->max_size
)
1582 s
->len
= s
->max_size
;
1586 win_chr_readfile(chr
);
1589 static int win_chr_poll(void *opaque
)
1591 CharDriverState
*chr
= opaque
;
1592 WinCharState
*s
= chr
->opaque
;
1596 ClearCommError(s
->hcom
, &comerr
, &status
);
1597 if (status
.cbInQue
> 0) {
1598 s
->len
= status
.cbInQue
;
1599 win_chr_read_poll(chr
);
1606 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1608 const char *filename
= qemu_opt_get(opts
, "path");
1609 CharDriverState
*chr
;
1612 chr
= qemu_mallocz(sizeof(CharDriverState
));
1613 s
= qemu_mallocz(sizeof(WinCharState
));
1615 chr
->chr_write
= win_chr_write
;
1616 chr
->chr_close
= win_chr_close
;
1618 if (win_chr_init(chr
, filename
) < 0) {
1623 qemu_chr_reset(chr
);
1627 static int win_chr_pipe_poll(void *opaque
)
1629 CharDriverState
*chr
= opaque
;
1630 WinCharState
*s
= chr
->opaque
;
1633 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1636 win_chr_read_poll(chr
);
1643 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1645 WinCharState
*s
= chr
->opaque
;
1653 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1655 fprintf(stderr
, "Failed CreateEvent\n");
1658 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1660 fprintf(stderr
, "Failed CreateEvent\n");
1664 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1665 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1666 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1668 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1669 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1670 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1675 ZeroMemory(&ov
, sizeof(ov
));
1676 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1677 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1679 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1683 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1685 fprintf(stderr
, "Failed GetOverlappedResult\n");
1687 CloseHandle(ov
.hEvent
);
1694 CloseHandle(ov
.hEvent
);
1697 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1706 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1708 const char *filename
= qemu_opt_get(opts
, "path");
1709 CharDriverState
*chr
;
1712 chr
= qemu_mallocz(sizeof(CharDriverState
));
1713 s
= qemu_mallocz(sizeof(WinCharState
));
1715 chr
->chr_write
= win_chr_write
;
1716 chr
->chr_close
= win_chr_close
;
1718 if (win_chr_pipe_init(chr
, filename
) < 0) {
1723 qemu_chr_reset(chr
);
1727 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1729 CharDriverState
*chr
;
1732 chr
= qemu_mallocz(sizeof(CharDriverState
));
1733 s
= qemu_mallocz(sizeof(WinCharState
));
1736 chr
->chr_write
= win_chr_write
;
1737 qemu_chr_reset(chr
);
1741 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1743 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1746 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1748 const char *file_out
= qemu_opt_get(opts
, "path");
1751 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1752 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1753 if (fd_out
== INVALID_HANDLE_VALUE
)
1756 return qemu_chr_open_win_file(fd_out
);
1758 #endif /* !_WIN32 */
1760 /***********************************************************/
1761 /* UDP Net console */
1765 uint8_t buf
[READ_BUF_LEN
];
1771 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1773 NetCharDriver
*s
= chr
->opaque
;
1775 return send(s
->fd
, (const void *)buf
, len
, 0);
1778 static int udp_chr_read_poll(void *opaque
)
1780 CharDriverState
*chr
= opaque
;
1781 NetCharDriver
*s
= chr
->opaque
;
1783 s
->max_size
= qemu_chr_can_read(chr
);
1785 /* If there were any stray characters in the queue process them
1788 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1789 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1791 s
->max_size
= qemu_chr_can_read(chr
);
1796 static void udp_chr_read(void *opaque
)
1798 CharDriverState
*chr
= opaque
;
1799 NetCharDriver
*s
= chr
->opaque
;
1801 if (s
->max_size
== 0)
1803 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1804 s
->bufptr
= s
->bufcnt
;
1809 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1810 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1812 s
->max_size
= qemu_chr_can_read(chr
);
1816 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1818 NetCharDriver
*s
= chr
->opaque
;
1821 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1822 udp_chr_read
, NULL
, chr
);
1826 static void udp_chr_close(CharDriverState
*chr
)
1828 NetCharDriver
*s
= chr
->opaque
;
1830 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1834 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1837 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1839 CharDriverState
*chr
= NULL
;
1840 NetCharDriver
*s
= NULL
;
1843 chr
= qemu_mallocz(sizeof(CharDriverState
));
1844 s
= qemu_mallocz(sizeof(NetCharDriver
));
1846 fd
= inet_dgram_opts(opts
);
1848 fprintf(stderr
, "inet_dgram_opts failed\n");
1856 chr
->chr_write
= udp_chr_write
;
1857 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1858 chr
->chr_close
= udp_chr_close
;
1871 /***********************************************************/
1872 /* TCP Net console */
1884 static void tcp_chr_accept(void *opaque
);
1886 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1888 TCPCharDriver
*s
= chr
->opaque
;
1890 return send_all(s
->fd
, buf
, len
);
1892 /* XXX: indicate an error ? */
1897 static int tcp_chr_read_poll(void *opaque
)
1899 CharDriverState
*chr
= opaque
;
1900 TCPCharDriver
*s
= chr
->opaque
;
1903 s
->max_size
= qemu_chr_can_read(chr
);
1908 #define IAC_BREAK 243
1909 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1911 uint8_t *buf
, int *size
)
1913 /* Handle any telnet client's basic IAC options to satisfy char by
1914 * char mode with no echo. All IAC options will be removed from
1915 * the buf and the do_telnetopt variable will be used to track the
1916 * state of the width of the IAC information.
1918 * IAC commands come in sets of 3 bytes with the exception of the
1919 * "IAC BREAK" command and the double IAC.
1925 for (i
= 0; i
< *size
; i
++) {
1926 if (s
->do_telnetopt
> 1) {
1927 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1928 /* Double IAC means send an IAC */
1932 s
->do_telnetopt
= 1;
1934 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1935 /* Handle IAC break commands by sending a serial break */
1936 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1941 if (s
->do_telnetopt
>= 4) {
1942 s
->do_telnetopt
= 1;
1945 if ((unsigned char)buf
[i
] == IAC
) {
1946 s
->do_telnetopt
= 2;
1957 static int tcp_get_msgfd(CharDriverState
*chr
)
1959 TCPCharDriver
*s
= chr
->opaque
;
1965 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1967 TCPCharDriver
*s
= chr
->opaque
;
1968 struct cmsghdr
*cmsg
;
1970 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1973 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1974 cmsg
->cmsg_level
!= SOL_SOCKET
||
1975 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1978 fd
= *((int *)CMSG_DATA(cmsg
));
1988 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1990 TCPCharDriver
*s
= chr
->opaque
;
1991 struct msghdr msg
= { NULL
, };
1992 struct iovec iov
[1];
1994 struct cmsghdr cmsg
;
1995 char control
[CMSG_SPACE(sizeof(int))];
1999 iov
[0].iov_base
= buf
;
2000 iov
[0].iov_len
= len
;
2004 msg
.msg_control
= &msg_control
;
2005 msg
.msg_controllen
= sizeof(msg_control
);
2007 ret
= recvmsg(s
->fd
, &msg
, 0);
2008 if (ret
> 0 && s
->is_unix
)
2009 unix_process_msgfd(chr
, &msg
);
2014 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2016 TCPCharDriver
*s
= chr
->opaque
;
2017 return recv(s
->fd
, buf
, len
, 0);
2021 static void tcp_chr_read(void *opaque
)
2023 CharDriverState
*chr
= opaque
;
2024 TCPCharDriver
*s
= chr
->opaque
;
2025 uint8_t buf
[READ_BUF_LEN
];
2028 if (!s
->connected
|| s
->max_size
<= 0)
2031 if (len
> s
->max_size
)
2033 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2035 /* connection closed */
2037 if (s
->listen_fd
>= 0) {
2038 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2040 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2043 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2044 } else if (size
> 0) {
2045 if (s
->do_telnetopt
)
2046 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2048 qemu_chr_read(chr
, buf
, size
);
2049 if (s
->msgfd
!= -1) {
2056 static void tcp_chr_connect(void *opaque
)
2058 CharDriverState
*chr
= opaque
;
2059 TCPCharDriver
*s
= chr
->opaque
;
2062 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2063 tcp_chr_read
, NULL
, chr
);
2064 qemu_chr_reset(chr
);
2067 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2068 static void tcp_chr_telnet_init(int fd
)
2071 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2072 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2073 send(fd
, (char *)buf
, 3, 0);
2074 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2075 send(fd
, (char *)buf
, 3, 0);
2076 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2077 send(fd
, (char *)buf
, 3, 0);
2078 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2079 send(fd
, (char *)buf
, 3, 0);
2082 static void socket_set_nodelay(int fd
)
2085 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2088 static void tcp_chr_accept(void *opaque
)
2090 CharDriverState
*chr
= opaque
;
2091 TCPCharDriver
*s
= chr
->opaque
;
2092 struct sockaddr_in saddr
;
2094 struct sockaddr_un uaddr
;
2096 struct sockaddr
*addr
;
2103 len
= sizeof(uaddr
);
2104 addr
= (struct sockaddr
*)&uaddr
;
2108 len
= sizeof(saddr
);
2109 addr
= (struct sockaddr
*)&saddr
;
2111 fd
= accept(s
->listen_fd
, addr
, &len
);
2112 if (fd
< 0 && errno
!= EINTR
) {
2114 } else if (fd
>= 0) {
2115 if (s
->do_telnetopt
)
2116 tcp_chr_telnet_init(fd
);
2120 socket_set_nonblock(fd
);
2122 socket_set_nodelay(fd
);
2124 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2125 tcp_chr_connect(chr
);
2128 static void tcp_chr_close(CharDriverState
*chr
)
2130 TCPCharDriver
*s
= chr
->opaque
;
2132 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2135 if (s
->listen_fd
>= 0) {
2136 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2137 closesocket(s
->listen_fd
);
2140 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2143 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2145 CharDriverState
*chr
= NULL
;
2146 TCPCharDriver
*s
= NULL
;
2154 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2155 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2156 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2157 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2158 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2162 chr
= qemu_mallocz(sizeof(CharDriverState
));
2163 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2167 fd
= unix_listen_opts(opts
);
2169 fd
= unix_connect_opts(opts
);
2173 fd
= inet_listen_opts(opts
, 0);
2175 fd
= inet_connect_opts(opts
);
2181 if (!is_waitconnect
)
2182 socket_set_nonblock(fd
);
2188 s
->is_unix
= is_unix
;
2189 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2192 chr
->chr_write
= tcp_chr_write
;
2193 chr
->chr_close
= tcp_chr_close
;
2194 chr
->get_msgfd
= tcp_get_msgfd
;
2198 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2200 s
->do_telnetopt
= 1;
2205 socket_set_nodelay(fd
);
2206 tcp_chr_connect(chr
);
2209 /* for "info chardev" monitor command */
2210 chr
->filename
= qemu_malloc(256);
2212 snprintf(chr
->filename
, 256, "unix:%s%s",
2213 qemu_opt_get(opts
, "path"),
2214 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2215 } else if (is_telnet
) {
2216 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2217 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2218 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2220 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2221 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2222 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2225 if (is_listen
&& is_waitconnect
) {
2226 printf("QEMU waiting for connection on: %s\n",
2228 tcp_chr_accept(chr
);
2229 socket_set_nonblock(s
->listen_fd
);
2241 static QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2243 char host
[65], port
[33], width
[8], height
[8];
2248 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2252 if (strstart(filename
, "mon:", &p
)) {
2254 qemu_opt_set(opts
, "mux", "on");
2257 if (strcmp(filename
, "null") == 0 ||
2258 strcmp(filename
, "pty") == 0 ||
2259 strcmp(filename
, "msmouse") == 0 ||
2260 strcmp(filename
, "braille") == 0 ||
2261 strcmp(filename
, "stdio") == 0) {
2262 qemu_opt_set(opts
, "backend", filename
);
2265 if (strstart(filename
, "vc", &p
)) {
2266 qemu_opt_set(opts
, "backend", "vc");
2268 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2270 qemu_opt_set(opts
, "width", width
);
2271 qemu_opt_set(opts
, "height", height
);
2272 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2274 qemu_opt_set(opts
, "cols", width
);
2275 qemu_opt_set(opts
, "rows", height
);
2282 if (strcmp(filename
, "con:") == 0) {
2283 qemu_opt_set(opts
, "backend", "console");
2286 if (strstart(filename
, "COM", NULL
)) {
2287 qemu_opt_set(opts
, "backend", "serial");
2288 qemu_opt_set(opts
, "path", filename
);
2291 if (strstart(filename
, "file:", &p
)) {
2292 qemu_opt_set(opts
, "backend", "file");
2293 qemu_opt_set(opts
, "path", p
);
2296 if (strstart(filename
, "pipe:", &p
)) {
2297 qemu_opt_set(opts
, "backend", "pipe");
2298 qemu_opt_set(opts
, "path", p
);
2301 if (strstart(filename
, "tcp:", &p
) ||
2302 strstart(filename
, "telnet:", &p
)) {
2303 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2305 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2308 qemu_opt_set(opts
, "backend", "socket");
2309 qemu_opt_set(opts
, "host", host
);
2310 qemu_opt_set(opts
, "port", port
);
2311 if (p
[pos
] == ',') {
2312 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2315 if (strstart(filename
, "telnet:", &p
))
2316 qemu_opt_set(opts
, "telnet", "on");
2319 if (strstart(filename
, "udp:", &p
)) {
2320 qemu_opt_set(opts
, "backend", "udp");
2321 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2323 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2324 fprintf(stderr
, "udp #1\n");
2328 qemu_opt_set(opts
, "host", host
);
2329 qemu_opt_set(opts
, "port", port
);
2330 if (p
[pos
] == '@') {
2332 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2334 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2335 fprintf(stderr
, "udp #2\n");
2339 qemu_opt_set(opts
, "localaddr", host
);
2340 qemu_opt_set(opts
, "localport", port
);
2344 if (strstart(filename
, "unix:", &p
)) {
2345 qemu_opt_set(opts
, "backend", "socket");
2346 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2350 if (strstart(filename
, "/dev/parport", NULL
) ||
2351 strstart(filename
, "/dev/ppi", NULL
)) {
2352 qemu_opt_set(opts
, "backend", "parport");
2353 qemu_opt_set(opts
, "path", filename
);
2356 if (strstart(filename
, "/dev/", NULL
)) {
2357 qemu_opt_set(opts
, "backend", "tty");
2358 qemu_opt_set(opts
, "path", filename
);
2363 fprintf(stderr
, "%s: fail on \"%s\"\n", __FUNCTION__
, filename
);
2364 qemu_opts_del(opts
);
2368 static const struct {
2370 CharDriverState
*(*open
)(QemuOpts
*opts
);
2371 } backend_table
[] = {
2372 { .name
= "null", .open
= qemu_chr_open_null
},
2373 { .name
= "socket", .open
= qemu_chr_open_socket
},
2374 { .name
= "udp", .open
= qemu_chr_open_udp
},
2375 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2376 { .name
= "vc", .open
= text_console_init
},
2378 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2379 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2380 { .name
= "console", .open
= qemu_chr_open_win_con
},
2381 { .name
= "serial", .open
= qemu_chr_open_win
},
2383 { .name
= "file", .open
= qemu_chr_open_file_out
},
2384 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2385 { .name
= "pty", .open
= qemu_chr_open_pty
},
2386 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2388 #ifdef CONFIG_BRLAPI
2389 { .name
= "braille", .open
= chr_baum_init
},
2391 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2392 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2393 { .name
= "tty", .open
= qemu_chr_open_tty
},
2395 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2396 { .name
= "parport", .open
= qemu_chr_open_pp
},
2400 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2401 void (*init
)(struct CharDriverState
*s
))
2403 CharDriverState
*chr
;
2406 if (qemu_opts_id(opts
) == NULL
) {
2407 fprintf(stderr
, "chardev: no id specified\n");
2411 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2412 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2415 if (i
== ARRAY_SIZE(backend_table
)) {
2416 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2417 qemu_opt_get(opts
, "backend"));
2421 chr
= backend_table
[i
].open(opts
);
2423 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2424 qemu_opt_get(opts
, "backend"));
2429 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2431 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2433 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2434 CharDriverState
*base
= chr
;
2435 int len
= strlen(qemu_opts_id(opts
)) + 6;
2436 base
->label
= qemu_malloc(len
);
2437 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2438 chr
= qemu_chr_open_mux(base
);
2439 chr
->filename
= base
->filename
;
2440 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2442 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2446 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2449 CharDriverState
*chr
;
2452 if (strstart(filename
, "chardev:", &p
)) {
2453 return qemu_chr_find(p
);
2456 opts
= qemu_chr_parse_compat(label
, filename
);
2460 chr
= qemu_chr_open_opts(opts
, init
);
2461 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2462 monitor_init(chr
, MONITOR_USE_READLINE
);
2467 void qemu_chr_close(CharDriverState
*chr
)
2469 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2471 chr
->chr_close(chr
);
2472 qemu_free(chr
->filename
);
2473 qemu_free(chr
->label
);
2477 void qemu_chr_info(Monitor
*mon
)
2479 CharDriverState
*chr
;
2481 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2482 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);
2486 CharDriverState
*qemu_chr_find(const char *name
)
2488 CharDriverState
*chr
;
2490 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2491 if (strcmp(chr
->label
, name
) != 0)