4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
34 #include "hw/msmouse.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
55 #include <net/if_tap.h>
58 #include <linux/if_tun.h>
60 #include <arpa/inet.h>
63 #include <sys/select.h>
68 #include <dev/ppbus/ppi.h>
69 #include <dev/ppbus/ppbconf.h>
70 #elif defined(__DragonFly__)
72 #include <dev/misc/ppi/ppi.h>
73 #include <bus/ppbus/ppbconf.h>
77 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
78 #include <freebsd/stdlib.h>
83 #include <linux/ppdev.h>
84 #include <linux/parport.h>
88 #include <sys/ethernet.h>
89 #include <sys/sockio.h>
90 #include <netinet/arp.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_icmp.h> // must come after ip.h
95 #include <netinet/udp.h>
96 #include <netinet/tcp.h>
104 #include "qemu_socket.h"
106 /***********************************************************/
107 /* character device */
109 static TAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
110 TAILQ_HEAD_INITIALIZER(chardevs
);
111 static int initial_reset_issued
;
113 static void qemu_chr_event(CharDriverState
*s
, int event
)
117 s
->chr_event(s
->handler_opaque
, event
);
120 static void qemu_chr_reset_bh(void *opaque
)
122 CharDriverState
*s
= opaque
;
123 qemu_chr_event(s
, CHR_EVENT_RESET
);
124 qemu_bh_delete(s
->bh
);
128 void qemu_chr_reset(CharDriverState
*s
)
130 if (s
->bh
== NULL
&& initial_reset_issued
) {
131 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
132 qemu_bh_schedule(s
->bh
);
136 void qemu_chr_initial_reset(void)
138 CharDriverState
*chr
;
140 initial_reset_issued
= 1;
142 TAILQ_FOREACH(chr
, &chardevs
, next
) {
147 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
149 return s
->chr_write(s
, buf
, len
);
152 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
156 return s
->chr_ioctl(s
, cmd
, arg
);
159 int qemu_chr_can_read(CharDriverState
*s
)
161 if (!s
->chr_can_read
)
163 return s
->chr_can_read(s
->handler_opaque
);
166 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
168 s
->chr_read(s
->handler_opaque
, buf
, len
);
171 int qemu_chr_get_msgfd(CharDriverState
*s
)
173 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
176 void qemu_chr_accept_input(CharDriverState
*s
)
178 if (s
->chr_accept_input
)
179 s
->chr_accept_input(s
);
182 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
187 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
188 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
192 void qemu_chr_send_event(CharDriverState
*s
, int event
)
194 if (s
->chr_send_event
)
195 s
->chr_send_event(s
, event
);
198 void qemu_chr_add_handlers(CharDriverState
*s
,
199 IOCanRWHandler
*fd_can_read
,
200 IOReadHandler
*fd_read
,
201 IOEventHandler
*fd_event
,
204 s
->chr_can_read
= fd_can_read
;
205 s
->chr_read
= fd_read
;
206 s
->chr_event
= fd_event
;
207 s
->handler_opaque
= opaque
;
208 if (s
->chr_update_read_handler
)
209 s
->chr_update_read_handler(s
);
212 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
217 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
219 CharDriverState
*chr
;
221 chr
= qemu_mallocz(sizeof(CharDriverState
));
222 chr
->chr_write
= null_chr_write
;
226 /* MUX driver for serial I/O splitting */
228 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
229 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
231 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
232 IOReadHandler
*chr_read
[MAX_MUX
];
233 IOEventHandler
*chr_event
[MAX_MUX
];
234 void *ext_opaque
[MAX_MUX
];
235 CharDriverState
*drv
;
239 /* Intermediate input buffer allows to catch escape sequences even if the
240 currently active device is not accepting any input - but only until it
242 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
247 int64_t timestamps_start
;
251 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
253 MuxDriver
*d
= chr
->opaque
;
255 if (!d
->timestamps
) {
256 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
261 for (i
= 0; i
< len
; i
++) {
267 ti
= qemu_get_clock(rt_clock
);
268 if (d
->timestamps_start
== -1)
269 d
->timestamps_start
= ti
;
270 ti
-= d
->timestamps_start
;
272 snprintf(buf1
, sizeof(buf1
),
273 "[%02d:%02d:%02d.%03d] ",
278 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
281 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
282 if (buf
[i
] == '\n') {
290 static const char * const mux_help
[] = {
291 "% h print this help\n\r",
292 "% x exit emulator\n\r",
293 "% s save disk data back to file (if -snapshot)\n\r",
294 "% t toggle console timestamps\n\r"
295 "% b send break (magic sysrq)\n\r",
296 "% c switch between console and monitor\n\r",
301 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
302 static void mux_print_help(CharDriverState
*chr
)
305 char ebuf
[15] = "Escape-Char";
306 char cbuf
[50] = "\n\r";
308 if (term_escape_char
> 0 && term_escape_char
< 26) {
309 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
310 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
312 snprintf(cbuf
, sizeof(cbuf
),
313 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
316 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
317 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
318 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
319 if (mux_help
[i
][j
] == '%')
320 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
322 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
327 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
329 if (d
->chr_event
[mux_nr
])
330 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
333 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
335 if (d
->term_got_escape
) {
336 d
->term_got_escape
= 0;
337 if (ch
== term_escape_char
)
346 const char *term
= "QEMU: Terminated\n\r";
347 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
354 TAILQ_FOREACH(dinfo
, &drives
, next
) {
355 bdrv_commit(dinfo
->bdrv
);
360 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
363 /* Switch to the next registered device */
364 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_OUT
);
366 if (chr
->focus
>= d
->mux_cnt
)
368 mux_chr_send_event(d
, chr
->focus
, CHR_EVENT_MUX_IN
);
371 d
->timestamps
= !d
->timestamps
;
372 d
->timestamps_start
= -1;
376 } else if (ch
== term_escape_char
) {
377 d
->term_got_escape
= 1;
385 static void mux_chr_accept_input(CharDriverState
*chr
)
388 MuxDriver
*d
= chr
->opaque
;
390 while (d
->prod
[m
] != d
->cons
[m
] &&
391 d
->chr_can_read
[m
] &&
392 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
393 d
->chr_read
[m
](d
->ext_opaque
[m
],
394 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
398 static int mux_chr_can_read(void *opaque
)
400 CharDriverState
*chr
= opaque
;
401 MuxDriver
*d
= chr
->opaque
;
404 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
406 if (d
->chr_can_read
[m
])
407 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
411 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
413 CharDriverState
*chr
= opaque
;
414 MuxDriver
*d
= chr
->opaque
;
418 mux_chr_accept_input (opaque
);
420 for(i
= 0; i
< size
; i
++)
421 if (mux_proc_byte(chr
, d
, buf
[i
])) {
422 if (d
->prod
[m
] == d
->cons
[m
] &&
423 d
->chr_can_read
[m
] &&
424 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
425 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
427 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
431 static void mux_chr_event(void *opaque
, int event
)
433 CharDriverState
*chr
= opaque
;
434 MuxDriver
*d
= chr
->opaque
;
437 /* Send the event to all registered listeners */
438 for (i
= 0; i
< d
->mux_cnt
; i
++)
439 mux_chr_send_event(d
, i
, event
);
442 static void mux_chr_update_read_handler(CharDriverState
*chr
)
444 MuxDriver
*d
= chr
->opaque
;
446 if (d
->mux_cnt
>= MAX_MUX
) {
447 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
450 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
451 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
452 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
453 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
454 /* Fix up the real driver with mux routines */
455 if (d
->mux_cnt
== 0) {
456 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
459 chr
->focus
= d
->mux_cnt
;
463 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
465 CharDriverState
*chr
;
468 chr
= qemu_mallocz(sizeof(CharDriverState
));
469 d
= qemu_mallocz(sizeof(MuxDriver
));
474 chr
->chr_write
= mux_chr_write
;
475 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
476 chr
->chr_accept_input
= mux_chr_accept_input
;
482 int send_all(int fd
, const void *buf
, int len1
)
488 ret
= send(fd
, buf
, len
, 0);
490 errno
= WSAGetLastError();
491 if (errno
!= WSAEWOULDBLOCK
) {
494 } else if (ret
== 0) {
506 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
512 ret
= write(fd
, buf
, len
);
514 if (errno
!= EINTR
&& errno
!= EAGAIN
)
516 } else if (ret
== 0) {
526 int send_all(int fd
, const void *buf
, int len1
)
528 return unix_write(fd
, buf
, len1
);
539 #define STDIO_MAX_CLIENTS 1
540 static int stdio_nb_clients
= 0;
542 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
544 FDCharDriver
*s
= chr
->opaque
;
545 return send_all(s
->fd_out
, buf
, len
);
548 static int fd_chr_read_poll(void *opaque
)
550 CharDriverState
*chr
= opaque
;
551 FDCharDriver
*s
= chr
->opaque
;
553 s
->max_size
= qemu_chr_can_read(chr
);
557 static void fd_chr_read(void *opaque
)
559 CharDriverState
*chr
= opaque
;
560 FDCharDriver
*s
= chr
->opaque
;
565 if (len
> s
->max_size
)
569 size
= read(s
->fd_in
, buf
, len
);
571 /* FD has been closed. Remove it from the active list. */
572 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
573 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
577 qemu_chr_read(chr
, buf
, size
);
581 static void fd_chr_update_read_handler(CharDriverState
*chr
)
583 FDCharDriver
*s
= chr
->opaque
;
586 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
588 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
589 fd_chr_read
, NULL
, chr
);
594 static void fd_chr_close(struct CharDriverState
*chr
)
596 FDCharDriver
*s
= chr
->opaque
;
599 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
601 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
606 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
609 /* open a character device to a unix fd */
610 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
612 CharDriverState
*chr
;
615 chr
= qemu_mallocz(sizeof(CharDriverState
));
616 s
= qemu_mallocz(sizeof(FDCharDriver
));
620 chr
->chr_write
= fd_chr_write
;
621 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
622 chr
->chr_close
= fd_chr_close
;
629 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
633 TFR(fd_out
= open(qemu_opt_get(opts
, "path"),
634 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
637 return qemu_chr_open_fd(-1, fd_out
);
640 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
643 char filename_in
[256], filename_out
[256];
644 const char *filename
= qemu_opt_get(opts
, "path");
646 if (filename
== NULL
) {
647 fprintf(stderr
, "chardev: pipe: no filename given\n");
651 snprintf(filename_in
, 256, "%s.in", filename
);
652 snprintf(filename_out
, 256, "%s.out", filename
);
653 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
654 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
655 if (fd_in
< 0 || fd_out
< 0) {
660 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
664 return qemu_chr_open_fd(fd_in
, fd_out
);
668 /* for STDIO, we handle the case where several clients use it
671 #define TERM_FIFO_MAX_SIZE 1
673 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
674 static int term_fifo_size
;
676 static int stdio_read_poll(void *opaque
)
678 CharDriverState
*chr
= opaque
;
680 /* try to flush the queue if needed */
681 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
682 qemu_chr_read(chr
, term_fifo
, 1);
685 /* see if we can absorb more chars */
686 if (term_fifo_size
== 0)
692 static void stdio_read(void *opaque
)
696 CharDriverState
*chr
= opaque
;
698 size
= read(0, buf
, 1);
700 /* stdin has been closed. Remove it from the active list. */
701 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
702 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
706 if (qemu_chr_can_read(chr
) > 0) {
707 qemu_chr_read(chr
, buf
, 1);
708 } else if (term_fifo_size
== 0) {
709 term_fifo
[term_fifo_size
++] = buf
[0];
714 /* init terminal so that we can grab keys */
715 static struct termios oldtty
;
716 static int old_fd0_flags
;
717 static int term_atexit_done
;
719 static void term_exit(void)
721 tcsetattr (0, TCSANOW
, &oldtty
);
722 fcntl(0, F_SETFL
, old_fd0_flags
);
725 static void term_init(void)
731 old_fd0_flags
= fcntl(0, F_GETFL
);
733 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
734 |INLCR
|IGNCR
|ICRNL
|IXON
);
735 tty
.c_oflag
|= OPOST
;
736 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
737 /* if graphical mode, we allow Ctrl-C handling */
738 if (display_type
== DT_NOGRAPHIC
)
739 tty
.c_lflag
&= ~ISIG
;
740 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
745 tcsetattr (0, TCSANOW
, &tty
);
747 if (!term_atexit_done
++)
750 fcntl(0, F_SETFL
, O_NONBLOCK
);
753 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
757 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
761 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
763 CharDriverState
*chr
;
765 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
767 chr
= qemu_chr_open_fd(0, 1);
768 chr
->chr_close
= qemu_chr_close_stdio
;
769 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
777 /* Once Solaris has openpty(), this is going to be removed. */
778 static int openpty(int *amaster
, int *aslave
, char *name
,
779 struct termios
*termp
, struct winsize
*winp
)
782 int mfd
= -1, sfd
= -1;
784 *amaster
= *aslave
= -1;
786 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
790 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
793 if ((slave
= ptsname(mfd
)) == NULL
)
796 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
799 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
800 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
808 ioctl(sfd
, TIOCSWINSZ
, winp
);
819 static void cfmakeraw (struct termios
*termios_p
)
821 termios_p
->c_iflag
&=
822 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
823 termios_p
->c_oflag
&= ~OPOST
;
824 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
825 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
826 termios_p
->c_cflag
|= CS8
;
828 termios_p
->c_cc
[VMIN
] = 0;
829 termios_p
->c_cc
[VTIME
] = 0;
833 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
834 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
844 static void pty_chr_update_read_handler(CharDriverState
*chr
);
845 static void pty_chr_state(CharDriverState
*chr
, int connected
);
847 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
849 PtyCharDriver
*s
= chr
->opaque
;
852 /* guest sends data, check for (re-)connect */
853 pty_chr_update_read_handler(chr
);
856 return send_all(s
->fd
, buf
, len
);
859 static int pty_chr_read_poll(void *opaque
)
861 CharDriverState
*chr
= opaque
;
862 PtyCharDriver
*s
= chr
->opaque
;
864 s
->read_bytes
= qemu_chr_can_read(chr
);
865 return s
->read_bytes
;
868 static void pty_chr_read(void *opaque
)
870 CharDriverState
*chr
= opaque
;
871 PtyCharDriver
*s
= chr
->opaque
;
876 if (len
> s
->read_bytes
)
880 size
= read(s
->fd
, buf
, len
);
881 if ((size
== -1 && errno
== EIO
) ||
883 pty_chr_state(chr
, 0);
887 pty_chr_state(chr
, 1);
888 qemu_chr_read(chr
, buf
, size
);
892 static void pty_chr_update_read_handler(CharDriverState
*chr
)
894 PtyCharDriver
*s
= chr
->opaque
;
896 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
897 pty_chr_read
, NULL
, chr
);
900 * Short timeout here: just need wait long enougth that qemu makes
901 * it through the poll loop once. When reconnected we want a
902 * short timeout so we notice it almost instantly. Otherwise
903 * read() gives us -EIO instantly, making pty_chr_state() reset the
904 * timeout to the normal (much longer) poll interval before the
907 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
910 static void pty_chr_state(CharDriverState
*chr
, int connected
)
912 PtyCharDriver
*s
= chr
->opaque
;
915 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
918 /* (re-)connect poll interval for idle guests: once per second.
919 * We check more frequently in case the guests sends data to
920 * the virtual device linked to our pty. */
921 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
929 static void pty_chr_timer(void *opaque
)
931 struct CharDriverState
*chr
= opaque
;
932 PtyCharDriver
*s
= chr
->opaque
;
937 /* If we arrive here without polling being cleared due
938 * read returning -EIO, then we are (re-)connected */
939 pty_chr_state(chr
, 1);
944 pty_chr_update_read_handler(chr
);
947 static void pty_chr_close(struct CharDriverState
*chr
)
949 PtyCharDriver
*s
= chr
->opaque
;
951 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
953 qemu_del_timer(s
->timer
);
954 qemu_free_timer(s
->timer
);
956 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
959 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
961 CharDriverState
*chr
;
965 #if defined(__OpenBSD__) || defined(__DragonFly__)
966 char pty_name
[PATH_MAX
];
967 #define q_ptsname(x) pty_name
969 char *pty_name
= NULL
;
970 #define q_ptsname(x) ptsname(x)
973 chr
= qemu_mallocz(sizeof(CharDriverState
));
974 s
= qemu_mallocz(sizeof(PtyCharDriver
));
976 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
980 /* Set raw attributes on the pty. */
981 tcgetattr(slave_fd
, &tty
);
983 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
986 len
= strlen(q_ptsname(s
->fd
)) + 5;
987 chr
->filename
= qemu_malloc(len
);
988 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
989 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
990 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
993 chr
->chr_write
= pty_chr_write
;
994 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
995 chr
->chr_close
= pty_chr_close
;
997 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1002 static void tty_serial_init(int fd
, int speed
,
1003 int parity
, int data_bits
, int stop_bits
)
1009 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1010 speed
, parity
, data_bits
, stop_bits
);
1012 tcgetattr (fd
, &tty
);
1015 if (speed
<= 50 * MARGIN
)
1017 else if (speed
<= 75 * MARGIN
)
1019 else if (speed
<= 300 * MARGIN
)
1021 else if (speed
<= 600 * MARGIN
)
1023 else if (speed
<= 1200 * MARGIN
)
1025 else if (speed
<= 2400 * MARGIN
)
1027 else if (speed
<= 4800 * MARGIN
)
1029 else if (speed
<= 9600 * MARGIN
)
1031 else if (speed
<= 19200 * MARGIN
)
1033 else if (speed
<= 38400 * MARGIN
)
1035 else if (speed
<= 57600 * MARGIN
)
1037 else if (speed
<= 115200 * MARGIN
)
1042 cfsetispeed(&tty
, spd
);
1043 cfsetospeed(&tty
, spd
);
1045 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1046 |INLCR
|IGNCR
|ICRNL
|IXON
);
1047 tty
.c_oflag
|= OPOST
;
1048 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1049 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1070 tty
.c_cflag
|= PARENB
;
1073 tty
.c_cflag
|= PARENB
| PARODD
;
1077 tty
.c_cflag
|= CSTOPB
;
1079 tcsetattr (fd
, TCSANOW
, &tty
);
1082 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1084 FDCharDriver
*s
= chr
->opaque
;
1087 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1089 QEMUSerialSetParams
*ssp
= arg
;
1090 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1091 ssp
->data_bits
, ssp
->stop_bits
);
1094 case CHR_IOCTL_SERIAL_SET_BREAK
:
1096 int enable
= *(int *)arg
;
1098 tcsendbreak(s
->fd_in
, 1);
1101 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1104 int *targ
= (int *)arg
;
1105 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1107 if (sarg
& TIOCM_CTS
)
1108 *targ
|= CHR_TIOCM_CTS
;
1109 if (sarg
& TIOCM_CAR
)
1110 *targ
|= CHR_TIOCM_CAR
;
1111 if (sarg
& TIOCM_DSR
)
1112 *targ
|= CHR_TIOCM_DSR
;
1113 if (sarg
& TIOCM_RI
)
1114 *targ
|= CHR_TIOCM_RI
;
1115 if (sarg
& TIOCM_DTR
)
1116 *targ
|= CHR_TIOCM_DTR
;
1117 if (sarg
& TIOCM_RTS
)
1118 *targ
|= CHR_TIOCM_RTS
;
1121 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1123 int sarg
= *(int *)arg
;
1125 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1126 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1127 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1128 if (sarg
& CHR_TIOCM_CTS
)
1130 if (sarg
& CHR_TIOCM_CAR
)
1132 if (sarg
& CHR_TIOCM_DSR
)
1134 if (sarg
& CHR_TIOCM_RI
)
1136 if (sarg
& CHR_TIOCM_DTR
)
1138 if (sarg
& CHR_TIOCM_RTS
)
1140 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1149 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1151 const char *filename
= qemu_opt_get(opts
, "path");
1152 CharDriverState
*chr
;
1155 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1156 tty_serial_init(fd
, 115200, 'N', 8, 1);
1157 chr
= qemu_chr_open_fd(fd
, fd
);
1162 chr
->chr_ioctl
= tty_serial_ioctl
;
1163 qemu_chr_reset(chr
);
1166 #else /* ! __linux__ && ! __sun__ */
1167 static CharDriverState
*qemu_chr_open_pty(void)
1171 #endif /* __linux__ || __sun__ */
1173 #if defined(__linux__)
1177 } ParallelCharDriver
;
1179 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1181 if (s
->mode
!= mode
) {
1183 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1190 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1192 ParallelCharDriver
*drv
= chr
->opaque
;
1197 case CHR_IOCTL_PP_READ_DATA
:
1198 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1200 *(uint8_t *)arg
= b
;
1202 case CHR_IOCTL_PP_WRITE_DATA
:
1203 b
= *(uint8_t *)arg
;
1204 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1207 case CHR_IOCTL_PP_READ_CONTROL
:
1208 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1210 /* Linux gives only the lowest bits, and no way to know data
1211 direction! For better compatibility set the fixed upper
1213 *(uint8_t *)arg
= b
| 0xc0;
1215 case CHR_IOCTL_PP_WRITE_CONTROL
:
1216 b
= *(uint8_t *)arg
;
1217 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1220 case CHR_IOCTL_PP_READ_STATUS
:
1221 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1223 *(uint8_t *)arg
= b
;
1225 case CHR_IOCTL_PP_DATA_DIR
:
1226 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1229 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1230 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1231 struct ParallelIOArg
*parg
= arg
;
1232 int n
= read(fd
, parg
->buffer
, parg
->count
);
1233 if (n
!= parg
->count
) {
1238 case CHR_IOCTL_PP_EPP_READ
:
1239 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1240 struct ParallelIOArg
*parg
= arg
;
1241 int n
= read(fd
, parg
->buffer
, parg
->count
);
1242 if (n
!= parg
->count
) {
1247 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1248 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1249 struct ParallelIOArg
*parg
= arg
;
1250 int n
= write(fd
, parg
->buffer
, parg
->count
);
1251 if (n
!= parg
->count
) {
1256 case CHR_IOCTL_PP_EPP_WRITE
:
1257 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1258 struct ParallelIOArg
*parg
= arg
;
1259 int n
= write(fd
, parg
->buffer
, parg
->count
);
1260 if (n
!= parg
->count
) {
1271 static void pp_close(CharDriverState
*chr
)
1273 ParallelCharDriver
*drv
= chr
->opaque
;
1276 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1277 ioctl(fd
, PPRELEASE
);
1280 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1283 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1285 const char *filename
= qemu_opt_get(opts
, "path");
1286 CharDriverState
*chr
;
1287 ParallelCharDriver
*drv
;
1290 TFR(fd
= open(filename
, O_RDWR
));
1294 if (ioctl(fd
, PPCLAIM
) < 0) {
1299 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1301 drv
->mode
= IEEE1284_MODE_COMPAT
;
1303 chr
= qemu_mallocz(sizeof(CharDriverState
));
1304 chr
->chr_write
= null_chr_write
;
1305 chr
->chr_ioctl
= pp_ioctl
;
1306 chr
->chr_close
= pp_close
;
1309 qemu_chr_reset(chr
);
1313 #endif /* __linux__ */
1315 #if defined(__FreeBSD__) || defined(__DragonFly__)
1316 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1318 int fd
= (int)chr
->opaque
;
1322 case CHR_IOCTL_PP_READ_DATA
:
1323 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1325 *(uint8_t *)arg
= b
;
1327 case CHR_IOCTL_PP_WRITE_DATA
:
1328 b
= *(uint8_t *)arg
;
1329 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1332 case CHR_IOCTL_PP_READ_CONTROL
:
1333 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1335 *(uint8_t *)arg
= b
;
1337 case CHR_IOCTL_PP_WRITE_CONTROL
:
1338 b
= *(uint8_t *)arg
;
1339 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1342 case CHR_IOCTL_PP_READ_STATUS
:
1343 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1345 *(uint8_t *)arg
= b
;
1353 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1355 const char *filename
= qemu_opt_get(opts
, "path");
1356 CharDriverState
*chr
;
1359 fd
= open(filename
, O_RDWR
);
1363 chr
= qemu_mallocz(sizeof(CharDriverState
));
1364 chr
->opaque
= (void *)fd
;
1365 chr
->chr_write
= null_chr_write
;
1366 chr
->chr_ioctl
= pp_ioctl
;
1375 HANDLE hcom
, hrecv
, hsend
;
1376 OVERLAPPED orecv
, osend
;
1381 #define NSENDBUF 2048
1382 #define NRECVBUF 2048
1383 #define MAXCONNECT 1
1384 #define NTIMEOUT 5000
1386 static int win_chr_poll(void *opaque
);
1387 static int win_chr_pipe_poll(void *opaque
);
1389 static void win_chr_close(CharDriverState
*chr
)
1391 WinCharState
*s
= chr
->opaque
;
1394 CloseHandle(s
->hsend
);
1398 CloseHandle(s
->hrecv
);
1402 CloseHandle(s
->hcom
);
1406 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1408 qemu_del_polling_cb(win_chr_poll
, chr
);
1410 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1413 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1415 WinCharState
*s
= chr
->opaque
;
1417 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1422 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1424 fprintf(stderr
, "Failed CreateEvent\n");
1427 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1429 fprintf(stderr
, "Failed CreateEvent\n");
1433 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1434 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1435 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1436 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1441 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1442 fprintf(stderr
, "Failed SetupComm\n");
1446 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1447 size
= sizeof(COMMCONFIG
);
1448 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1449 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1450 CommConfigDialog(filename
, NULL
, &comcfg
);
1452 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1453 fprintf(stderr
, "Failed SetCommState\n");
1457 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1458 fprintf(stderr
, "Failed SetCommMask\n");
1462 cto
.ReadIntervalTimeout
= MAXDWORD
;
1463 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1464 fprintf(stderr
, "Failed SetCommTimeouts\n");
1468 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1469 fprintf(stderr
, "Failed ClearCommError\n");
1472 qemu_add_polling_cb(win_chr_poll
, chr
);
1480 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1482 WinCharState
*s
= chr
->opaque
;
1483 DWORD len
, ret
, size
, err
;
1486 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1487 s
->osend
.hEvent
= s
->hsend
;
1490 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1492 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1494 err
= GetLastError();
1495 if (err
== ERROR_IO_PENDING
) {
1496 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1514 static int win_chr_read_poll(CharDriverState
*chr
)
1516 WinCharState
*s
= chr
->opaque
;
1518 s
->max_size
= qemu_chr_can_read(chr
);
1522 static void win_chr_readfile(CharDriverState
*chr
)
1524 WinCharState
*s
= chr
->opaque
;
1529 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1530 s
->orecv
.hEvent
= s
->hrecv
;
1531 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1533 err
= GetLastError();
1534 if (err
== ERROR_IO_PENDING
) {
1535 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1540 qemu_chr_read(chr
, buf
, size
);
1544 static void win_chr_read(CharDriverState
*chr
)
1546 WinCharState
*s
= chr
->opaque
;
1548 if (s
->len
> s
->max_size
)
1549 s
->len
= s
->max_size
;
1553 win_chr_readfile(chr
);
1556 static int win_chr_poll(void *opaque
)
1558 CharDriverState
*chr
= opaque
;
1559 WinCharState
*s
= chr
->opaque
;
1563 ClearCommError(s
->hcom
, &comerr
, &status
);
1564 if (status
.cbInQue
> 0) {
1565 s
->len
= status
.cbInQue
;
1566 win_chr_read_poll(chr
);
1573 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1575 const char *filename
= qemu_opt_get(opts
, "path");
1576 CharDriverState
*chr
;
1579 chr
= qemu_mallocz(sizeof(CharDriverState
));
1580 s
= qemu_mallocz(sizeof(WinCharState
));
1582 chr
->chr_write
= win_chr_write
;
1583 chr
->chr_close
= win_chr_close
;
1585 if (win_chr_init(chr
, filename
) < 0) {
1590 qemu_chr_reset(chr
);
1594 static int win_chr_pipe_poll(void *opaque
)
1596 CharDriverState
*chr
= opaque
;
1597 WinCharState
*s
= chr
->opaque
;
1600 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1603 win_chr_read_poll(chr
);
1610 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1612 WinCharState
*s
= chr
->opaque
;
1620 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1622 fprintf(stderr
, "Failed CreateEvent\n");
1625 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1627 fprintf(stderr
, "Failed CreateEvent\n");
1631 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1632 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1633 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1635 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1636 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1637 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1642 ZeroMemory(&ov
, sizeof(ov
));
1643 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1644 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1646 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1650 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1652 fprintf(stderr
, "Failed GetOverlappedResult\n");
1654 CloseHandle(ov
.hEvent
);
1661 CloseHandle(ov
.hEvent
);
1664 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1673 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1675 const char *filename
= qemu_opt_get(opts
, "path");
1676 CharDriverState
*chr
;
1679 chr
= qemu_mallocz(sizeof(CharDriverState
));
1680 s
= qemu_mallocz(sizeof(WinCharState
));
1682 chr
->chr_write
= win_chr_write
;
1683 chr
->chr_close
= win_chr_close
;
1685 if (win_chr_pipe_init(chr
, filename
) < 0) {
1690 qemu_chr_reset(chr
);
1694 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1696 CharDriverState
*chr
;
1699 chr
= qemu_mallocz(sizeof(CharDriverState
));
1700 s
= qemu_mallocz(sizeof(WinCharState
));
1703 chr
->chr_write
= win_chr_write
;
1704 qemu_chr_reset(chr
);
1708 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1710 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1713 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1715 const char *file_out
= qemu_opt_get(opts
, "path");
1718 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1719 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1720 if (fd_out
== INVALID_HANDLE_VALUE
)
1723 return qemu_chr_open_win_file(fd_out
);
1725 #endif /* !_WIN32 */
1727 /***********************************************************/
1728 /* UDP Net console */
1732 struct sockaddr_in daddr
;
1739 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1741 NetCharDriver
*s
= chr
->opaque
;
1743 return sendto(s
->fd
, (const void *)buf
, len
, 0,
1744 (struct sockaddr
*)&s
->daddr
, sizeof(struct sockaddr_in
));
1747 static int udp_chr_read_poll(void *opaque
)
1749 CharDriverState
*chr
= opaque
;
1750 NetCharDriver
*s
= chr
->opaque
;
1752 s
->max_size
= qemu_chr_can_read(chr
);
1754 /* If there were any stray characters in the queue process them
1757 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1758 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1760 s
->max_size
= qemu_chr_can_read(chr
);
1765 static void udp_chr_read(void *opaque
)
1767 CharDriverState
*chr
= opaque
;
1768 NetCharDriver
*s
= chr
->opaque
;
1770 if (s
->max_size
== 0)
1772 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1773 s
->bufptr
= s
->bufcnt
;
1778 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1779 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1781 s
->max_size
= qemu_chr_can_read(chr
);
1785 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1787 NetCharDriver
*s
= chr
->opaque
;
1790 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1791 udp_chr_read
, NULL
, chr
);
1795 static void udp_chr_close(CharDriverState
*chr
)
1797 NetCharDriver
*s
= chr
->opaque
;
1799 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1803 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1806 static CharDriverState
*qemu_chr_open_udp(const char *def
)
1808 CharDriverState
*chr
= NULL
;
1809 NetCharDriver
*s
= NULL
;
1811 struct sockaddr_in saddr
;
1813 chr
= qemu_mallocz(sizeof(CharDriverState
));
1814 s
= qemu_mallocz(sizeof(NetCharDriver
));
1816 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1818 perror("socket(PF_INET, SOCK_DGRAM)");
1822 if (parse_host_src_port(&s
->daddr
, &saddr
, def
) < 0) {
1823 printf("Could not parse: %s\n", def
);
1827 if (bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
)) < 0)
1837 chr
->chr_write
= udp_chr_write
;
1838 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1839 chr
->chr_close
= udp_chr_close
;
1852 /***********************************************************/
1853 /* TCP Net console */
1865 static void tcp_chr_accept(void *opaque
);
1867 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1869 TCPCharDriver
*s
= chr
->opaque
;
1871 return send_all(s
->fd
, buf
, len
);
1873 /* XXX: indicate an error ? */
1878 static int tcp_chr_read_poll(void *opaque
)
1880 CharDriverState
*chr
= opaque
;
1881 TCPCharDriver
*s
= chr
->opaque
;
1884 s
->max_size
= qemu_chr_can_read(chr
);
1889 #define IAC_BREAK 243
1890 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1892 uint8_t *buf
, int *size
)
1894 /* Handle any telnet client's basic IAC options to satisfy char by
1895 * char mode with no echo. All IAC options will be removed from
1896 * the buf and the do_telnetopt variable will be used to track the
1897 * state of the width of the IAC information.
1899 * IAC commands come in sets of 3 bytes with the exception of the
1900 * "IAC BREAK" command and the double IAC.
1906 for (i
= 0; i
< *size
; i
++) {
1907 if (s
->do_telnetopt
> 1) {
1908 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1909 /* Double IAC means send an IAC */
1913 s
->do_telnetopt
= 1;
1915 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1916 /* Handle IAC break commands by sending a serial break */
1917 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1922 if (s
->do_telnetopt
>= 4) {
1923 s
->do_telnetopt
= 1;
1926 if ((unsigned char)buf
[i
] == IAC
) {
1927 s
->do_telnetopt
= 2;
1938 static int tcp_get_msgfd(CharDriverState
*chr
)
1940 TCPCharDriver
*s
= chr
->opaque
;
1946 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1948 TCPCharDriver
*s
= chr
->opaque
;
1949 struct cmsghdr
*cmsg
;
1951 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1954 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1955 cmsg
->cmsg_level
!= SOL_SOCKET
||
1956 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1959 fd
= *((int *)CMSG_DATA(cmsg
));
1969 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1971 TCPCharDriver
*s
= chr
->opaque
;
1972 struct msghdr msg
= { NULL
, };
1973 struct iovec iov
[1];
1975 struct cmsghdr cmsg
;
1976 char control
[CMSG_SPACE(sizeof(int))];
1980 iov
[0].iov_base
= buf
;
1981 iov
[0].iov_len
= len
;
1985 msg
.msg_control
= &msg_control
;
1986 msg
.msg_controllen
= sizeof(msg_control
);
1988 ret
= recvmsg(s
->fd
, &msg
, 0);
1989 if (ret
> 0 && s
->is_unix
)
1990 unix_process_msgfd(chr
, &msg
);
1995 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1997 TCPCharDriver
*s
= chr
->opaque
;
1998 return recv(s
->fd
, buf
, len
, 0);
2002 static void tcp_chr_read(void *opaque
)
2004 CharDriverState
*chr
= opaque
;
2005 TCPCharDriver
*s
= chr
->opaque
;
2009 if (!s
->connected
|| s
->max_size
<= 0)
2012 if (len
> s
->max_size
)
2014 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2016 /* connection closed */
2018 if (s
->listen_fd
>= 0) {
2019 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2021 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2024 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2025 } else if (size
> 0) {
2026 if (s
->do_telnetopt
)
2027 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2029 qemu_chr_read(chr
, buf
, size
);
2030 if (s
->msgfd
!= -1) {
2037 static void tcp_chr_connect(void *opaque
)
2039 CharDriverState
*chr
= opaque
;
2040 TCPCharDriver
*s
= chr
->opaque
;
2043 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2044 tcp_chr_read
, NULL
, chr
);
2045 qemu_chr_reset(chr
);
2048 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2049 static void tcp_chr_telnet_init(int fd
)
2052 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2053 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2054 send(fd
, (char *)buf
, 3, 0);
2055 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2056 send(fd
, (char *)buf
, 3, 0);
2057 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2058 send(fd
, (char *)buf
, 3, 0);
2059 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2060 send(fd
, (char *)buf
, 3, 0);
2063 static void socket_set_nodelay(int fd
)
2066 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2069 static void tcp_chr_accept(void *opaque
)
2071 CharDriverState
*chr
= opaque
;
2072 TCPCharDriver
*s
= chr
->opaque
;
2073 struct sockaddr_in saddr
;
2075 struct sockaddr_un uaddr
;
2077 struct sockaddr
*addr
;
2084 len
= sizeof(uaddr
);
2085 addr
= (struct sockaddr
*)&uaddr
;
2089 len
= sizeof(saddr
);
2090 addr
= (struct sockaddr
*)&saddr
;
2092 fd
= accept(s
->listen_fd
, addr
, &len
);
2093 if (fd
< 0 && errno
!= EINTR
) {
2095 } else if (fd
>= 0) {
2096 if (s
->do_telnetopt
)
2097 tcp_chr_telnet_init(fd
);
2101 socket_set_nonblock(fd
);
2103 socket_set_nodelay(fd
);
2105 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2106 tcp_chr_connect(chr
);
2109 static void tcp_chr_close(CharDriverState
*chr
)
2111 TCPCharDriver
*s
= chr
->opaque
;
2113 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2116 if (s
->listen_fd
>= 0) {
2117 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2118 closesocket(s
->listen_fd
);
2121 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2124 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2126 CharDriverState
*chr
= NULL
;
2127 TCPCharDriver
*s
= NULL
;
2135 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2136 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2137 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2138 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2139 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2143 chr
= qemu_mallocz(sizeof(CharDriverState
));
2144 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2148 fd
= unix_listen_opts(opts
);
2150 fd
= unix_connect_opts(opts
);
2154 fd
= inet_listen_opts(opts
, 0);
2156 fd
= inet_connect_opts(opts
);
2162 if (!is_waitconnect
)
2163 socket_set_nonblock(fd
);
2169 s
->is_unix
= is_unix
;
2170 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2173 chr
->chr_write
= tcp_chr_write
;
2174 chr
->chr_close
= tcp_chr_close
;
2175 chr
->get_msgfd
= tcp_get_msgfd
;
2179 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2181 s
->do_telnetopt
= 1;
2186 socket_set_nodelay(fd
);
2187 tcp_chr_connect(chr
);
2190 /* for "info chardev" monitor command */
2191 chr
->filename
= qemu_malloc(256);
2193 snprintf(chr
->filename
, 256, "unix:%s%s",
2194 qemu_opt_get(opts
, "path"),
2195 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2196 } else if (is_telnet
) {
2197 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2198 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2199 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2201 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2202 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2203 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2206 if (is_listen
&& is_waitconnect
) {
2207 printf("QEMU waiting for connection on: %s\n",
2209 tcp_chr_accept(chr
);
2210 socket_set_nonblock(s
->listen_fd
);
2222 static QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2224 char host
[65], port
[33], width
[8], height
[8];
2229 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2233 if (strstart(filename
, "mon:", &p
)) {
2235 qemu_opt_set(opts
, "mux", "on");
2238 if (strcmp(filename
, "null") == 0 ||
2239 strcmp(filename
, "pty") == 0 ||
2240 strcmp(filename
, "msmouse") == 0 ||
2241 strcmp(filename
, "braille") == 0 ||
2242 strcmp(filename
, "stdio") == 0) {
2243 qemu_opt_set(opts
, "backend", filename
);
2246 if (strstart(filename
, "vc", &p
)) {
2247 qemu_opt_set(opts
, "backend", "vc");
2249 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2251 qemu_opt_set(opts
, "width", width
);
2252 qemu_opt_set(opts
, "height", height
);
2253 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2255 qemu_opt_set(opts
, "cols", width
);
2256 qemu_opt_set(opts
, "rows", height
);
2263 if (strcmp(filename
, "con:") == 0) {
2264 qemu_opt_set(opts
, "backend", "console");
2267 if (strstart(filename
, "COM", NULL
)) {
2268 qemu_opt_set(opts
, "backend", "serial");
2269 qemu_opt_set(opts
, "path", filename
);
2272 if (strstart(filename
, "file:", &p
)) {
2273 qemu_opt_set(opts
, "backend", "file");
2274 qemu_opt_set(opts
, "path", p
);
2277 if (strstart(filename
, "pipe:", &p
)) {
2278 qemu_opt_set(opts
, "backend", "pipe");
2279 qemu_opt_set(opts
, "path", p
);
2282 if (strstart(filename
, "tcp:", &p
) ||
2283 strstart(filename
, "telnet:", &p
)) {
2284 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2286 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2289 qemu_opt_set(opts
, "backend", "socket");
2290 qemu_opt_set(opts
, "host", host
);
2291 qemu_opt_set(opts
, "port", port
);
2292 if (p
[pos
] == ',') {
2293 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2296 if (strstart(filename
, "telnet:", &p
))
2297 qemu_opt_set(opts
, "telnet", "on");
2300 if (strstart(filename
, "unix:", &p
)) {
2301 qemu_opt_set(opts
, "backend", "socket");
2302 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2306 if (strstart(filename
, "/dev/parport", NULL
) ||
2307 strstart(filename
, "/dev/ppi", NULL
)) {
2308 qemu_opt_set(opts
, "backend", "parport");
2309 qemu_opt_set(opts
, "path", filename
);
2312 if (strstart(filename
, "/dev/", NULL
)) {
2313 qemu_opt_set(opts
, "backend", "tty");
2314 qemu_opt_set(opts
, "path", filename
);
2319 fprintf(stderr
, "%s: fail on \"%s\"\n", __FUNCTION__
, filename
);
2320 qemu_opts_del(opts
);
2324 static const struct {
2326 CharDriverState
*(*open
)(QemuOpts
*opts
);
2327 } backend_table
[] = {
2328 { .name
= "null", .open
= qemu_chr_open_null
},
2329 { .name
= "socket", .open
= qemu_chr_open_socket
},
2330 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2331 { .name
= "vc", .open
= text_console_init
},
2333 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2334 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2335 { .name
= "console", .open
= qemu_chr_open_win_con
},
2336 { .name
= "serial", .open
= qemu_chr_open_win
},
2338 { .name
= "file", .open
= qemu_chr_open_file_out
},
2339 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2340 { .name
= "pty", .open
= qemu_chr_open_pty
},
2341 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2343 #ifdef CONFIG_BRLAPI
2344 { .name
= "braille", .open
= chr_baum_init
},
2346 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2347 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2348 { .name
= "tty", .open
= qemu_chr_open_tty
},
2350 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2351 { .name
= "parport", .open
= qemu_chr_open_pp
},
2355 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2356 void (*init
)(struct CharDriverState
*s
))
2358 CharDriverState
*chr
;
2361 if (qemu_opts_id(opts
) == NULL
) {
2362 fprintf(stderr
, "chardev: no id specified\n");
2366 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2367 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2370 if (i
== ARRAY_SIZE(backend_table
)) {
2371 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2372 qemu_opt_get(opts
, "backend"));
2376 chr
= backend_table
[i
].open(opts
);
2378 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2379 qemu_opt_get(opts
, "backend"));
2384 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2386 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2388 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2389 CharDriverState
*base
= chr
;
2390 int len
= strlen(qemu_opts_id(opts
)) + 6;
2391 base
->label
= qemu_malloc(len
);
2392 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2393 chr
= qemu_chr_open_mux(base
);
2394 chr
->filename
= base
->filename
;
2395 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2397 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2401 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2404 CharDriverState
*chr
;
2407 opts
= qemu_chr_parse_compat(label
, filename
);
2409 chr
= qemu_chr_open_opts(opts
, init
);
2410 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2411 monitor_init(chr
, MONITOR_USE_READLINE
);
2416 if (strstart(filename
, "udp:", &p
)) {
2417 chr
= qemu_chr_open_udp(p
);
2425 chr
->filename
= qemu_strdup(filename
);
2427 chr
->label
= qemu_strdup(label
);
2428 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2433 void qemu_chr_close(CharDriverState
*chr
)
2435 TAILQ_REMOVE(&chardevs
, chr
, next
);
2437 chr
->chr_close(chr
);
2438 qemu_free(chr
->filename
);
2439 qemu_free(chr
->label
);
2443 void qemu_chr_info(Monitor
*mon
)
2445 CharDriverState
*chr
;
2447 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2448 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);