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
;
240 /* Intermediate input buffer allows to catch escape sequences even if the
241 currently active device is not accepting any input - but only until it
243 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
248 int64_t timestamps_start
;
252 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
254 MuxDriver
*d
= chr
->opaque
;
256 if (!d
->timestamps
) {
257 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
262 for (i
= 0; i
< len
; i
++) {
268 ti
= qemu_get_clock(rt_clock
);
269 if (d
->timestamps_start
== -1)
270 d
->timestamps_start
= ti
;
271 ti
-= d
->timestamps_start
;
273 snprintf(buf1
, sizeof(buf1
),
274 "[%02d:%02d:%02d.%03d] ",
279 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
282 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
283 if (buf
[i
] == '\n') {
291 static const char * const mux_help
[] = {
292 "% h print this help\n\r",
293 "% x exit emulator\n\r",
294 "% s save disk data back to file (if -snapshot)\n\r",
295 "% t toggle console timestamps\n\r"
296 "% b send break (magic sysrq)\n\r",
297 "% c switch between console and monitor\n\r",
302 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
303 static void mux_print_help(CharDriverState
*chr
)
306 char ebuf
[15] = "Escape-Char";
307 char cbuf
[50] = "\n\r";
309 if (term_escape_char
> 0 && term_escape_char
< 26) {
310 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
311 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
313 snprintf(cbuf
, sizeof(cbuf
),
314 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
317 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
318 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
319 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
320 if (mux_help
[i
][j
] == '%')
321 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
323 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
328 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
330 if (d
->chr_event
[mux_nr
])
331 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
334 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
336 if (d
->term_got_escape
) {
337 d
->term_got_escape
= 0;
338 if (ch
== term_escape_char
)
347 const char *term
= "QEMU: Terminated\n\r";
348 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
355 TAILQ_FOREACH(dinfo
, &drives
, next
) {
356 bdrv_commit(dinfo
->bdrv
);
361 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
364 /* Switch to the next registered device */
365 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
367 if (d
->focus
>= d
->mux_cnt
)
369 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
372 d
->timestamps
= !d
->timestamps
;
373 d
->timestamps_start
= -1;
377 } else if (ch
== term_escape_char
) {
378 d
->term_got_escape
= 1;
386 static void mux_chr_accept_input(CharDriverState
*chr
)
388 MuxDriver
*d
= chr
->opaque
;
391 while (d
->prod
[m
] != d
->cons
[m
] &&
392 d
->chr_can_read
[m
] &&
393 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
394 d
->chr_read
[m
](d
->ext_opaque
[m
],
395 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
399 static int mux_chr_can_read(void *opaque
)
401 CharDriverState
*chr
= opaque
;
402 MuxDriver
*d
= chr
->opaque
;
405 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
407 if (d
->chr_can_read
[m
])
408 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
412 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
414 CharDriverState
*chr
= opaque
;
415 MuxDriver
*d
= chr
->opaque
;
419 mux_chr_accept_input (opaque
);
421 for(i
= 0; i
< size
; i
++)
422 if (mux_proc_byte(chr
, d
, buf
[i
])) {
423 if (d
->prod
[m
] == d
->cons
[m
] &&
424 d
->chr_can_read
[m
] &&
425 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
426 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
428 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
432 static void mux_chr_event(void *opaque
, int event
)
434 CharDriverState
*chr
= opaque
;
435 MuxDriver
*d
= chr
->opaque
;
438 /* Send the event to all registered listeners */
439 for (i
= 0; i
< d
->mux_cnt
; i
++)
440 mux_chr_send_event(d
, i
, event
);
443 static void mux_chr_update_read_handler(CharDriverState
*chr
)
445 MuxDriver
*d
= chr
->opaque
;
447 if (d
->mux_cnt
>= MAX_MUX
) {
448 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
451 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
452 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
453 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
454 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
455 /* Fix up the real driver with mux routines */
456 if (d
->mux_cnt
== 0) {
457 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
460 if (d
->focus
!= -1) {
461 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
463 d
->focus
= d
->mux_cnt
;
465 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
468 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
470 CharDriverState
*chr
;
473 chr
= qemu_mallocz(sizeof(CharDriverState
));
474 d
= qemu_mallocz(sizeof(MuxDriver
));
479 chr
->chr_write
= mux_chr_write
;
480 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
481 chr
->chr_accept_input
= mux_chr_accept_input
;
487 int send_all(int fd
, const void *buf
, int len1
)
493 ret
= send(fd
, buf
, len
, 0);
495 errno
= WSAGetLastError();
496 if (errno
!= WSAEWOULDBLOCK
) {
499 } else if (ret
== 0) {
511 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
517 ret
= write(fd
, buf
, len
);
519 if (errno
!= EINTR
&& errno
!= EAGAIN
)
521 } else if (ret
== 0) {
531 int send_all(int fd
, const void *buf
, int len1
)
533 return unix_write(fd
, buf
, len1
);
544 #define STDIO_MAX_CLIENTS 1
545 static int stdio_nb_clients
= 0;
547 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
549 FDCharDriver
*s
= chr
->opaque
;
550 return send_all(s
->fd_out
, buf
, len
);
553 static int fd_chr_read_poll(void *opaque
)
555 CharDriverState
*chr
= opaque
;
556 FDCharDriver
*s
= chr
->opaque
;
558 s
->max_size
= qemu_chr_can_read(chr
);
562 static void fd_chr_read(void *opaque
)
564 CharDriverState
*chr
= opaque
;
565 FDCharDriver
*s
= chr
->opaque
;
570 if (len
> s
->max_size
)
574 size
= read(s
->fd_in
, buf
, len
);
576 /* FD has been closed. Remove it from the active list. */
577 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
578 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
582 qemu_chr_read(chr
, buf
, size
);
586 static void fd_chr_update_read_handler(CharDriverState
*chr
)
588 FDCharDriver
*s
= chr
->opaque
;
591 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
593 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
594 fd_chr_read
, NULL
, chr
);
599 static void fd_chr_close(struct CharDriverState
*chr
)
601 FDCharDriver
*s
= chr
->opaque
;
604 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
606 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
611 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
614 /* open a character device to a unix fd */
615 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
617 CharDriverState
*chr
;
620 chr
= qemu_mallocz(sizeof(CharDriverState
));
621 s
= qemu_mallocz(sizeof(FDCharDriver
));
625 chr
->chr_write
= fd_chr_write
;
626 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
627 chr
->chr_close
= fd_chr_close
;
634 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
638 TFR(fd_out
= open(qemu_opt_get(opts
, "path"),
639 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
642 return qemu_chr_open_fd(-1, fd_out
);
645 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
648 char filename_in
[256], filename_out
[256];
649 const char *filename
= qemu_opt_get(opts
, "path");
651 if (filename
== NULL
) {
652 fprintf(stderr
, "chardev: pipe: no filename given\n");
656 snprintf(filename_in
, 256, "%s.in", filename
);
657 snprintf(filename_out
, 256, "%s.out", filename
);
658 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
659 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
660 if (fd_in
< 0 || fd_out
< 0) {
665 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
669 return qemu_chr_open_fd(fd_in
, fd_out
);
673 /* for STDIO, we handle the case where several clients use it
676 #define TERM_FIFO_MAX_SIZE 1
678 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
679 static int term_fifo_size
;
681 static int stdio_read_poll(void *opaque
)
683 CharDriverState
*chr
= opaque
;
685 /* try to flush the queue if needed */
686 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
687 qemu_chr_read(chr
, term_fifo
, 1);
690 /* see if we can absorb more chars */
691 if (term_fifo_size
== 0)
697 static void stdio_read(void *opaque
)
701 CharDriverState
*chr
= opaque
;
703 size
= read(0, buf
, 1);
705 /* stdin has been closed. Remove it from the active list. */
706 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
707 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
711 if (qemu_chr_can_read(chr
) > 0) {
712 qemu_chr_read(chr
, buf
, 1);
713 } else if (term_fifo_size
== 0) {
714 term_fifo
[term_fifo_size
++] = buf
[0];
719 /* init terminal so that we can grab keys */
720 static struct termios oldtty
;
721 static int old_fd0_flags
;
722 static int term_atexit_done
;
724 static void term_exit(void)
726 tcsetattr (0, TCSANOW
, &oldtty
);
727 fcntl(0, F_SETFL
, old_fd0_flags
);
730 static void term_init(void)
736 old_fd0_flags
= fcntl(0, F_GETFL
);
738 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
739 |INLCR
|IGNCR
|ICRNL
|IXON
);
740 tty
.c_oflag
|= OPOST
;
741 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
742 /* if graphical mode, we allow Ctrl-C handling */
743 if (display_type
== DT_NOGRAPHIC
)
744 tty
.c_lflag
&= ~ISIG
;
745 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
750 tcsetattr (0, TCSANOW
, &tty
);
752 if (!term_atexit_done
++)
755 fcntl(0, F_SETFL
, O_NONBLOCK
);
758 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
762 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
766 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
768 CharDriverState
*chr
;
770 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
772 chr
= qemu_chr_open_fd(0, 1);
773 chr
->chr_close
= qemu_chr_close_stdio
;
774 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
782 /* Once Solaris has openpty(), this is going to be removed. */
783 static int openpty(int *amaster
, int *aslave
, char *name
,
784 struct termios
*termp
, struct winsize
*winp
)
787 int mfd
= -1, sfd
= -1;
789 *amaster
= *aslave
= -1;
791 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
795 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
798 if ((slave
= ptsname(mfd
)) == NULL
)
801 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
804 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
805 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
813 ioctl(sfd
, TIOCSWINSZ
, winp
);
824 static void cfmakeraw (struct termios
*termios_p
)
826 termios_p
->c_iflag
&=
827 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
828 termios_p
->c_oflag
&= ~OPOST
;
829 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
830 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
831 termios_p
->c_cflag
|= CS8
;
833 termios_p
->c_cc
[VMIN
] = 0;
834 termios_p
->c_cc
[VTIME
] = 0;
838 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
839 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
849 static void pty_chr_update_read_handler(CharDriverState
*chr
);
850 static void pty_chr_state(CharDriverState
*chr
, int connected
);
852 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
854 PtyCharDriver
*s
= chr
->opaque
;
857 /* guest sends data, check for (re-)connect */
858 pty_chr_update_read_handler(chr
);
861 return send_all(s
->fd
, buf
, len
);
864 static int pty_chr_read_poll(void *opaque
)
866 CharDriverState
*chr
= opaque
;
867 PtyCharDriver
*s
= chr
->opaque
;
869 s
->read_bytes
= qemu_chr_can_read(chr
);
870 return s
->read_bytes
;
873 static void pty_chr_read(void *opaque
)
875 CharDriverState
*chr
= opaque
;
876 PtyCharDriver
*s
= chr
->opaque
;
881 if (len
> s
->read_bytes
)
885 size
= read(s
->fd
, buf
, len
);
886 if ((size
== -1 && errno
== EIO
) ||
888 pty_chr_state(chr
, 0);
892 pty_chr_state(chr
, 1);
893 qemu_chr_read(chr
, buf
, size
);
897 static void pty_chr_update_read_handler(CharDriverState
*chr
)
899 PtyCharDriver
*s
= chr
->opaque
;
901 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
902 pty_chr_read
, NULL
, chr
);
905 * Short timeout here: just need wait long enougth that qemu makes
906 * it through the poll loop once. When reconnected we want a
907 * short timeout so we notice it almost instantly. Otherwise
908 * read() gives us -EIO instantly, making pty_chr_state() reset the
909 * timeout to the normal (much longer) poll interval before the
912 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
915 static void pty_chr_state(CharDriverState
*chr
, int connected
)
917 PtyCharDriver
*s
= chr
->opaque
;
920 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
923 /* (re-)connect poll interval for idle guests: once per second.
924 * We check more frequently in case the guests sends data to
925 * the virtual device linked to our pty. */
926 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
934 static void pty_chr_timer(void *opaque
)
936 struct CharDriverState
*chr
= opaque
;
937 PtyCharDriver
*s
= chr
->opaque
;
942 /* If we arrive here without polling being cleared due
943 * read returning -EIO, then we are (re-)connected */
944 pty_chr_state(chr
, 1);
949 pty_chr_update_read_handler(chr
);
952 static void pty_chr_close(struct CharDriverState
*chr
)
954 PtyCharDriver
*s
= chr
->opaque
;
956 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
958 qemu_del_timer(s
->timer
);
959 qemu_free_timer(s
->timer
);
961 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
964 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
966 CharDriverState
*chr
;
970 #if defined(__OpenBSD__) || defined(__DragonFly__)
971 char pty_name
[PATH_MAX
];
972 #define q_ptsname(x) pty_name
974 char *pty_name
= NULL
;
975 #define q_ptsname(x) ptsname(x)
978 chr
= qemu_mallocz(sizeof(CharDriverState
));
979 s
= qemu_mallocz(sizeof(PtyCharDriver
));
981 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
985 /* Set raw attributes on the pty. */
986 tcgetattr(slave_fd
, &tty
);
988 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
991 len
= strlen(q_ptsname(s
->fd
)) + 5;
992 chr
->filename
= qemu_malloc(len
);
993 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
994 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
995 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
998 chr
->chr_write
= pty_chr_write
;
999 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1000 chr
->chr_close
= pty_chr_close
;
1002 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1007 static void tty_serial_init(int fd
, int speed
,
1008 int parity
, int data_bits
, int stop_bits
)
1014 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1015 speed
, parity
, data_bits
, stop_bits
);
1017 tcgetattr (fd
, &tty
);
1020 if (speed
<= 50 * MARGIN
)
1022 else if (speed
<= 75 * MARGIN
)
1024 else if (speed
<= 300 * MARGIN
)
1026 else if (speed
<= 600 * MARGIN
)
1028 else if (speed
<= 1200 * MARGIN
)
1030 else if (speed
<= 2400 * MARGIN
)
1032 else if (speed
<= 4800 * MARGIN
)
1034 else if (speed
<= 9600 * MARGIN
)
1036 else if (speed
<= 19200 * MARGIN
)
1038 else if (speed
<= 38400 * MARGIN
)
1040 else if (speed
<= 57600 * MARGIN
)
1042 else if (speed
<= 115200 * MARGIN
)
1047 cfsetispeed(&tty
, spd
);
1048 cfsetospeed(&tty
, spd
);
1050 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1051 |INLCR
|IGNCR
|ICRNL
|IXON
);
1052 tty
.c_oflag
|= OPOST
;
1053 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1054 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1075 tty
.c_cflag
|= PARENB
;
1078 tty
.c_cflag
|= PARENB
| PARODD
;
1082 tty
.c_cflag
|= CSTOPB
;
1084 tcsetattr (fd
, TCSANOW
, &tty
);
1087 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1089 FDCharDriver
*s
= chr
->opaque
;
1092 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1094 QEMUSerialSetParams
*ssp
= arg
;
1095 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1096 ssp
->data_bits
, ssp
->stop_bits
);
1099 case CHR_IOCTL_SERIAL_SET_BREAK
:
1101 int enable
= *(int *)arg
;
1103 tcsendbreak(s
->fd_in
, 1);
1106 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1109 int *targ
= (int *)arg
;
1110 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1112 if (sarg
& TIOCM_CTS
)
1113 *targ
|= CHR_TIOCM_CTS
;
1114 if (sarg
& TIOCM_CAR
)
1115 *targ
|= CHR_TIOCM_CAR
;
1116 if (sarg
& TIOCM_DSR
)
1117 *targ
|= CHR_TIOCM_DSR
;
1118 if (sarg
& TIOCM_RI
)
1119 *targ
|= CHR_TIOCM_RI
;
1120 if (sarg
& TIOCM_DTR
)
1121 *targ
|= CHR_TIOCM_DTR
;
1122 if (sarg
& TIOCM_RTS
)
1123 *targ
|= CHR_TIOCM_RTS
;
1126 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1128 int sarg
= *(int *)arg
;
1130 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1131 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1132 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1133 if (sarg
& CHR_TIOCM_CTS
)
1135 if (sarg
& CHR_TIOCM_CAR
)
1137 if (sarg
& CHR_TIOCM_DSR
)
1139 if (sarg
& CHR_TIOCM_RI
)
1141 if (sarg
& CHR_TIOCM_DTR
)
1143 if (sarg
& CHR_TIOCM_RTS
)
1145 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1154 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1156 const char *filename
= qemu_opt_get(opts
, "path");
1157 CharDriverState
*chr
;
1160 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1161 tty_serial_init(fd
, 115200, 'N', 8, 1);
1162 chr
= qemu_chr_open_fd(fd
, fd
);
1167 chr
->chr_ioctl
= tty_serial_ioctl
;
1168 qemu_chr_reset(chr
);
1171 #else /* ! __linux__ && ! __sun__ */
1172 static CharDriverState
*qemu_chr_open_pty(void)
1176 #endif /* __linux__ || __sun__ */
1178 #if defined(__linux__)
1182 } ParallelCharDriver
;
1184 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1186 if (s
->mode
!= mode
) {
1188 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1195 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1197 ParallelCharDriver
*drv
= chr
->opaque
;
1202 case CHR_IOCTL_PP_READ_DATA
:
1203 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1205 *(uint8_t *)arg
= b
;
1207 case CHR_IOCTL_PP_WRITE_DATA
:
1208 b
= *(uint8_t *)arg
;
1209 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1212 case CHR_IOCTL_PP_READ_CONTROL
:
1213 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1215 /* Linux gives only the lowest bits, and no way to know data
1216 direction! For better compatibility set the fixed upper
1218 *(uint8_t *)arg
= b
| 0xc0;
1220 case CHR_IOCTL_PP_WRITE_CONTROL
:
1221 b
= *(uint8_t *)arg
;
1222 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1225 case CHR_IOCTL_PP_READ_STATUS
:
1226 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1228 *(uint8_t *)arg
= b
;
1230 case CHR_IOCTL_PP_DATA_DIR
:
1231 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1234 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1235 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1236 struct ParallelIOArg
*parg
= arg
;
1237 int n
= read(fd
, parg
->buffer
, parg
->count
);
1238 if (n
!= parg
->count
) {
1243 case CHR_IOCTL_PP_EPP_READ
:
1244 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1245 struct ParallelIOArg
*parg
= arg
;
1246 int n
= read(fd
, parg
->buffer
, parg
->count
);
1247 if (n
!= parg
->count
) {
1252 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1253 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1254 struct ParallelIOArg
*parg
= arg
;
1255 int n
= write(fd
, parg
->buffer
, parg
->count
);
1256 if (n
!= parg
->count
) {
1261 case CHR_IOCTL_PP_EPP_WRITE
:
1262 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1263 struct ParallelIOArg
*parg
= arg
;
1264 int n
= write(fd
, parg
->buffer
, parg
->count
);
1265 if (n
!= parg
->count
) {
1276 static void pp_close(CharDriverState
*chr
)
1278 ParallelCharDriver
*drv
= chr
->opaque
;
1281 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1282 ioctl(fd
, PPRELEASE
);
1285 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1288 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1290 const char *filename
= qemu_opt_get(opts
, "path");
1291 CharDriverState
*chr
;
1292 ParallelCharDriver
*drv
;
1295 TFR(fd
= open(filename
, O_RDWR
));
1299 if (ioctl(fd
, PPCLAIM
) < 0) {
1304 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1306 drv
->mode
= IEEE1284_MODE_COMPAT
;
1308 chr
= qemu_mallocz(sizeof(CharDriverState
));
1309 chr
->chr_write
= null_chr_write
;
1310 chr
->chr_ioctl
= pp_ioctl
;
1311 chr
->chr_close
= pp_close
;
1314 qemu_chr_reset(chr
);
1318 #endif /* __linux__ */
1320 #if defined(__FreeBSD__) || defined(__DragonFly__)
1321 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1323 int fd
= (int)chr
->opaque
;
1327 case CHR_IOCTL_PP_READ_DATA
:
1328 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1330 *(uint8_t *)arg
= b
;
1332 case CHR_IOCTL_PP_WRITE_DATA
:
1333 b
= *(uint8_t *)arg
;
1334 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1337 case CHR_IOCTL_PP_READ_CONTROL
:
1338 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1340 *(uint8_t *)arg
= b
;
1342 case CHR_IOCTL_PP_WRITE_CONTROL
:
1343 b
= *(uint8_t *)arg
;
1344 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1347 case CHR_IOCTL_PP_READ_STATUS
:
1348 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1350 *(uint8_t *)arg
= b
;
1358 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1360 const char *filename
= qemu_opt_get(opts
, "path");
1361 CharDriverState
*chr
;
1364 fd
= open(filename
, O_RDWR
);
1368 chr
= qemu_mallocz(sizeof(CharDriverState
));
1369 chr
->opaque
= (void *)fd
;
1370 chr
->chr_write
= null_chr_write
;
1371 chr
->chr_ioctl
= pp_ioctl
;
1380 HANDLE hcom
, hrecv
, hsend
;
1381 OVERLAPPED orecv
, osend
;
1386 #define NSENDBUF 2048
1387 #define NRECVBUF 2048
1388 #define MAXCONNECT 1
1389 #define NTIMEOUT 5000
1391 static int win_chr_poll(void *opaque
);
1392 static int win_chr_pipe_poll(void *opaque
);
1394 static void win_chr_close(CharDriverState
*chr
)
1396 WinCharState
*s
= chr
->opaque
;
1399 CloseHandle(s
->hsend
);
1403 CloseHandle(s
->hrecv
);
1407 CloseHandle(s
->hcom
);
1411 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1413 qemu_del_polling_cb(win_chr_poll
, chr
);
1415 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1418 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1420 WinCharState
*s
= chr
->opaque
;
1422 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1427 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1429 fprintf(stderr
, "Failed CreateEvent\n");
1432 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1434 fprintf(stderr
, "Failed CreateEvent\n");
1438 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1439 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1440 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1441 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1446 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1447 fprintf(stderr
, "Failed SetupComm\n");
1451 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1452 size
= sizeof(COMMCONFIG
);
1453 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1454 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1455 CommConfigDialog(filename
, NULL
, &comcfg
);
1457 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1458 fprintf(stderr
, "Failed SetCommState\n");
1462 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1463 fprintf(stderr
, "Failed SetCommMask\n");
1467 cto
.ReadIntervalTimeout
= MAXDWORD
;
1468 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1469 fprintf(stderr
, "Failed SetCommTimeouts\n");
1473 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1474 fprintf(stderr
, "Failed ClearCommError\n");
1477 qemu_add_polling_cb(win_chr_poll
, chr
);
1485 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1487 WinCharState
*s
= chr
->opaque
;
1488 DWORD len
, ret
, size
, err
;
1491 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1492 s
->osend
.hEvent
= s
->hsend
;
1495 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1497 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1499 err
= GetLastError();
1500 if (err
== ERROR_IO_PENDING
) {
1501 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1519 static int win_chr_read_poll(CharDriverState
*chr
)
1521 WinCharState
*s
= chr
->opaque
;
1523 s
->max_size
= qemu_chr_can_read(chr
);
1527 static void win_chr_readfile(CharDriverState
*chr
)
1529 WinCharState
*s
= chr
->opaque
;
1534 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1535 s
->orecv
.hEvent
= s
->hrecv
;
1536 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1538 err
= GetLastError();
1539 if (err
== ERROR_IO_PENDING
) {
1540 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1545 qemu_chr_read(chr
, buf
, size
);
1549 static void win_chr_read(CharDriverState
*chr
)
1551 WinCharState
*s
= chr
->opaque
;
1553 if (s
->len
> s
->max_size
)
1554 s
->len
= s
->max_size
;
1558 win_chr_readfile(chr
);
1561 static int win_chr_poll(void *opaque
)
1563 CharDriverState
*chr
= opaque
;
1564 WinCharState
*s
= chr
->opaque
;
1568 ClearCommError(s
->hcom
, &comerr
, &status
);
1569 if (status
.cbInQue
> 0) {
1570 s
->len
= status
.cbInQue
;
1571 win_chr_read_poll(chr
);
1578 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1580 const char *filename
= qemu_opt_get(opts
, "path");
1581 CharDriverState
*chr
;
1584 chr
= qemu_mallocz(sizeof(CharDriverState
));
1585 s
= qemu_mallocz(sizeof(WinCharState
));
1587 chr
->chr_write
= win_chr_write
;
1588 chr
->chr_close
= win_chr_close
;
1590 if (win_chr_init(chr
, filename
) < 0) {
1595 qemu_chr_reset(chr
);
1599 static int win_chr_pipe_poll(void *opaque
)
1601 CharDriverState
*chr
= opaque
;
1602 WinCharState
*s
= chr
->opaque
;
1605 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1608 win_chr_read_poll(chr
);
1615 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1617 WinCharState
*s
= chr
->opaque
;
1625 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1627 fprintf(stderr
, "Failed CreateEvent\n");
1630 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1632 fprintf(stderr
, "Failed CreateEvent\n");
1636 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1637 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1638 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1640 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1641 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1642 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1647 ZeroMemory(&ov
, sizeof(ov
));
1648 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1649 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1651 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1655 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1657 fprintf(stderr
, "Failed GetOverlappedResult\n");
1659 CloseHandle(ov
.hEvent
);
1666 CloseHandle(ov
.hEvent
);
1669 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1678 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1680 const char *filename
= qemu_opt_get(opts
, "path");
1681 CharDriverState
*chr
;
1684 chr
= qemu_mallocz(sizeof(CharDriverState
));
1685 s
= qemu_mallocz(sizeof(WinCharState
));
1687 chr
->chr_write
= win_chr_write
;
1688 chr
->chr_close
= win_chr_close
;
1690 if (win_chr_pipe_init(chr
, filename
) < 0) {
1695 qemu_chr_reset(chr
);
1699 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1701 CharDriverState
*chr
;
1704 chr
= qemu_mallocz(sizeof(CharDriverState
));
1705 s
= qemu_mallocz(sizeof(WinCharState
));
1708 chr
->chr_write
= win_chr_write
;
1709 qemu_chr_reset(chr
);
1713 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1715 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1718 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1720 const char *file_out
= qemu_opt_get(opts
, "path");
1723 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1724 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1725 if (fd_out
== INVALID_HANDLE_VALUE
)
1728 return qemu_chr_open_win_file(fd_out
);
1730 #endif /* !_WIN32 */
1732 /***********************************************************/
1733 /* UDP Net console */
1743 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1745 NetCharDriver
*s
= chr
->opaque
;
1747 return send(s
->fd
, (const void *)buf
, len
, 0);
1750 static int udp_chr_read_poll(void *opaque
)
1752 CharDriverState
*chr
= opaque
;
1753 NetCharDriver
*s
= chr
->opaque
;
1755 s
->max_size
= qemu_chr_can_read(chr
);
1757 /* If there were any stray characters in the queue process them
1760 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1761 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1763 s
->max_size
= qemu_chr_can_read(chr
);
1768 static void udp_chr_read(void *opaque
)
1770 CharDriverState
*chr
= opaque
;
1771 NetCharDriver
*s
= chr
->opaque
;
1773 if (s
->max_size
== 0)
1775 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1776 s
->bufptr
= s
->bufcnt
;
1781 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1782 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1784 s
->max_size
= qemu_chr_can_read(chr
);
1788 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1790 NetCharDriver
*s
= chr
->opaque
;
1793 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1794 udp_chr_read
, NULL
, chr
);
1798 static void udp_chr_close(CharDriverState
*chr
)
1800 NetCharDriver
*s
= chr
->opaque
;
1802 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1806 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1809 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1811 CharDriverState
*chr
= NULL
;
1812 NetCharDriver
*s
= NULL
;
1815 chr
= qemu_mallocz(sizeof(CharDriverState
));
1816 s
= qemu_mallocz(sizeof(NetCharDriver
));
1818 fd
= inet_dgram_opts(opts
);
1820 fprintf(stderr
, "inet_dgram_opts failed\n");
1828 chr
->chr_write
= udp_chr_write
;
1829 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1830 chr
->chr_close
= udp_chr_close
;
1843 /***********************************************************/
1844 /* TCP Net console */
1856 static void tcp_chr_accept(void *opaque
);
1858 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1860 TCPCharDriver
*s
= chr
->opaque
;
1862 return send_all(s
->fd
, buf
, len
);
1864 /* XXX: indicate an error ? */
1869 static int tcp_chr_read_poll(void *opaque
)
1871 CharDriverState
*chr
= opaque
;
1872 TCPCharDriver
*s
= chr
->opaque
;
1875 s
->max_size
= qemu_chr_can_read(chr
);
1880 #define IAC_BREAK 243
1881 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1883 uint8_t *buf
, int *size
)
1885 /* Handle any telnet client's basic IAC options to satisfy char by
1886 * char mode with no echo. All IAC options will be removed from
1887 * the buf and the do_telnetopt variable will be used to track the
1888 * state of the width of the IAC information.
1890 * IAC commands come in sets of 3 bytes with the exception of the
1891 * "IAC BREAK" command and the double IAC.
1897 for (i
= 0; i
< *size
; i
++) {
1898 if (s
->do_telnetopt
> 1) {
1899 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1900 /* Double IAC means send an IAC */
1904 s
->do_telnetopt
= 1;
1906 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1907 /* Handle IAC break commands by sending a serial break */
1908 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1913 if (s
->do_telnetopt
>= 4) {
1914 s
->do_telnetopt
= 1;
1917 if ((unsigned char)buf
[i
] == IAC
) {
1918 s
->do_telnetopt
= 2;
1929 static int tcp_get_msgfd(CharDriverState
*chr
)
1931 TCPCharDriver
*s
= chr
->opaque
;
1937 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1939 TCPCharDriver
*s
= chr
->opaque
;
1940 struct cmsghdr
*cmsg
;
1942 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1945 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1946 cmsg
->cmsg_level
!= SOL_SOCKET
||
1947 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1950 fd
= *((int *)CMSG_DATA(cmsg
));
1960 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1962 TCPCharDriver
*s
= chr
->opaque
;
1963 struct msghdr msg
= { NULL
, };
1964 struct iovec iov
[1];
1966 struct cmsghdr cmsg
;
1967 char control
[CMSG_SPACE(sizeof(int))];
1971 iov
[0].iov_base
= buf
;
1972 iov
[0].iov_len
= len
;
1976 msg
.msg_control
= &msg_control
;
1977 msg
.msg_controllen
= sizeof(msg_control
);
1979 ret
= recvmsg(s
->fd
, &msg
, 0);
1980 if (ret
> 0 && s
->is_unix
)
1981 unix_process_msgfd(chr
, &msg
);
1986 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1988 TCPCharDriver
*s
= chr
->opaque
;
1989 return recv(s
->fd
, buf
, len
, 0);
1993 static void tcp_chr_read(void *opaque
)
1995 CharDriverState
*chr
= opaque
;
1996 TCPCharDriver
*s
= chr
->opaque
;
2000 if (!s
->connected
|| s
->max_size
<= 0)
2003 if (len
> s
->max_size
)
2005 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2007 /* connection closed */
2009 if (s
->listen_fd
>= 0) {
2010 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2012 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2015 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2016 } else if (size
> 0) {
2017 if (s
->do_telnetopt
)
2018 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2020 qemu_chr_read(chr
, buf
, size
);
2021 if (s
->msgfd
!= -1) {
2028 static void tcp_chr_connect(void *opaque
)
2030 CharDriverState
*chr
= opaque
;
2031 TCPCharDriver
*s
= chr
->opaque
;
2034 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2035 tcp_chr_read
, NULL
, chr
);
2036 qemu_chr_reset(chr
);
2039 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2040 static void tcp_chr_telnet_init(int fd
)
2043 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2044 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2045 send(fd
, (char *)buf
, 3, 0);
2046 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2047 send(fd
, (char *)buf
, 3, 0);
2048 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2049 send(fd
, (char *)buf
, 3, 0);
2050 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2051 send(fd
, (char *)buf
, 3, 0);
2054 static void socket_set_nodelay(int fd
)
2057 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2060 static void tcp_chr_accept(void *opaque
)
2062 CharDriverState
*chr
= opaque
;
2063 TCPCharDriver
*s
= chr
->opaque
;
2064 struct sockaddr_in saddr
;
2066 struct sockaddr_un uaddr
;
2068 struct sockaddr
*addr
;
2075 len
= sizeof(uaddr
);
2076 addr
= (struct sockaddr
*)&uaddr
;
2080 len
= sizeof(saddr
);
2081 addr
= (struct sockaddr
*)&saddr
;
2083 fd
= accept(s
->listen_fd
, addr
, &len
);
2084 if (fd
< 0 && errno
!= EINTR
) {
2086 } else if (fd
>= 0) {
2087 if (s
->do_telnetopt
)
2088 tcp_chr_telnet_init(fd
);
2092 socket_set_nonblock(fd
);
2094 socket_set_nodelay(fd
);
2096 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2097 tcp_chr_connect(chr
);
2100 static void tcp_chr_close(CharDriverState
*chr
)
2102 TCPCharDriver
*s
= chr
->opaque
;
2104 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2107 if (s
->listen_fd
>= 0) {
2108 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2109 closesocket(s
->listen_fd
);
2112 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2115 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2117 CharDriverState
*chr
= NULL
;
2118 TCPCharDriver
*s
= NULL
;
2126 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2127 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2128 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2129 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2130 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2134 chr
= qemu_mallocz(sizeof(CharDriverState
));
2135 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2139 fd
= unix_listen_opts(opts
);
2141 fd
= unix_connect_opts(opts
);
2145 fd
= inet_listen_opts(opts
, 0);
2147 fd
= inet_connect_opts(opts
);
2153 if (!is_waitconnect
)
2154 socket_set_nonblock(fd
);
2160 s
->is_unix
= is_unix
;
2161 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2164 chr
->chr_write
= tcp_chr_write
;
2165 chr
->chr_close
= tcp_chr_close
;
2166 chr
->get_msgfd
= tcp_get_msgfd
;
2170 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2172 s
->do_telnetopt
= 1;
2177 socket_set_nodelay(fd
);
2178 tcp_chr_connect(chr
);
2181 /* for "info chardev" monitor command */
2182 chr
->filename
= qemu_malloc(256);
2184 snprintf(chr
->filename
, 256, "unix:%s%s",
2185 qemu_opt_get(opts
, "path"),
2186 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2187 } else if (is_telnet
) {
2188 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2189 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2190 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2192 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2193 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2194 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2197 if (is_listen
&& is_waitconnect
) {
2198 printf("QEMU waiting for connection on: %s\n",
2200 tcp_chr_accept(chr
);
2201 socket_set_nonblock(s
->listen_fd
);
2213 static QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2215 char host
[65], port
[33], width
[8], height
[8];
2220 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2224 if (strstart(filename
, "mon:", &p
)) {
2226 qemu_opt_set(opts
, "mux", "on");
2229 if (strcmp(filename
, "null") == 0 ||
2230 strcmp(filename
, "pty") == 0 ||
2231 strcmp(filename
, "msmouse") == 0 ||
2232 strcmp(filename
, "braille") == 0 ||
2233 strcmp(filename
, "stdio") == 0) {
2234 qemu_opt_set(opts
, "backend", filename
);
2237 if (strstart(filename
, "vc", &p
)) {
2238 qemu_opt_set(opts
, "backend", "vc");
2240 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2242 qemu_opt_set(opts
, "width", width
);
2243 qemu_opt_set(opts
, "height", height
);
2244 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2246 qemu_opt_set(opts
, "cols", width
);
2247 qemu_opt_set(opts
, "rows", height
);
2254 if (strcmp(filename
, "con:") == 0) {
2255 qemu_opt_set(opts
, "backend", "console");
2258 if (strstart(filename
, "COM", NULL
)) {
2259 qemu_opt_set(opts
, "backend", "serial");
2260 qemu_opt_set(opts
, "path", filename
);
2263 if (strstart(filename
, "file:", &p
)) {
2264 qemu_opt_set(opts
, "backend", "file");
2265 qemu_opt_set(opts
, "path", p
);
2268 if (strstart(filename
, "pipe:", &p
)) {
2269 qemu_opt_set(opts
, "backend", "pipe");
2270 qemu_opt_set(opts
, "path", p
);
2273 if (strstart(filename
, "tcp:", &p
) ||
2274 strstart(filename
, "telnet:", &p
)) {
2275 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2277 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2280 qemu_opt_set(opts
, "backend", "socket");
2281 qemu_opt_set(opts
, "host", host
);
2282 qemu_opt_set(opts
, "port", port
);
2283 if (p
[pos
] == ',') {
2284 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2287 if (strstart(filename
, "telnet:", &p
))
2288 qemu_opt_set(opts
, "telnet", "on");
2291 if (strstart(filename
, "udp:", &p
)) {
2292 qemu_opt_set(opts
, "backend", "udp");
2293 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2295 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2296 fprintf(stderr
, "udp #1\n");
2300 qemu_opt_set(opts
, "host", host
);
2301 qemu_opt_set(opts
, "port", port
);
2302 if (p
[pos
] == '@') {
2304 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2306 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2307 fprintf(stderr
, "udp #2\n");
2311 qemu_opt_set(opts
, "localaddr", host
);
2312 qemu_opt_set(opts
, "localport", port
);
2316 if (strstart(filename
, "unix:", &p
)) {
2317 qemu_opt_set(opts
, "backend", "socket");
2318 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2322 if (strstart(filename
, "/dev/parport", NULL
) ||
2323 strstart(filename
, "/dev/ppi", NULL
)) {
2324 qemu_opt_set(opts
, "backend", "parport");
2325 qemu_opt_set(opts
, "path", filename
);
2328 if (strstart(filename
, "/dev/", NULL
)) {
2329 qemu_opt_set(opts
, "backend", "tty");
2330 qemu_opt_set(opts
, "path", filename
);
2335 fprintf(stderr
, "%s: fail on \"%s\"\n", __FUNCTION__
, filename
);
2336 qemu_opts_del(opts
);
2340 static const struct {
2342 CharDriverState
*(*open
)(QemuOpts
*opts
);
2343 } backend_table
[] = {
2344 { .name
= "null", .open
= qemu_chr_open_null
},
2345 { .name
= "socket", .open
= qemu_chr_open_socket
},
2346 { .name
= "udp", .open
= qemu_chr_open_udp
},
2347 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2348 { .name
= "vc", .open
= text_console_init
},
2350 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2351 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2352 { .name
= "console", .open
= qemu_chr_open_win_con
},
2353 { .name
= "serial", .open
= qemu_chr_open_win
},
2355 { .name
= "file", .open
= qemu_chr_open_file_out
},
2356 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2357 { .name
= "pty", .open
= qemu_chr_open_pty
},
2358 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2360 #ifdef CONFIG_BRLAPI
2361 { .name
= "braille", .open
= chr_baum_init
},
2363 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2364 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2365 { .name
= "tty", .open
= qemu_chr_open_tty
},
2367 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2368 { .name
= "parport", .open
= qemu_chr_open_pp
},
2372 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2373 void (*init
)(struct CharDriverState
*s
))
2375 CharDriverState
*chr
;
2378 if (qemu_opts_id(opts
) == NULL
) {
2379 fprintf(stderr
, "chardev: no id specified\n");
2383 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2384 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2387 if (i
== ARRAY_SIZE(backend_table
)) {
2388 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2389 qemu_opt_get(opts
, "backend"));
2393 chr
= backend_table
[i
].open(opts
);
2395 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2396 qemu_opt_get(opts
, "backend"));
2401 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2403 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2405 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2406 CharDriverState
*base
= chr
;
2407 int len
= strlen(qemu_opts_id(opts
)) + 6;
2408 base
->label
= qemu_malloc(len
);
2409 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2410 chr
= qemu_chr_open_mux(base
);
2411 chr
->filename
= base
->filename
;
2412 TAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2414 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2418 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2421 CharDriverState
*chr
;
2424 if (strstart(filename
, "chardev:", &p
)) {
2425 return qemu_chr_find(p
);
2428 opts
= qemu_chr_parse_compat(label
, filename
);
2432 chr
= qemu_chr_open_opts(opts
, init
);
2433 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2434 monitor_init(chr
, MONITOR_USE_READLINE
);
2439 void qemu_chr_close(CharDriverState
*chr
)
2441 TAILQ_REMOVE(&chardevs
, chr
, next
);
2443 chr
->chr_close(chr
);
2444 qemu_free(chr
->filename
);
2445 qemu_free(chr
->label
);
2449 void qemu_chr_info(Monitor
*mon
)
2451 CharDriverState
*chr
;
2453 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2454 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);
2458 CharDriverState
*qemu_chr_find(const char *name
)
2460 CharDriverState
*chr
;
2462 TAILQ_FOREACH(chr
, &chardevs
, next
) {
2463 if (strcmp(chr
->label
, name
) != 0)