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"
33 #include "hw/msmouse.h"
34 #include "qemu-objects.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
54 #include <arpa/inet.h>
57 #include <sys/select.h>
60 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
62 #include <dev/ppbus/ppi.h>
63 #include <dev/ppbus/ppbconf.h>
64 #if defined(__GLIBC__)
67 #elif defined(__DragonFly__)
69 #include <dev/misc/ppi/ppi.h>
70 #include <bus/ppbus/ppbconf.h>
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
99 #include "qemu_socket.h"
101 #define READ_BUF_LEN 4096
103 /***********************************************************/
104 /* character device */
106 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
107 QTAILQ_HEAD_INITIALIZER(chardevs
);
109 static void qemu_chr_event(CharDriverState
*s
, int event
)
111 /* Keep track if the char device is open */
113 case CHR_EVENT_OPENED
:
116 case CHR_EVENT_CLOSED
:
123 s
->chr_event(s
->handler_opaque
, event
);
126 static void qemu_chr_generic_open_bh(void *opaque
)
128 CharDriverState
*s
= opaque
;
129 qemu_chr_event(s
, CHR_EVENT_OPENED
);
130 qemu_bh_delete(s
->bh
);
134 void qemu_chr_generic_open(CharDriverState
*s
)
137 s
->bh
= qemu_bh_new(qemu_chr_generic_open_bh
, s
);
138 qemu_bh_schedule(s
->bh
);
142 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
144 return s
->chr_write(s
, buf
, len
);
147 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
151 return s
->chr_ioctl(s
, cmd
, arg
);
154 int qemu_chr_can_read(CharDriverState
*s
)
156 if (!s
->chr_can_read
)
158 return s
->chr_can_read(s
->handler_opaque
);
161 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
163 s
->chr_read(s
->handler_opaque
, buf
, len
);
166 int qemu_chr_get_msgfd(CharDriverState
*s
)
168 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
171 void qemu_chr_accept_input(CharDriverState
*s
)
173 if (s
->chr_accept_input
)
174 s
->chr_accept_input(s
);
177 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
179 char buf
[READ_BUF_LEN
];
182 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
183 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
187 void qemu_chr_send_event(CharDriverState
*s
, int event
)
189 if (s
->chr_send_event
)
190 s
->chr_send_event(s
, event
);
193 void qemu_chr_add_handlers(CharDriverState
*s
,
194 IOCanReadHandler
*fd_can_read
,
195 IOReadHandler
*fd_read
,
196 IOEventHandler
*fd_event
,
199 s
->chr_can_read
= fd_can_read
;
200 s
->chr_read
= fd_read
;
201 s
->chr_event
= fd_event
;
202 s
->handler_opaque
= opaque
;
203 if (s
->chr_update_read_handler
)
204 s
->chr_update_read_handler(s
);
206 /* We're connecting to an already opened device, so let's make sure we
207 also get the open event */
209 qemu_chr_generic_open(s
);
213 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
218 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
220 CharDriverState
*chr
;
222 chr
= qemu_mallocz(sizeof(CharDriverState
));
223 chr
->chr_write
= null_chr_write
;
227 /* MUX driver for serial I/O splitting */
229 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
230 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
232 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
233 IOReadHandler
*chr_read
[MAX_MUX
];
234 IOEventHandler
*chr_event
[MAX_MUX
];
235 void *ext_opaque
[MAX_MUX
];
236 CharDriverState
*drv
;
241 /* Intermediate input buffer allows to catch escape sequences even if the
242 currently active device is not accepting any input - but only until it
244 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
249 int64_t timestamps_start
;
253 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
255 MuxDriver
*d
= chr
->opaque
;
257 if (!d
->timestamps
) {
258 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
263 for (i
= 0; i
< len
; i
++) {
269 ti
= qemu_get_clock(rt_clock
);
270 if (d
->timestamps_start
== -1)
271 d
->timestamps_start
= ti
;
272 ti
-= d
->timestamps_start
;
274 snprintf(buf1
, sizeof(buf1
),
275 "[%02d:%02d:%02d.%03d] ",
280 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
283 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
284 if (buf
[i
] == '\n') {
292 static const char * const mux_help
[] = {
293 "% h print this help\n\r",
294 "% x exit emulator\n\r",
295 "% s save disk data back to file (if -snapshot)\n\r",
296 "% t toggle console timestamps\n\r"
297 "% b send break (magic sysrq)\n\r",
298 "% c switch between console and monitor\n\r",
303 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
304 static void mux_print_help(CharDriverState
*chr
)
307 char ebuf
[15] = "Escape-Char";
308 char cbuf
[50] = "\n\r";
310 if (term_escape_char
> 0 && term_escape_char
< 26) {
311 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
312 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
314 snprintf(cbuf
, sizeof(cbuf
),
315 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
318 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
319 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
320 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
321 if (mux_help
[i
][j
] == '%')
322 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
324 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
329 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
331 if (d
->chr_event
[mux_nr
])
332 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
335 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
337 if (d
->term_got_escape
) {
338 d
->term_got_escape
= 0;
339 if (ch
== term_escape_char
)
348 const char *term
= "QEMU: Terminated\n\r";
349 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
357 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
360 /* Switch to the next registered device */
361 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
363 if (d
->focus
>= d
->mux_cnt
)
365 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
368 d
->timestamps
= !d
->timestamps
;
369 d
->timestamps_start
= -1;
373 } else if (ch
== term_escape_char
) {
374 d
->term_got_escape
= 1;
382 static void mux_chr_accept_input(CharDriverState
*chr
)
384 MuxDriver
*d
= chr
->opaque
;
387 while (d
->prod
[m
] != d
->cons
[m
] &&
388 d
->chr_can_read
[m
] &&
389 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
390 d
->chr_read
[m
](d
->ext_opaque
[m
],
391 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
395 static int mux_chr_can_read(void *opaque
)
397 CharDriverState
*chr
= opaque
;
398 MuxDriver
*d
= chr
->opaque
;
401 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
403 if (d
->chr_can_read
[m
])
404 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
408 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
410 CharDriverState
*chr
= opaque
;
411 MuxDriver
*d
= chr
->opaque
;
415 mux_chr_accept_input (opaque
);
417 for(i
= 0; i
< size
; i
++)
418 if (mux_proc_byte(chr
, d
, buf
[i
])) {
419 if (d
->prod
[m
] == d
->cons
[m
] &&
420 d
->chr_can_read
[m
] &&
421 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
422 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
424 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
428 static void mux_chr_event(void *opaque
, int event
)
430 CharDriverState
*chr
= opaque
;
431 MuxDriver
*d
= chr
->opaque
;
434 /* Send the event to all registered listeners */
435 for (i
= 0; i
< d
->mux_cnt
; i
++)
436 mux_chr_send_event(d
, i
, event
);
439 static void mux_chr_update_read_handler(CharDriverState
*chr
)
441 MuxDriver
*d
= chr
->opaque
;
443 if (d
->mux_cnt
>= MAX_MUX
) {
444 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
447 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
448 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
449 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
450 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
451 /* Fix up the real driver with mux routines */
452 if (d
->mux_cnt
== 0) {
453 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
456 if (d
->focus
!= -1) {
457 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
459 d
->focus
= d
->mux_cnt
;
461 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
464 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
466 CharDriverState
*chr
;
469 chr
= qemu_mallocz(sizeof(CharDriverState
));
470 d
= qemu_mallocz(sizeof(MuxDriver
));
475 chr
->chr_write
= mux_chr_write
;
476 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
477 chr
->chr_accept_input
= mux_chr_accept_input
;
479 /* Muxes are always open on creation */
480 qemu_chr_generic_open(chr
);
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 int send_all(int fd
, const void *_buf
, int len1
)
514 const uint8_t *buf
= _buf
;
518 ret
= write(fd
, buf
, len
);
520 if (errno
!= EINTR
&& errno
!= EAGAIN
)
522 } else if (ret
== 0) {
540 #define STDIO_MAX_CLIENTS 1
541 static int stdio_nb_clients
= 0;
543 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
545 FDCharDriver
*s
= chr
->opaque
;
546 return send_all(s
->fd_out
, buf
, len
);
549 static int fd_chr_read_poll(void *opaque
)
551 CharDriverState
*chr
= opaque
;
552 FDCharDriver
*s
= chr
->opaque
;
554 s
->max_size
= qemu_chr_can_read(chr
);
558 static void fd_chr_read(void *opaque
)
560 CharDriverState
*chr
= opaque
;
561 FDCharDriver
*s
= chr
->opaque
;
563 uint8_t buf
[READ_BUF_LEN
];
566 if (len
> s
->max_size
)
570 size
= read(s
->fd_in
, buf
, len
);
572 /* FD has been closed. Remove it from the active list. */
573 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
574 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
578 qemu_chr_read(chr
, buf
, size
);
582 static void fd_chr_update_read_handler(CharDriverState
*chr
)
584 FDCharDriver
*s
= chr
->opaque
;
587 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
589 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
590 fd_chr_read
, NULL
, chr
);
595 static void fd_chr_close(struct CharDriverState
*chr
)
597 FDCharDriver
*s
= chr
->opaque
;
600 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
602 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
607 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
610 /* open a character device to a unix fd */
611 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
613 CharDriverState
*chr
;
616 chr
= qemu_mallocz(sizeof(CharDriverState
));
617 s
= qemu_mallocz(sizeof(FDCharDriver
));
621 chr
->chr_write
= fd_chr_write
;
622 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
623 chr
->chr_close
= fd_chr_close
;
625 qemu_chr_generic_open(chr
);
630 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
634 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
635 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
638 return qemu_chr_open_fd(-1, fd_out
);
641 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
644 char filename_in
[256], filename_out
[256];
645 const char *filename
= qemu_opt_get(opts
, "path");
647 if (filename
== NULL
) {
648 fprintf(stderr
, "chardev: pipe: no filename given\n");
652 snprintf(filename_in
, 256, "%s.in", filename
);
653 snprintf(filename_out
, 256, "%s.out", filename
);
654 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
655 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
656 if (fd_in
< 0 || fd_out
< 0) {
661 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
665 return qemu_chr_open_fd(fd_in
, fd_out
);
669 /* for STDIO, we handle the case where several clients use it
672 #define TERM_FIFO_MAX_SIZE 1
674 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
675 static int term_fifo_size
;
677 static int stdio_read_poll(void *opaque
)
679 CharDriverState
*chr
= opaque
;
681 /* try to flush the queue if needed */
682 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
683 qemu_chr_read(chr
, term_fifo
, 1);
686 /* see if we can absorb more chars */
687 if (term_fifo_size
== 0)
693 static void stdio_read(void *opaque
)
697 CharDriverState
*chr
= opaque
;
699 size
= read(0, buf
, 1);
701 /* stdin has been closed. Remove it from the active list. */
702 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
703 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
707 if (qemu_chr_can_read(chr
) > 0) {
708 qemu_chr_read(chr
, buf
, 1);
709 } else if (term_fifo_size
== 0) {
710 term_fifo
[term_fifo_size
++] = buf
[0];
715 /* init terminal so that we can grab keys */
716 static struct termios oldtty
;
717 static int old_fd0_flags
;
718 static int term_atexit_done
;
720 static void term_exit(void)
722 tcsetattr (0, TCSANOW
, &oldtty
);
723 fcntl(0, F_SETFL
, old_fd0_flags
);
726 static void term_init(QemuOpts
*opts
)
732 old_fd0_flags
= fcntl(0, F_GETFL
);
734 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
735 |INLCR
|IGNCR
|ICRNL
|IXON
);
736 tty
.c_oflag
|= OPOST
;
737 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
738 /* if graphical mode, we allow Ctrl-C handling */
739 if (!qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
))
740 tty
.c_lflag
&= ~ISIG
;
741 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
746 tcsetattr (0, TCSANOW
, &tty
);
748 if (!term_atexit_done
++)
751 fcntl(0, F_SETFL
, O_NONBLOCK
);
754 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
758 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
762 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
764 CharDriverState
*chr
;
766 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
768 chr
= qemu_chr_open_fd(0, 1);
769 chr
->chr_close
= qemu_chr_close_stdio
;
770 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
778 /* Once Solaris has openpty(), this is going to be removed. */
779 static int openpty(int *amaster
, int *aslave
, char *name
,
780 struct termios
*termp
, struct winsize
*winp
)
783 int mfd
= -1, sfd
= -1;
785 *amaster
= *aslave
= -1;
787 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
791 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
794 if ((slave
= ptsname(mfd
)) == NULL
)
797 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
800 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
801 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
809 ioctl(sfd
, TIOCSWINSZ
, winp
);
820 static void cfmakeraw (struct termios
*termios_p
)
822 termios_p
->c_iflag
&=
823 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
824 termios_p
->c_oflag
&= ~OPOST
;
825 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
826 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
827 termios_p
->c_cflag
|= CS8
;
829 termios_p
->c_cc
[VMIN
] = 0;
830 termios_p
->c_cc
[VTIME
] = 0;
834 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
835 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
836 || defined(__GLIBC__)
846 static void pty_chr_update_read_handler(CharDriverState
*chr
);
847 static void pty_chr_state(CharDriverState
*chr
, int connected
);
849 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
851 PtyCharDriver
*s
= chr
->opaque
;
854 /* guest sends data, check for (re-)connect */
855 pty_chr_update_read_handler(chr
);
858 return send_all(s
->fd
, buf
, len
);
861 static int pty_chr_read_poll(void *opaque
)
863 CharDriverState
*chr
= opaque
;
864 PtyCharDriver
*s
= chr
->opaque
;
866 s
->read_bytes
= qemu_chr_can_read(chr
);
867 return s
->read_bytes
;
870 static void pty_chr_read(void *opaque
)
872 CharDriverState
*chr
= opaque
;
873 PtyCharDriver
*s
= chr
->opaque
;
875 uint8_t buf
[READ_BUF_LEN
];
878 if (len
> s
->read_bytes
)
882 size
= read(s
->fd
, buf
, len
);
883 if ((size
== -1 && errno
== EIO
) ||
885 pty_chr_state(chr
, 0);
889 pty_chr_state(chr
, 1);
890 qemu_chr_read(chr
, buf
, size
);
894 static void pty_chr_update_read_handler(CharDriverState
*chr
)
896 PtyCharDriver
*s
= chr
->opaque
;
898 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
899 pty_chr_read
, NULL
, chr
);
902 * Short timeout here: just need wait long enougth that qemu makes
903 * it through the poll loop once. When reconnected we want a
904 * short timeout so we notice it almost instantly. Otherwise
905 * read() gives us -EIO instantly, making pty_chr_state() reset the
906 * timeout to the normal (much longer) poll interval before the
909 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
912 static void pty_chr_state(CharDriverState
*chr
, int connected
)
914 PtyCharDriver
*s
= chr
->opaque
;
917 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
920 /* (re-)connect poll interval for idle guests: once per second.
921 * We check more frequently in case the guests sends data to
922 * the virtual device linked to our pty. */
923 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
926 qemu_chr_generic_open(chr
);
931 static void pty_chr_timer(void *opaque
)
933 struct CharDriverState
*chr
= opaque
;
934 PtyCharDriver
*s
= chr
->opaque
;
939 /* If we arrive here without polling being cleared due
940 * read returning -EIO, then we are (re-)connected */
941 pty_chr_state(chr
, 1);
946 pty_chr_update_read_handler(chr
);
949 static void pty_chr_close(struct CharDriverState
*chr
)
951 PtyCharDriver
*s
= chr
->opaque
;
953 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
955 qemu_del_timer(s
->timer
);
956 qemu_free_timer(s
->timer
);
958 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
961 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
963 CharDriverState
*chr
;
967 #if defined(__OpenBSD__) || defined(__DragonFly__)
968 char pty_name
[PATH_MAX
];
969 #define q_ptsname(x) pty_name
971 char *pty_name
= NULL
;
972 #define q_ptsname(x) ptsname(x)
975 chr
= qemu_mallocz(sizeof(CharDriverState
));
976 s
= qemu_mallocz(sizeof(PtyCharDriver
));
978 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
982 /* Set raw attributes on the pty. */
983 tcgetattr(slave_fd
, &tty
);
985 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
988 len
= strlen(q_ptsname(s
->fd
)) + 5;
989 chr
->filename
= qemu_malloc(len
);
990 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
991 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
992 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
995 chr
->chr_write
= pty_chr_write
;
996 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
997 chr
->chr_close
= pty_chr_close
;
999 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1004 static void tty_serial_init(int fd
, int speed
,
1005 int parity
, int data_bits
, int stop_bits
)
1011 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1012 speed
, parity
, data_bits
, stop_bits
);
1014 tcgetattr (fd
, &tty
);
1015 if (!term_atexit_done
) {
1019 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1020 speed
= speed
* 10 / 11;
1037 /* Non-Posix values follow. They may be unsupported on some systems. */
1039 check_speed(115200);
1041 check_speed(230400);
1044 check_speed(460800);
1047 check_speed(500000);
1050 check_speed(576000);
1053 check_speed(921600);
1056 check_speed(1000000);
1059 check_speed(1152000);
1062 check_speed(1500000);
1065 check_speed(2000000);
1068 check_speed(2500000);
1071 check_speed(3000000);
1074 check_speed(3500000);
1077 check_speed(4000000);
1082 cfsetispeed(&tty
, spd
);
1083 cfsetospeed(&tty
, spd
);
1085 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1086 |INLCR
|IGNCR
|ICRNL
|IXON
);
1087 tty
.c_oflag
|= OPOST
;
1088 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1089 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1110 tty
.c_cflag
|= PARENB
;
1113 tty
.c_cflag
|= PARENB
| PARODD
;
1117 tty
.c_cflag
|= CSTOPB
;
1119 tcsetattr (fd
, TCSANOW
, &tty
);
1122 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1124 FDCharDriver
*s
= chr
->opaque
;
1127 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1129 QEMUSerialSetParams
*ssp
= arg
;
1130 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1131 ssp
->data_bits
, ssp
->stop_bits
);
1134 case CHR_IOCTL_SERIAL_SET_BREAK
:
1136 int enable
= *(int *)arg
;
1138 tcsendbreak(s
->fd_in
, 1);
1141 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1144 int *targ
= (int *)arg
;
1145 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1147 if (sarg
& TIOCM_CTS
)
1148 *targ
|= CHR_TIOCM_CTS
;
1149 if (sarg
& TIOCM_CAR
)
1150 *targ
|= CHR_TIOCM_CAR
;
1151 if (sarg
& TIOCM_DSR
)
1152 *targ
|= CHR_TIOCM_DSR
;
1153 if (sarg
& TIOCM_RI
)
1154 *targ
|= CHR_TIOCM_RI
;
1155 if (sarg
& TIOCM_DTR
)
1156 *targ
|= CHR_TIOCM_DTR
;
1157 if (sarg
& TIOCM_RTS
)
1158 *targ
|= CHR_TIOCM_RTS
;
1161 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1163 int sarg
= *(int *)arg
;
1165 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1166 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1167 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1168 if (sarg
& CHR_TIOCM_CTS
)
1170 if (sarg
& CHR_TIOCM_CAR
)
1172 if (sarg
& CHR_TIOCM_DSR
)
1174 if (sarg
& CHR_TIOCM_RI
)
1176 if (sarg
& CHR_TIOCM_DTR
)
1178 if (sarg
& CHR_TIOCM_RTS
)
1180 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1189 static void tty_exit(void)
1191 tcsetattr(0, TCSANOW
, &oldtty
);
1194 static void qemu_chr_close_tty(CharDriverState
*chr
)
1196 FDCharDriver
*s
= chr
->opaque
;
1210 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1212 const char *filename
= qemu_opt_get(opts
, "path");
1213 CharDriverState
*chr
;
1216 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1220 tty_serial_init(fd
, 115200, 'N', 8, 1);
1221 chr
= qemu_chr_open_fd(fd
, fd
);
1226 chr
->chr_ioctl
= tty_serial_ioctl
;
1227 chr
->chr_close
= qemu_chr_close_tty
;
1228 if (!term_atexit_done
++)
1232 #else /* ! __linux__ && ! __sun__ */
1233 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1237 #endif /* __linux__ || __sun__ */
1239 #if defined(__linux__)
1243 } ParallelCharDriver
;
1245 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1247 if (s
->mode
!= mode
) {
1249 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1256 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1258 ParallelCharDriver
*drv
= chr
->opaque
;
1263 case CHR_IOCTL_PP_READ_DATA
:
1264 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1266 *(uint8_t *)arg
= b
;
1268 case CHR_IOCTL_PP_WRITE_DATA
:
1269 b
= *(uint8_t *)arg
;
1270 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1273 case CHR_IOCTL_PP_READ_CONTROL
:
1274 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1276 /* Linux gives only the lowest bits, and no way to know data
1277 direction! For better compatibility set the fixed upper
1279 *(uint8_t *)arg
= b
| 0xc0;
1281 case CHR_IOCTL_PP_WRITE_CONTROL
:
1282 b
= *(uint8_t *)arg
;
1283 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1286 case CHR_IOCTL_PP_READ_STATUS
:
1287 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1289 *(uint8_t *)arg
= b
;
1291 case CHR_IOCTL_PP_DATA_DIR
:
1292 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1295 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1296 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1297 struct ParallelIOArg
*parg
= arg
;
1298 int n
= read(fd
, parg
->buffer
, parg
->count
);
1299 if (n
!= parg
->count
) {
1304 case CHR_IOCTL_PP_EPP_READ
:
1305 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1306 struct ParallelIOArg
*parg
= arg
;
1307 int n
= read(fd
, parg
->buffer
, parg
->count
);
1308 if (n
!= parg
->count
) {
1313 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1314 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1315 struct ParallelIOArg
*parg
= arg
;
1316 int n
= write(fd
, parg
->buffer
, parg
->count
);
1317 if (n
!= parg
->count
) {
1322 case CHR_IOCTL_PP_EPP_WRITE
:
1323 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1324 struct ParallelIOArg
*parg
= arg
;
1325 int n
= write(fd
, parg
->buffer
, parg
->count
);
1326 if (n
!= parg
->count
) {
1337 static void pp_close(CharDriverState
*chr
)
1339 ParallelCharDriver
*drv
= chr
->opaque
;
1342 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1343 ioctl(fd
, PPRELEASE
);
1346 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1349 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1351 const char *filename
= qemu_opt_get(opts
, "path");
1352 CharDriverState
*chr
;
1353 ParallelCharDriver
*drv
;
1356 TFR(fd
= open(filename
, O_RDWR
));
1360 if (ioctl(fd
, PPCLAIM
) < 0) {
1365 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1367 drv
->mode
= IEEE1284_MODE_COMPAT
;
1369 chr
= qemu_mallocz(sizeof(CharDriverState
));
1370 chr
->chr_write
= null_chr_write
;
1371 chr
->chr_ioctl
= pp_ioctl
;
1372 chr
->chr_close
= pp_close
;
1375 qemu_chr_generic_open(chr
);
1379 #endif /* __linux__ */
1381 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1382 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1384 int fd
= (int)(long)chr
->opaque
;
1388 case CHR_IOCTL_PP_READ_DATA
:
1389 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1391 *(uint8_t *)arg
= b
;
1393 case CHR_IOCTL_PP_WRITE_DATA
:
1394 b
= *(uint8_t *)arg
;
1395 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1398 case CHR_IOCTL_PP_READ_CONTROL
:
1399 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1401 *(uint8_t *)arg
= b
;
1403 case CHR_IOCTL_PP_WRITE_CONTROL
:
1404 b
= *(uint8_t *)arg
;
1405 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1408 case CHR_IOCTL_PP_READ_STATUS
:
1409 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1411 *(uint8_t *)arg
= b
;
1419 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1421 const char *filename
= qemu_opt_get(opts
, "path");
1422 CharDriverState
*chr
;
1425 fd
= open(filename
, O_RDWR
);
1429 chr
= qemu_mallocz(sizeof(CharDriverState
));
1430 chr
->opaque
= (void *)(long)fd
;
1431 chr
->chr_write
= null_chr_write
;
1432 chr
->chr_ioctl
= pp_ioctl
;
1441 HANDLE hcom
, hrecv
, hsend
;
1442 OVERLAPPED orecv
, osend
;
1447 #define NSENDBUF 2048
1448 #define NRECVBUF 2048
1449 #define MAXCONNECT 1
1450 #define NTIMEOUT 5000
1452 static int win_chr_poll(void *opaque
);
1453 static int win_chr_pipe_poll(void *opaque
);
1455 static void win_chr_close(CharDriverState
*chr
)
1457 WinCharState
*s
= chr
->opaque
;
1460 CloseHandle(s
->hsend
);
1464 CloseHandle(s
->hrecv
);
1468 CloseHandle(s
->hcom
);
1472 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1474 qemu_del_polling_cb(win_chr_poll
, chr
);
1476 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1479 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1481 WinCharState
*s
= chr
->opaque
;
1483 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1488 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1490 fprintf(stderr
, "Failed CreateEvent\n");
1493 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1495 fprintf(stderr
, "Failed CreateEvent\n");
1499 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1500 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1501 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1502 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1507 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1508 fprintf(stderr
, "Failed SetupComm\n");
1512 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1513 size
= sizeof(COMMCONFIG
);
1514 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1515 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1516 CommConfigDialog(filename
, NULL
, &comcfg
);
1518 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1519 fprintf(stderr
, "Failed SetCommState\n");
1523 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1524 fprintf(stderr
, "Failed SetCommMask\n");
1528 cto
.ReadIntervalTimeout
= MAXDWORD
;
1529 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1530 fprintf(stderr
, "Failed SetCommTimeouts\n");
1534 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1535 fprintf(stderr
, "Failed ClearCommError\n");
1538 qemu_add_polling_cb(win_chr_poll
, chr
);
1546 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1548 WinCharState
*s
= chr
->opaque
;
1549 DWORD len
, ret
, size
, err
;
1552 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1553 s
->osend
.hEvent
= s
->hsend
;
1556 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1558 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1560 err
= GetLastError();
1561 if (err
== ERROR_IO_PENDING
) {
1562 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1580 static int win_chr_read_poll(CharDriverState
*chr
)
1582 WinCharState
*s
= chr
->opaque
;
1584 s
->max_size
= qemu_chr_can_read(chr
);
1588 static void win_chr_readfile(CharDriverState
*chr
)
1590 WinCharState
*s
= chr
->opaque
;
1592 uint8_t buf
[READ_BUF_LEN
];
1595 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1596 s
->orecv
.hEvent
= s
->hrecv
;
1597 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1599 err
= GetLastError();
1600 if (err
== ERROR_IO_PENDING
) {
1601 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1606 qemu_chr_read(chr
, buf
, size
);
1610 static void win_chr_read(CharDriverState
*chr
)
1612 WinCharState
*s
= chr
->opaque
;
1614 if (s
->len
> s
->max_size
)
1615 s
->len
= s
->max_size
;
1619 win_chr_readfile(chr
);
1622 static int win_chr_poll(void *opaque
)
1624 CharDriverState
*chr
= opaque
;
1625 WinCharState
*s
= chr
->opaque
;
1629 ClearCommError(s
->hcom
, &comerr
, &status
);
1630 if (status
.cbInQue
> 0) {
1631 s
->len
= status
.cbInQue
;
1632 win_chr_read_poll(chr
);
1639 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1641 const char *filename
= qemu_opt_get(opts
, "path");
1642 CharDriverState
*chr
;
1645 chr
= qemu_mallocz(sizeof(CharDriverState
));
1646 s
= qemu_mallocz(sizeof(WinCharState
));
1648 chr
->chr_write
= win_chr_write
;
1649 chr
->chr_close
= win_chr_close
;
1651 if (win_chr_init(chr
, filename
) < 0) {
1656 qemu_chr_generic_open(chr
);
1660 static int win_chr_pipe_poll(void *opaque
)
1662 CharDriverState
*chr
= opaque
;
1663 WinCharState
*s
= chr
->opaque
;
1666 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1669 win_chr_read_poll(chr
);
1676 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1678 WinCharState
*s
= chr
->opaque
;
1686 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1688 fprintf(stderr
, "Failed CreateEvent\n");
1691 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1693 fprintf(stderr
, "Failed CreateEvent\n");
1697 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1698 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1699 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1701 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1702 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1703 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1708 ZeroMemory(&ov
, sizeof(ov
));
1709 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1710 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1712 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1716 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1718 fprintf(stderr
, "Failed GetOverlappedResult\n");
1720 CloseHandle(ov
.hEvent
);
1727 CloseHandle(ov
.hEvent
);
1730 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1739 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1741 const char *filename
= qemu_opt_get(opts
, "path");
1742 CharDriverState
*chr
;
1745 chr
= qemu_mallocz(sizeof(CharDriverState
));
1746 s
= qemu_mallocz(sizeof(WinCharState
));
1748 chr
->chr_write
= win_chr_write
;
1749 chr
->chr_close
= win_chr_close
;
1751 if (win_chr_pipe_init(chr
, filename
) < 0) {
1756 qemu_chr_generic_open(chr
);
1760 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1762 CharDriverState
*chr
;
1765 chr
= qemu_mallocz(sizeof(CharDriverState
));
1766 s
= qemu_mallocz(sizeof(WinCharState
));
1769 chr
->chr_write
= win_chr_write
;
1770 qemu_chr_generic_open(chr
);
1774 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1776 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1779 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1781 const char *file_out
= qemu_opt_get(opts
, "path");
1784 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1785 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1786 if (fd_out
== INVALID_HANDLE_VALUE
)
1789 return qemu_chr_open_win_file(fd_out
);
1791 #endif /* !_WIN32 */
1793 /***********************************************************/
1794 /* UDP Net console */
1798 uint8_t buf
[READ_BUF_LEN
];
1804 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1806 NetCharDriver
*s
= chr
->opaque
;
1808 return send(s
->fd
, (const void *)buf
, len
, 0);
1811 static int udp_chr_read_poll(void *opaque
)
1813 CharDriverState
*chr
= opaque
;
1814 NetCharDriver
*s
= chr
->opaque
;
1816 s
->max_size
= qemu_chr_can_read(chr
);
1818 /* If there were any stray characters in the queue process them
1821 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1822 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1824 s
->max_size
= qemu_chr_can_read(chr
);
1829 static void udp_chr_read(void *opaque
)
1831 CharDriverState
*chr
= opaque
;
1832 NetCharDriver
*s
= chr
->opaque
;
1834 if (s
->max_size
== 0)
1836 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1837 s
->bufptr
= s
->bufcnt
;
1842 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1843 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1845 s
->max_size
= qemu_chr_can_read(chr
);
1849 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1851 NetCharDriver
*s
= chr
->opaque
;
1854 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1855 udp_chr_read
, NULL
, chr
);
1859 static void udp_chr_close(CharDriverState
*chr
)
1861 NetCharDriver
*s
= chr
->opaque
;
1863 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1867 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1870 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1872 CharDriverState
*chr
= NULL
;
1873 NetCharDriver
*s
= NULL
;
1876 chr
= qemu_mallocz(sizeof(CharDriverState
));
1877 s
= qemu_mallocz(sizeof(NetCharDriver
));
1879 fd
= inet_dgram_opts(opts
);
1881 fprintf(stderr
, "inet_dgram_opts failed\n");
1889 chr
->chr_write
= udp_chr_write
;
1890 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1891 chr
->chr_close
= udp_chr_close
;
1904 /***********************************************************/
1905 /* TCP Net console */
1917 static void tcp_chr_accept(void *opaque
);
1919 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1921 TCPCharDriver
*s
= chr
->opaque
;
1923 return send_all(s
->fd
, buf
, len
);
1925 /* XXX: indicate an error ? */
1930 static int tcp_chr_read_poll(void *opaque
)
1932 CharDriverState
*chr
= opaque
;
1933 TCPCharDriver
*s
= chr
->opaque
;
1936 s
->max_size
= qemu_chr_can_read(chr
);
1941 #define IAC_BREAK 243
1942 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1944 uint8_t *buf
, int *size
)
1946 /* Handle any telnet client's basic IAC options to satisfy char by
1947 * char mode with no echo. All IAC options will be removed from
1948 * the buf and the do_telnetopt variable will be used to track the
1949 * state of the width of the IAC information.
1951 * IAC commands come in sets of 3 bytes with the exception of the
1952 * "IAC BREAK" command and the double IAC.
1958 for (i
= 0; i
< *size
; i
++) {
1959 if (s
->do_telnetopt
> 1) {
1960 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1961 /* Double IAC means send an IAC */
1965 s
->do_telnetopt
= 1;
1967 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1968 /* Handle IAC break commands by sending a serial break */
1969 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1974 if (s
->do_telnetopt
>= 4) {
1975 s
->do_telnetopt
= 1;
1978 if ((unsigned char)buf
[i
] == IAC
) {
1979 s
->do_telnetopt
= 2;
1990 static int tcp_get_msgfd(CharDriverState
*chr
)
1992 TCPCharDriver
*s
= chr
->opaque
;
1999 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2001 TCPCharDriver
*s
= chr
->opaque
;
2002 struct cmsghdr
*cmsg
;
2004 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2007 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2008 cmsg
->cmsg_level
!= SOL_SOCKET
||
2009 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2012 fd
= *((int *)CMSG_DATA(cmsg
));
2022 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2024 TCPCharDriver
*s
= chr
->opaque
;
2025 struct msghdr msg
= { NULL
, };
2026 struct iovec iov
[1];
2028 struct cmsghdr cmsg
;
2029 char control
[CMSG_SPACE(sizeof(int))];
2033 iov
[0].iov_base
= buf
;
2034 iov
[0].iov_len
= len
;
2038 msg
.msg_control
= &msg_control
;
2039 msg
.msg_controllen
= sizeof(msg_control
);
2041 ret
= recvmsg(s
->fd
, &msg
, 0);
2042 if (ret
> 0 && s
->is_unix
)
2043 unix_process_msgfd(chr
, &msg
);
2048 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2050 TCPCharDriver
*s
= chr
->opaque
;
2051 return recv(s
->fd
, buf
, len
, 0);
2055 static void tcp_chr_read(void *opaque
)
2057 CharDriverState
*chr
= opaque
;
2058 TCPCharDriver
*s
= chr
->opaque
;
2059 uint8_t buf
[READ_BUF_LEN
];
2062 if (!s
->connected
|| s
->max_size
<= 0)
2065 if (len
> s
->max_size
)
2067 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2069 /* connection closed */
2071 if (s
->listen_fd
>= 0) {
2072 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2074 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2077 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2078 } else if (size
> 0) {
2079 if (s
->do_telnetopt
)
2080 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2082 qemu_chr_read(chr
, buf
, size
);
2087 CharDriverState
*qemu_chr_open_eventfd(int eventfd
)
2089 return qemu_chr_open_fd(eventfd
, eventfd
);
2093 static void tcp_chr_connect(void *opaque
)
2095 CharDriverState
*chr
= opaque
;
2096 TCPCharDriver
*s
= chr
->opaque
;
2099 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2100 tcp_chr_read
, NULL
, chr
);
2101 qemu_chr_generic_open(chr
);
2104 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2105 static void tcp_chr_telnet_init(int fd
)
2108 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2109 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2110 send(fd
, (char *)buf
, 3, 0);
2111 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2112 send(fd
, (char *)buf
, 3, 0);
2113 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2114 send(fd
, (char *)buf
, 3, 0);
2115 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2116 send(fd
, (char *)buf
, 3, 0);
2119 static void socket_set_nodelay(int fd
)
2122 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2125 static void tcp_chr_accept(void *opaque
)
2127 CharDriverState
*chr
= opaque
;
2128 TCPCharDriver
*s
= chr
->opaque
;
2129 struct sockaddr_in saddr
;
2131 struct sockaddr_un uaddr
;
2133 struct sockaddr
*addr
;
2140 len
= sizeof(uaddr
);
2141 addr
= (struct sockaddr
*)&uaddr
;
2145 len
= sizeof(saddr
);
2146 addr
= (struct sockaddr
*)&saddr
;
2148 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2149 if (fd
< 0 && errno
!= EINTR
) {
2151 } else if (fd
>= 0) {
2152 if (s
->do_telnetopt
)
2153 tcp_chr_telnet_init(fd
);
2157 socket_set_nonblock(fd
);
2159 socket_set_nodelay(fd
);
2161 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2162 tcp_chr_connect(chr
);
2165 static void tcp_chr_close(CharDriverState
*chr
)
2167 TCPCharDriver
*s
= chr
->opaque
;
2169 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2172 if (s
->listen_fd
>= 0) {
2173 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2174 closesocket(s
->listen_fd
);
2177 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2180 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2182 CharDriverState
*chr
= NULL
;
2183 TCPCharDriver
*s
= NULL
;
2191 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2192 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2193 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2194 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2195 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2199 chr
= qemu_mallocz(sizeof(CharDriverState
));
2200 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2204 fd
= unix_listen_opts(opts
);
2206 fd
= unix_connect_opts(opts
);
2210 fd
= inet_listen_opts(opts
, 0);
2212 fd
= inet_connect_opts(opts
);
2218 if (!is_waitconnect
)
2219 socket_set_nonblock(fd
);
2225 s
->is_unix
= is_unix
;
2226 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2229 chr
->chr_write
= tcp_chr_write
;
2230 chr
->chr_close
= tcp_chr_close
;
2231 chr
->get_msgfd
= tcp_get_msgfd
;
2235 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2237 s
->do_telnetopt
= 1;
2242 socket_set_nodelay(fd
);
2243 tcp_chr_connect(chr
);
2246 /* for "info chardev" monitor command */
2247 chr
->filename
= qemu_malloc(256);
2249 snprintf(chr
->filename
, 256, "unix:%s%s",
2250 qemu_opt_get(opts
, "path"),
2251 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2252 } else if (is_telnet
) {
2253 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2254 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2255 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2257 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2258 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2259 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2262 if (is_listen
&& is_waitconnect
) {
2263 printf("QEMU waiting for connection on: %s\n",
2265 tcp_chr_accept(chr
);
2266 socket_set_nonblock(s
->listen_fd
);
2278 /***********************************************************/
2279 /* Memory chardev */
2282 size_t outbuf_capacity
;
2286 static int mem_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2288 MemoryDriver
*d
= chr
->opaque
;
2290 /* TODO: the QString implementation has the same code, we should
2291 * introduce a generic way to do this in cutils.c */
2292 if (d
->outbuf_capacity
< d
->outbuf_size
+ len
) {
2294 d
->outbuf_capacity
+= len
;
2295 d
->outbuf_capacity
*= 2;
2296 d
->outbuf
= qemu_realloc(d
->outbuf
, d
->outbuf_capacity
);
2299 memcpy(d
->outbuf
+ d
->outbuf_size
, buf
, len
);
2300 d
->outbuf_size
+= len
;
2305 void qemu_chr_init_mem(CharDriverState
*chr
)
2309 d
= qemu_malloc(sizeof(*d
));
2311 d
->outbuf_capacity
= 4096;
2312 d
->outbuf
= qemu_mallocz(d
->outbuf_capacity
);
2314 memset(chr
, 0, sizeof(*chr
));
2316 chr
->chr_write
= mem_chr_write
;
2319 QString
*qemu_chr_mem_to_qs(CharDriverState
*chr
)
2321 MemoryDriver
*d
= chr
->opaque
;
2322 return qstring_from_substr((char *) d
->outbuf
, 0, d
->outbuf_size
- 1);
2325 /* NOTE: this driver can not be closed with qemu_chr_close()! */
2326 void qemu_chr_close_mem(CharDriverState
*chr
)
2328 MemoryDriver
*d
= chr
->opaque
;
2330 qemu_free(d
->outbuf
);
2331 qemu_free(chr
->opaque
);
2333 chr
->chr_write
= NULL
;
2336 size_t qemu_chr_mem_osize(const CharDriverState
*chr
)
2338 const MemoryDriver
*d
= chr
->opaque
;
2339 return d
->outbuf_size
;
2342 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2344 char host
[65], port
[33], width
[8], height
[8];
2349 opts
= qemu_opts_create(qemu_find_opts("chardev"), label
, 1);
2353 if (strstart(filename
, "mon:", &p
)) {
2355 qemu_opt_set(opts
, "mux", "on");
2358 if (strcmp(filename
, "null") == 0 ||
2359 strcmp(filename
, "pty") == 0 ||
2360 strcmp(filename
, "msmouse") == 0 ||
2361 strcmp(filename
, "braille") == 0 ||
2362 strcmp(filename
, "stdio") == 0) {
2363 qemu_opt_set(opts
, "backend", filename
);
2366 if (strstart(filename
, "vc", &p
)) {
2367 qemu_opt_set(opts
, "backend", "vc");
2369 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2371 qemu_opt_set(opts
, "width", width
);
2372 qemu_opt_set(opts
, "height", height
);
2373 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2375 qemu_opt_set(opts
, "cols", width
);
2376 qemu_opt_set(opts
, "rows", height
);
2383 if (strcmp(filename
, "con:") == 0) {
2384 qemu_opt_set(opts
, "backend", "console");
2387 if (strstart(filename
, "COM", NULL
)) {
2388 qemu_opt_set(opts
, "backend", "serial");
2389 qemu_opt_set(opts
, "path", filename
);
2392 if (strstart(filename
, "file:", &p
)) {
2393 qemu_opt_set(opts
, "backend", "file");
2394 qemu_opt_set(opts
, "path", p
);
2397 if (strstart(filename
, "pipe:", &p
)) {
2398 qemu_opt_set(opts
, "backend", "pipe");
2399 qemu_opt_set(opts
, "path", p
);
2402 if (strstart(filename
, "tcp:", &p
) ||
2403 strstart(filename
, "telnet:", &p
)) {
2404 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2406 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2409 qemu_opt_set(opts
, "backend", "socket");
2410 qemu_opt_set(opts
, "host", host
);
2411 qemu_opt_set(opts
, "port", port
);
2412 if (p
[pos
] == ',') {
2413 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2416 if (strstart(filename
, "telnet:", &p
))
2417 qemu_opt_set(opts
, "telnet", "on");
2420 if (strstart(filename
, "udp:", &p
)) {
2421 qemu_opt_set(opts
, "backend", "udp");
2422 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2424 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2428 qemu_opt_set(opts
, "host", host
);
2429 qemu_opt_set(opts
, "port", port
);
2430 if (p
[pos
] == '@') {
2432 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2434 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2438 qemu_opt_set(opts
, "localaddr", host
);
2439 qemu_opt_set(opts
, "localport", port
);
2443 if (strstart(filename
, "unix:", &p
)) {
2444 qemu_opt_set(opts
, "backend", "socket");
2445 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2449 if (strstart(filename
, "/dev/parport", NULL
) ||
2450 strstart(filename
, "/dev/ppi", NULL
)) {
2451 qemu_opt_set(opts
, "backend", "parport");
2452 qemu_opt_set(opts
, "path", filename
);
2455 if (strstart(filename
, "/dev/", NULL
)) {
2456 qemu_opt_set(opts
, "backend", "tty");
2457 qemu_opt_set(opts
, "path", filename
);
2462 qemu_opts_del(opts
);
2466 static const struct {
2468 CharDriverState
*(*open
)(QemuOpts
*opts
);
2469 } backend_table
[] = {
2470 { .name
= "null", .open
= qemu_chr_open_null
},
2471 { .name
= "socket", .open
= qemu_chr_open_socket
},
2472 { .name
= "udp", .open
= qemu_chr_open_udp
},
2473 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2474 { .name
= "vc", .open
= text_console_init
},
2476 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2477 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2478 { .name
= "console", .open
= qemu_chr_open_win_con
},
2479 { .name
= "serial", .open
= qemu_chr_open_win
},
2481 { .name
= "file", .open
= qemu_chr_open_file_out
},
2482 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2483 { .name
= "pty", .open
= qemu_chr_open_pty
},
2484 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2486 #ifdef CONFIG_BRLAPI
2487 { .name
= "braille", .open
= chr_baum_init
},
2489 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2490 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2491 || defined(__FreeBSD_kernel__)
2492 { .name
= "tty", .open
= qemu_chr_open_tty
},
2494 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2495 || defined(__FreeBSD_kernel__)
2496 { .name
= "parport", .open
= qemu_chr_open_pp
},
2500 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2501 void (*init
)(struct CharDriverState
*s
))
2503 CharDriverState
*chr
;
2506 if (qemu_opts_id(opts
) == NULL
) {
2507 fprintf(stderr
, "chardev: no id specified\n");
2511 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2512 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2515 if (i
== ARRAY_SIZE(backend_table
)) {
2516 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2517 qemu_opt_get(opts
, "backend"));
2521 chr
= backend_table
[i
].open(opts
);
2523 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2524 qemu_opt_get(opts
, "backend"));
2529 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2531 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2533 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2534 CharDriverState
*base
= chr
;
2535 int len
= strlen(qemu_opts_id(opts
)) + 6;
2536 base
->label
= qemu_malloc(len
);
2537 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2538 chr
= qemu_chr_open_mux(base
);
2539 chr
->filename
= base
->filename
;
2540 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2542 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2546 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2549 CharDriverState
*chr
;
2552 if (strstart(filename
, "chardev:", &p
)) {
2553 return qemu_chr_find(p
);
2556 opts
= qemu_chr_parse_compat(label
, filename
);
2560 chr
= qemu_chr_open_opts(opts
, init
);
2561 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2562 monitor_init(chr
, MONITOR_USE_READLINE
);
2567 void qemu_chr_close(CharDriverState
*chr
)
2569 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2571 chr
->chr_close(chr
);
2572 qemu_free(chr
->filename
);
2573 qemu_free(chr
->label
);
2577 static void qemu_chr_qlist_iter(QObject
*obj
, void *opaque
)
2580 Monitor
*mon
= opaque
;
2582 chr_dict
= qobject_to_qdict(obj
);
2583 monitor_printf(mon
, "%s: filename=%s\n", qdict_get_str(chr_dict
, "label"),
2584 qdict_get_str(chr_dict
, "filename"));
2587 void qemu_chr_info_print(Monitor
*mon
, const QObject
*ret_data
)
2589 qlist_iter(qobject_to_qlist(ret_data
), qemu_chr_qlist_iter
, mon
);
2592 void qemu_chr_info(Monitor
*mon
, QObject
**ret_data
)
2595 CharDriverState
*chr
;
2597 chr_list
= qlist_new();
2599 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2600 QObject
*obj
= qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2601 chr
->label
, chr
->filename
);
2602 qlist_append_obj(chr_list
, obj
);
2605 *ret_data
= QOBJECT(chr_list
);
2608 CharDriverState
*qemu_chr_find(const char *name
)
2610 CharDriverState
*chr
;
2612 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2613 if (strcmp(chr
->label
, name
) != 0)