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"
35 #include "qemu-objects.h"
46 #include <sys/times.h>
50 #include <sys/ioctl.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
63 #include <dev/ppbus/ppi.h>
64 #include <dev/ppbus/ppbconf.h>
65 #if defined(__GLIBC__)
68 #elif defined(__DragonFly__)
70 #include <dev/misc/ppi/ppi.h>
71 #include <bus/ppbus/ppbconf.h>
79 #include <linux/ppdev.h>
80 #include <linux/parport.h>
84 #include <sys/ethernet.h>
85 #include <sys/sockio.h>
86 #include <netinet/arp.h>
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_icmp.h> // must come after ip.h
91 #include <netinet/udp.h>
92 #include <netinet/tcp.h>
100 #include "qemu_socket.h"
102 #define READ_BUF_LEN 4096
104 /***********************************************************/
105 /* character device */
107 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
108 QTAILQ_HEAD_INITIALIZER(chardevs
);
110 static void qemu_chr_event(CharDriverState
*s
, int event
)
112 /* Keep track if the char device is open */
114 case CHR_EVENT_OPENED
:
117 case CHR_EVENT_CLOSED
:
124 s
->chr_event(s
->handler_opaque
, event
);
127 static void qemu_chr_generic_open_bh(void *opaque
)
129 CharDriverState
*s
= opaque
;
130 qemu_chr_event(s
, CHR_EVENT_OPENED
);
131 qemu_bh_delete(s
->bh
);
135 void qemu_chr_generic_open(CharDriverState
*s
)
138 s
->bh
= qemu_bh_new(qemu_chr_generic_open_bh
, s
);
139 qemu_bh_schedule(s
->bh
);
143 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
145 return s
->chr_write(s
, buf
, len
);
148 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
152 return s
->chr_ioctl(s
, cmd
, arg
);
155 int qemu_chr_can_read(CharDriverState
*s
)
157 if (!s
->chr_can_read
)
159 return s
->chr_can_read(s
->handler_opaque
);
162 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
164 s
->chr_read(s
->handler_opaque
, buf
, len
);
167 int qemu_chr_get_msgfd(CharDriverState
*s
)
169 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
172 void qemu_chr_accept_input(CharDriverState
*s
)
174 if (s
->chr_accept_input
)
175 s
->chr_accept_input(s
);
178 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
180 char buf
[READ_BUF_LEN
];
183 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
184 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
188 void qemu_chr_send_event(CharDriverState
*s
, int event
)
190 if (s
->chr_send_event
)
191 s
->chr_send_event(s
, event
);
194 void qemu_chr_add_handlers(CharDriverState
*s
,
195 IOCanReadHandler
*fd_can_read
,
196 IOReadHandler
*fd_read
,
197 IOEventHandler
*fd_event
,
200 s
->chr_can_read
= fd_can_read
;
201 s
->chr_read
= fd_read
;
202 s
->chr_event
= fd_event
;
203 s
->handler_opaque
= opaque
;
204 if (s
->chr_update_read_handler
)
205 s
->chr_update_read_handler(s
);
207 /* We're connecting to an already opened device, so let's make sure we
208 also get the open event */
210 qemu_chr_generic_open(s
);
214 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
219 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
221 CharDriverState
*chr
;
223 chr
= qemu_mallocz(sizeof(CharDriverState
));
224 chr
->chr_write
= null_chr_write
;
228 /* MUX driver for serial I/O splitting */
230 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
231 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
233 IOCanReadHandler
*chr_can_read
[MAX_MUX
];
234 IOReadHandler
*chr_read
[MAX_MUX
];
235 IOEventHandler
*chr_event
[MAX_MUX
];
236 void *ext_opaque
[MAX_MUX
];
237 CharDriverState
*drv
;
242 /* Intermediate input buffer allows to catch escape sequences even if the
243 currently active device is not accepting any input - but only until it
245 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
250 int64_t timestamps_start
;
254 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
256 MuxDriver
*d
= chr
->opaque
;
258 if (!d
->timestamps
) {
259 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
264 for (i
= 0; i
< len
; i
++) {
270 ti
= qemu_get_clock(rt_clock
);
271 if (d
->timestamps_start
== -1)
272 d
->timestamps_start
= ti
;
273 ti
-= d
->timestamps_start
;
275 snprintf(buf1
, sizeof(buf1
),
276 "[%02d:%02d:%02d.%03d] ",
281 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
284 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
285 if (buf
[i
] == '\n') {
293 static const char * const mux_help
[] = {
294 "% h print this help\n\r",
295 "% x exit emulator\n\r",
296 "% s save disk data back to file (if -snapshot)\n\r",
297 "% t toggle console timestamps\n\r"
298 "% b send break (magic sysrq)\n\r",
299 "% c switch between console and monitor\n\r",
304 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
305 static void mux_print_help(CharDriverState
*chr
)
308 char ebuf
[15] = "Escape-Char";
309 char cbuf
[50] = "\n\r";
311 if (term_escape_char
> 0 && term_escape_char
< 26) {
312 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
313 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
315 snprintf(cbuf
, sizeof(cbuf
),
316 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
319 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
320 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
321 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
322 if (mux_help
[i
][j
] == '%')
323 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
325 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
330 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
332 if (d
->chr_event
[mux_nr
])
333 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
336 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
338 if (d
->term_got_escape
) {
339 d
->term_got_escape
= 0;
340 if (ch
== term_escape_char
)
349 const char *term
= "QEMU: Terminated\n\r";
350 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
357 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
358 bdrv_commit(dinfo
->bdrv
);
363 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
366 /* Switch to the next registered device */
367 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
369 if (d
->focus
>= d
->mux_cnt
)
371 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
374 d
->timestamps
= !d
->timestamps
;
375 d
->timestamps_start
= -1;
379 } else if (ch
== term_escape_char
) {
380 d
->term_got_escape
= 1;
388 static void mux_chr_accept_input(CharDriverState
*chr
)
390 MuxDriver
*d
= chr
->opaque
;
393 while (d
->prod
[m
] != d
->cons
[m
] &&
394 d
->chr_can_read
[m
] &&
395 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
396 d
->chr_read
[m
](d
->ext_opaque
[m
],
397 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
401 static int mux_chr_can_read(void *opaque
)
403 CharDriverState
*chr
= opaque
;
404 MuxDriver
*d
= chr
->opaque
;
407 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
409 if (d
->chr_can_read
[m
])
410 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
414 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
416 CharDriverState
*chr
= opaque
;
417 MuxDriver
*d
= chr
->opaque
;
421 mux_chr_accept_input (opaque
);
423 for(i
= 0; i
< size
; i
++)
424 if (mux_proc_byte(chr
, d
, buf
[i
])) {
425 if (d
->prod
[m
] == d
->cons
[m
] &&
426 d
->chr_can_read
[m
] &&
427 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
428 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
430 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
434 static void mux_chr_event(void *opaque
, int event
)
436 CharDriverState
*chr
= opaque
;
437 MuxDriver
*d
= chr
->opaque
;
440 /* Send the event to all registered listeners */
441 for (i
= 0; i
< d
->mux_cnt
; i
++)
442 mux_chr_send_event(d
, i
, event
);
445 static void mux_chr_update_read_handler(CharDriverState
*chr
)
447 MuxDriver
*d
= chr
->opaque
;
449 if (d
->mux_cnt
>= MAX_MUX
) {
450 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
453 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
454 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
455 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
456 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
457 /* Fix up the real driver with mux routines */
458 if (d
->mux_cnt
== 0) {
459 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
462 if (d
->focus
!= -1) {
463 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
465 d
->focus
= d
->mux_cnt
;
467 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
470 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
472 CharDriverState
*chr
;
475 chr
= qemu_mallocz(sizeof(CharDriverState
));
476 d
= qemu_mallocz(sizeof(MuxDriver
));
481 chr
->chr_write
= mux_chr_write
;
482 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
483 chr
->chr_accept_input
= mux_chr_accept_input
;
485 /* Muxes are always open on creation */
486 qemu_chr_generic_open(chr
);
493 int send_all(int fd
, const void *buf
, int len1
)
499 ret
= send(fd
, buf
, len
, 0);
501 errno
= WSAGetLastError();
502 if (errno
!= WSAEWOULDBLOCK
) {
505 } else if (ret
== 0) {
517 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
523 ret
= write(fd
, buf
, len
);
525 if (errno
!= EINTR
&& errno
!= EAGAIN
)
527 } else if (ret
== 0) {
537 int send_all(int fd
, const void *buf
, int len1
)
539 return unix_write(fd
, buf
, len1
);
550 #define STDIO_MAX_CLIENTS 1
551 static int stdio_nb_clients
= 0;
553 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
555 FDCharDriver
*s
= chr
->opaque
;
556 return send_all(s
->fd_out
, buf
, len
);
559 static int fd_chr_read_poll(void *opaque
)
561 CharDriverState
*chr
= opaque
;
562 FDCharDriver
*s
= chr
->opaque
;
564 s
->max_size
= qemu_chr_can_read(chr
);
568 static void fd_chr_read(void *opaque
)
570 CharDriverState
*chr
= opaque
;
571 FDCharDriver
*s
= chr
->opaque
;
573 uint8_t buf
[READ_BUF_LEN
];
576 if (len
> s
->max_size
)
580 size
= read(s
->fd_in
, buf
, len
);
582 /* FD has been closed. Remove it from the active list. */
583 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
584 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
588 qemu_chr_read(chr
, buf
, size
);
592 static void fd_chr_update_read_handler(CharDriverState
*chr
)
594 FDCharDriver
*s
= chr
->opaque
;
597 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
599 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
600 fd_chr_read
, NULL
, chr
);
605 static void fd_chr_close(struct CharDriverState
*chr
)
607 FDCharDriver
*s
= chr
->opaque
;
610 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
612 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
617 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
620 /* open a character device to a unix fd */
621 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
623 CharDriverState
*chr
;
626 chr
= qemu_mallocz(sizeof(CharDriverState
));
627 s
= qemu_mallocz(sizeof(FDCharDriver
));
631 chr
->chr_write
= fd_chr_write
;
632 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
633 chr
->chr_close
= fd_chr_close
;
635 qemu_chr_generic_open(chr
);
640 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
644 TFR(fd_out
= qemu_open(qemu_opt_get(opts
, "path"),
645 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
648 return qemu_chr_open_fd(-1, fd_out
);
651 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
654 char filename_in
[256], filename_out
[256];
655 const char *filename
= qemu_opt_get(opts
, "path");
657 if (filename
== NULL
) {
658 fprintf(stderr
, "chardev: pipe: no filename given\n");
662 snprintf(filename_in
, 256, "%s.in", filename
);
663 snprintf(filename_out
, 256, "%s.out", filename
);
664 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
665 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
666 if (fd_in
< 0 || fd_out
< 0) {
671 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
675 return qemu_chr_open_fd(fd_in
, fd_out
);
679 /* for STDIO, we handle the case where several clients use it
682 #define TERM_FIFO_MAX_SIZE 1
684 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
685 static int term_fifo_size
;
687 static int stdio_read_poll(void *opaque
)
689 CharDriverState
*chr
= opaque
;
691 /* try to flush the queue if needed */
692 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
693 qemu_chr_read(chr
, term_fifo
, 1);
696 /* see if we can absorb more chars */
697 if (term_fifo_size
== 0)
703 static void stdio_read(void *opaque
)
707 CharDriverState
*chr
= opaque
;
709 size
= read(0, buf
, 1);
711 /* stdin has been closed. Remove it from the active list. */
712 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
713 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
717 if (qemu_chr_can_read(chr
) > 0) {
718 qemu_chr_read(chr
, buf
, 1);
719 } else if (term_fifo_size
== 0) {
720 term_fifo
[term_fifo_size
++] = buf
[0];
725 /* init terminal so that we can grab keys */
726 static struct termios oldtty
;
727 static int old_fd0_flags
;
728 static int term_atexit_done
;
730 static void term_exit(void)
732 tcsetattr (0, TCSANOW
, &oldtty
);
733 fcntl(0, F_SETFL
, old_fd0_flags
);
736 static void term_init(QemuOpts
*opts
)
742 old_fd0_flags
= fcntl(0, F_GETFL
);
744 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
745 |INLCR
|IGNCR
|ICRNL
|IXON
);
746 tty
.c_oflag
|= OPOST
;
747 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
748 /* if graphical mode, we allow Ctrl-C handling */
749 if (!qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
))
750 tty
.c_lflag
&= ~ISIG
;
751 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
756 tcsetattr (0, TCSANOW
, &tty
);
758 if (!term_atexit_done
++)
761 fcntl(0, F_SETFL
, O_NONBLOCK
);
764 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
768 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
772 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
774 CharDriverState
*chr
;
776 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
778 chr
= qemu_chr_open_fd(0, 1);
779 chr
->chr_close
= qemu_chr_close_stdio
;
780 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
788 /* Once Solaris has openpty(), this is going to be removed. */
789 static int openpty(int *amaster
, int *aslave
, char *name
,
790 struct termios
*termp
, struct winsize
*winp
)
793 int mfd
= -1, sfd
= -1;
795 *amaster
= *aslave
= -1;
797 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
801 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
804 if ((slave
= ptsname(mfd
)) == NULL
)
807 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
810 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
811 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
819 ioctl(sfd
, TIOCSWINSZ
, winp
);
830 static void cfmakeraw (struct termios
*termios_p
)
832 termios_p
->c_iflag
&=
833 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
834 termios_p
->c_oflag
&= ~OPOST
;
835 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
836 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
837 termios_p
->c_cflag
|= CS8
;
839 termios_p
->c_cc
[VMIN
] = 0;
840 termios_p
->c_cc
[VTIME
] = 0;
844 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
845 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
846 || defined(__GLIBC__)
856 static void pty_chr_update_read_handler(CharDriverState
*chr
);
857 static void pty_chr_state(CharDriverState
*chr
, int connected
);
859 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
861 PtyCharDriver
*s
= chr
->opaque
;
864 /* guest sends data, check for (re-)connect */
865 pty_chr_update_read_handler(chr
);
868 return send_all(s
->fd
, buf
, len
);
871 static int pty_chr_read_poll(void *opaque
)
873 CharDriverState
*chr
= opaque
;
874 PtyCharDriver
*s
= chr
->opaque
;
876 s
->read_bytes
= qemu_chr_can_read(chr
);
877 return s
->read_bytes
;
880 static void pty_chr_read(void *opaque
)
882 CharDriverState
*chr
= opaque
;
883 PtyCharDriver
*s
= chr
->opaque
;
885 uint8_t buf
[READ_BUF_LEN
];
888 if (len
> s
->read_bytes
)
892 size
= read(s
->fd
, buf
, len
);
893 if ((size
== -1 && errno
== EIO
) ||
895 pty_chr_state(chr
, 0);
899 pty_chr_state(chr
, 1);
900 qemu_chr_read(chr
, buf
, size
);
904 static void pty_chr_update_read_handler(CharDriverState
*chr
)
906 PtyCharDriver
*s
= chr
->opaque
;
908 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
909 pty_chr_read
, NULL
, chr
);
912 * Short timeout here: just need wait long enougth that qemu makes
913 * it through the poll loop once. When reconnected we want a
914 * short timeout so we notice it almost instantly. Otherwise
915 * read() gives us -EIO instantly, making pty_chr_state() reset the
916 * timeout to the normal (much longer) poll interval before the
919 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
922 static void pty_chr_state(CharDriverState
*chr
, int connected
)
924 PtyCharDriver
*s
= chr
->opaque
;
927 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
930 /* (re-)connect poll interval for idle guests: once per second.
931 * We check more frequently in case the guests sends data to
932 * the virtual device linked to our pty. */
933 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
936 qemu_chr_generic_open(chr
);
941 static void pty_chr_timer(void *opaque
)
943 struct CharDriverState
*chr
= opaque
;
944 PtyCharDriver
*s
= chr
->opaque
;
949 /* If we arrive here without polling being cleared due
950 * read returning -EIO, then we are (re-)connected */
951 pty_chr_state(chr
, 1);
956 pty_chr_update_read_handler(chr
);
959 static void pty_chr_close(struct CharDriverState
*chr
)
961 PtyCharDriver
*s
= chr
->opaque
;
963 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
965 qemu_del_timer(s
->timer
);
966 qemu_free_timer(s
->timer
);
968 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
971 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
973 CharDriverState
*chr
;
977 #if defined(__OpenBSD__) || defined(__DragonFly__)
978 char pty_name
[PATH_MAX
];
979 #define q_ptsname(x) pty_name
981 char *pty_name
= NULL
;
982 #define q_ptsname(x) ptsname(x)
985 chr
= qemu_mallocz(sizeof(CharDriverState
));
986 s
= qemu_mallocz(sizeof(PtyCharDriver
));
988 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
992 /* Set raw attributes on the pty. */
993 tcgetattr(slave_fd
, &tty
);
995 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
998 len
= strlen(q_ptsname(s
->fd
)) + 5;
999 chr
->filename
= qemu_malloc(len
);
1000 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
1001 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
1002 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
1005 chr
->chr_write
= pty_chr_write
;
1006 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
1007 chr
->chr_close
= pty_chr_close
;
1009 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1014 static void tty_serial_init(int fd
, int speed
,
1015 int parity
, int data_bits
, int stop_bits
)
1021 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1022 speed
, parity
, data_bits
, stop_bits
);
1024 tcgetattr (fd
, &tty
);
1025 if (!term_atexit_done
) {
1029 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1030 speed
= speed
* 10 / 11;
1047 /* Non-Posix values follow. They may be unsupported on some systems. */
1049 check_speed(115200);
1051 check_speed(230400);
1054 check_speed(460800);
1057 check_speed(500000);
1060 check_speed(576000);
1063 check_speed(921600);
1066 check_speed(1000000);
1069 check_speed(1152000);
1072 check_speed(1500000);
1075 check_speed(2000000);
1078 check_speed(2500000);
1081 check_speed(3000000);
1084 check_speed(3500000);
1087 check_speed(4000000);
1092 cfsetispeed(&tty
, spd
);
1093 cfsetospeed(&tty
, spd
);
1095 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1096 |INLCR
|IGNCR
|ICRNL
|IXON
);
1097 tty
.c_oflag
|= OPOST
;
1098 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1099 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1120 tty
.c_cflag
|= PARENB
;
1123 tty
.c_cflag
|= PARENB
| PARODD
;
1127 tty
.c_cflag
|= CSTOPB
;
1129 tcsetattr (fd
, TCSANOW
, &tty
);
1132 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1134 FDCharDriver
*s
= chr
->opaque
;
1137 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1139 QEMUSerialSetParams
*ssp
= arg
;
1140 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1141 ssp
->data_bits
, ssp
->stop_bits
);
1144 case CHR_IOCTL_SERIAL_SET_BREAK
:
1146 int enable
= *(int *)arg
;
1148 tcsendbreak(s
->fd_in
, 1);
1151 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1154 int *targ
= (int *)arg
;
1155 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1157 if (sarg
& TIOCM_CTS
)
1158 *targ
|= CHR_TIOCM_CTS
;
1159 if (sarg
& TIOCM_CAR
)
1160 *targ
|= CHR_TIOCM_CAR
;
1161 if (sarg
& TIOCM_DSR
)
1162 *targ
|= CHR_TIOCM_DSR
;
1163 if (sarg
& TIOCM_RI
)
1164 *targ
|= CHR_TIOCM_RI
;
1165 if (sarg
& TIOCM_DTR
)
1166 *targ
|= CHR_TIOCM_DTR
;
1167 if (sarg
& TIOCM_RTS
)
1168 *targ
|= CHR_TIOCM_RTS
;
1171 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1173 int sarg
= *(int *)arg
;
1175 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1176 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1177 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1178 if (sarg
& CHR_TIOCM_CTS
)
1180 if (sarg
& CHR_TIOCM_CAR
)
1182 if (sarg
& CHR_TIOCM_DSR
)
1184 if (sarg
& CHR_TIOCM_RI
)
1186 if (sarg
& CHR_TIOCM_DTR
)
1188 if (sarg
& CHR_TIOCM_RTS
)
1190 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1199 static void tty_exit(void)
1201 tcsetattr(0, TCSANOW
, &oldtty
);
1204 static void qemu_chr_close_tty(CharDriverState
*chr
)
1206 FDCharDriver
*s
= chr
->opaque
;
1220 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1222 const char *filename
= qemu_opt_get(opts
, "path");
1223 CharDriverState
*chr
;
1226 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1230 tty_serial_init(fd
, 115200, 'N', 8, 1);
1231 chr
= qemu_chr_open_fd(fd
, fd
);
1236 chr
->chr_ioctl
= tty_serial_ioctl
;
1237 chr
->chr_close
= qemu_chr_close_tty
;
1238 if (!term_atexit_done
++)
1242 #else /* ! __linux__ && ! __sun__ */
1243 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
1247 #endif /* __linux__ || __sun__ */
1249 #if defined(__linux__)
1253 } ParallelCharDriver
;
1255 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1257 if (s
->mode
!= mode
) {
1259 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1266 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1268 ParallelCharDriver
*drv
= chr
->opaque
;
1273 case CHR_IOCTL_PP_READ_DATA
:
1274 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1276 *(uint8_t *)arg
= b
;
1278 case CHR_IOCTL_PP_WRITE_DATA
:
1279 b
= *(uint8_t *)arg
;
1280 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1283 case CHR_IOCTL_PP_READ_CONTROL
:
1284 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1286 /* Linux gives only the lowest bits, and no way to know data
1287 direction! For better compatibility set the fixed upper
1289 *(uint8_t *)arg
= b
| 0xc0;
1291 case CHR_IOCTL_PP_WRITE_CONTROL
:
1292 b
= *(uint8_t *)arg
;
1293 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1296 case CHR_IOCTL_PP_READ_STATUS
:
1297 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1299 *(uint8_t *)arg
= b
;
1301 case CHR_IOCTL_PP_DATA_DIR
:
1302 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1305 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1306 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1307 struct ParallelIOArg
*parg
= arg
;
1308 int n
= read(fd
, parg
->buffer
, parg
->count
);
1309 if (n
!= parg
->count
) {
1314 case CHR_IOCTL_PP_EPP_READ
:
1315 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1316 struct ParallelIOArg
*parg
= arg
;
1317 int n
= read(fd
, parg
->buffer
, parg
->count
);
1318 if (n
!= parg
->count
) {
1323 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1324 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1325 struct ParallelIOArg
*parg
= arg
;
1326 int n
= write(fd
, parg
->buffer
, parg
->count
);
1327 if (n
!= parg
->count
) {
1332 case CHR_IOCTL_PP_EPP_WRITE
:
1333 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1334 struct ParallelIOArg
*parg
= arg
;
1335 int n
= write(fd
, parg
->buffer
, parg
->count
);
1336 if (n
!= parg
->count
) {
1347 static void pp_close(CharDriverState
*chr
)
1349 ParallelCharDriver
*drv
= chr
->opaque
;
1352 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1353 ioctl(fd
, PPRELEASE
);
1356 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1359 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1361 const char *filename
= qemu_opt_get(opts
, "path");
1362 CharDriverState
*chr
;
1363 ParallelCharDriver
*drv
;
1366 TFR(fd
= open(filename
, O_RDWR
));
1370 if (ioctl(fd
, PPCLAIM
) < 0) {
1375 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1377 drv
->mode
= IEEE1284_MODE_COMPAT
;
1379 chr
= qemu_mallocz(sizeof(CharDriverState
));
1380 chr
->chr_write
= null_chr_write
;
1381 chr
->chr_ioctl
= pp_ioctl
;
1382 chr
->chr_close
= pp_close
;
1385 qemu_chr_generic_open(chr
);
1389 #endif /* __linux__ */
1391 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1392 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1394 int fd
= (int)(long)chr
->opaque
;
1398 case CHR_IOCTL_PP_READ_DATA
:
1399 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1401 *(uint8_t *)arg
= b
;
1403 case CHR_IOCTL_PP_WRITE_DATA
:
1404 b
= *(uint8_t *)arg
;
1405 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1408 case CHR_IOCTL_PP_READ_CONTROL
:
1409 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1411 *(uint8_t *)arg
= b
;
1413 case CHR_IOCTL_PP_WRITE_CONTROL
:
1414 b
= *(uint8_t *)arg
;
1415 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1418 case CHR_IOCTL_PP_READ_STATUS
:
1419 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1421 *(uint8_t *)arg
= b
;
1429 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1431 const char *filename
= qemu_opt_get(opts
, "path");
1432 CharDriverState
*chr
;
1435 fd
= open(filename
, O_RDWR
);
1439 chr
= qemu_mallocz(sizeof(CharDriverState
));
1440 chr
->opaque
= (void *)(long)fd
;
1441 chr
->chr_write
= null_chr_write
;
1442 chr
->chr_ioctl
= pp_ioctl
;
1451 HANDLE hcom
, hrecv
, hsend
;
1452 OVERLAPPED orecv
, osend
;
1457 #define NSENDBUF 2048
1458 #define NRECVBUF 2048
1459 #define MAXCONNECT 1
1460 #define NTIMEOUT 5000
1462 static int win_chr_poll(void *opaque
);
1463 static int win_chr_pipe_poll(void *opaque
);
1465 static void win_chr_close(CharDriverState
*chr
)
1467 WinCharState
*s
= chr
->opaque
;
1470 CloseHandle(s
->hsend
);
1474 CloseHandle(s
->hrecv
);
1478 CloseHandle(s
->hcom
);
1482 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1484 qemu_del_polling_cb(win_chr_poll
, chr
);
1486 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1489 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1491 WinCharState
*s
= chr
->opaque
;
1493 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1498 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1500 fprintf(stderr
, "Failed CreateEvent\n");
1503 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1505 fprintf(stderr
, "Failed CreateEvent\n");
1509 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1510 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1511 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1512 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1517 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1518 fprintf(stderr
, "Failed SetupComm\n");
1522 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1523 size
= sizeof(COMMCONFIG
);
1524 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1525 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1526 CommConfigDialog(filename
, NULL
, &comcfg
);
1528 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1529 fprintf(stderr
, "Failed SetCommState\n");
1533 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1534 fprintf(stderr
, "Failed SetCommMask\n");
1538 cto
.ReadIntervalTimeout
= MAXDWORD
;
1539 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1540 fprintf(stderr
, "Failed SetCommTimeouts\n");
1544 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1545 fprintf(stderr
, "Failed ClearCommError\n");
1548 qemu_add_polling_cb(win_chr_poll
, chr
);
1556 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1558 WinCharState
*s
= chr
->opaque
;
1559 DWORD len
, ret
, size
, err
;
1562 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1563 s
->osend
.hEvent
= s
->hsend
;
1566 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1568 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1570 err
= GetLastError();
1571 if (err
== ERROR_IO_PENDING
) {
1572 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1590 static int win_chr_read_poll(CharDriverState
*chr
)
1592 WinCharState
*s
= chr
->opaque
;
1594 s
->max_size
= qemu_chr_can_read(chr
);
1598 static void win_chr_readfile(CharDriverState
*chr
)
1600 WinCharState
*s
= chr
->opaque
;
1602 uint8_t buf
[READ_BUF_LEN
];
1605 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1606 s
->orecv
.hEvent
= s
->hrecv
;
1607 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1609 err
= GetLastError();
1610 if (err
== ERROR_IO_PENDING
) {
1611 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1616 qemu_chr_read(chr
, buf
, size
);
1620 static void win_chr_read(CharDriverState
*chr
)
1622 WinCharState
*s
= chr
->opaque
;
1624 if (s
->len
> s
->max_size
)
1625 s
->len
= s
->max_size
;
1629 win_chr_readfile(chr
);
1632 static int win_chr_poll(void *opaque
)
1634 CharDriverState
*chr
= opaque
;
1635 WinCharState
*s
= chr
->opaque
;
1639 ClearCommError(s
->hcom
, &comerr
, &status
);
1640 if (status
.cbInQue
> 0) {
1641 s
->len
= status
.cbInQue
;
1642 win_chr_read_poll(chr
);
1649 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1651 const char *filename
= qemu_opt_get(opts
, "path");
1652 CharDriverState
*chr
;
1655 chr
= qemu_mallocz(sizeof(CharDriverState
));
1656 s
= qemu_mallocz(sizeof(WinCharState
));
1658 chr
->chr_write
= win_chr_write
;
1659 chr
->chr_close
= win_chr_close
;
1661 if (win_chr_init(chr
, filename
) < 0) {
1666 qemu_chr_generic_open(chr
);
1670 static int win_chr_pipe_poll(void *opaque
)
1672 CharDriverState
*chr
= opaque
;
1673 WinCharState
*s
= chr
->opaque
;
1676 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1679 win_chr_read_poll(chr
);
1686 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1688 WinCharState
*s
= chr
->opaque
;
1696 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1698 fprintf(stderr
, "Failed CreateEvent\n");
1701 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1703 fprintf(stderr
, "Failed CreateEvent\n");
1707 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1708 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1709 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1711 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1712 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1713 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1718 ZeroMemory(&ov
, sizeof(ov
));
1719 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1720 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1722 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1726 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1728 fprintf(stderr
, "Failed GetOverlappedResult\n");
1730 CloseHandle(ov
.hEvent
);
1737 CloseHandle(ov
.hEvent
);
1740 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1749 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1751 const char *filename
= qemu_opt_get(opts
, "path");
1752 CharDriverState
*chr
;
1755 chr
= qemu_mallocz(sizeof(CharDriverState
));
1756 s
= qemu_mallocz(sizeof(WinCharState
));
1758 chr
->chr_write
= win_chr_write
;
1759 chr
->chr_close
= win_chr_close
;
1761 if (win_chr_pipe_init(chr
, filename
) < 0) {
1766 qemu_chr_generic_open(chr
);
1770 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1772 CharDriverState
*chr
;
1775 chr
= qemu_mallocz(sizeof(CharDriverState
));
1776 s
= qemu_mallocz(sizeof(WinCharState
));
1779 chr
->chr_write
= win_chr_write
;
1780 qemu_chr_generic_open(chr
);
1784 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1786 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1789 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1791 const char *file_out
= qemu_opt_get(opts
, "path");
1794 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1795 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1796 if (fd_out
== INVALID_HANDLE_VALUE
)
1799 return qemu_chr_open_win_file(fd_out
);
1801 #endif /* !_WIN32 */
1803 /***********************************************************/
1804 /* UDP Net console */
1808 uint8_t buf
[READ_BUF_LEN
];
1814 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1816 NetCharDriver
*s
= chr
->opaque
;
1818 return send(s
->fd
, (const void *)buf
, len
, 0);
1821 static int udp_chr_read_poll(void *opaque
)
1823 CharDriverState
*chr
= opaque
;
1824 NetCharDriver
*s
= chr
->opaque
;
1826 s
->max_size
= qemu_chr_can_read(chr
);
1828 /* If there were any stray characters in the queue process them
1831 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1832 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1834 s
->max_size
= qemu_chr_can_read(chr
);
1839 static void udp_chr_read(void *opaque
)
1841 CharDriverState
*chr
= opaque
;
1842 NetCharDriver
*s
= chr
->opaque
;
1844 if (s
->max_size
== 0)
1846 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1847 s
->bufptr
= s
->bufcnt
;
1852 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1853 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1855 s
->max_size
= qemu_chr_can_read(chr
);
1859 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1861 NetCharDriver
*s
= chr
->opaque
;
1864 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1865 udp_chr_read
, NULL
, chr
);
1869 static void udp_chr_close(CharDriverState
*chr
)
1871 NetCharDriver
*s
= chr
->opaque
;
1873 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1877 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1880 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1882 CharDriverState
*chr
= NULL
;
1883 NetCharDriver
*s
= NULL
;
1886 chr
= qemu_mallocz(sizeof(CharDriverState
));
1887 s
= qemu_mallocz(sizeof(NetCharDriver
));
1889 fd
= inet_dgram_opts(opts
);
1891 fprintf(stderr
, "inet_dgram_opts failed\n");
1899 chr
->chr_write
= udp_chr_write
;
1900 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1901 chr
->chr_close
= udp_chr_close
;
1914 /***********************************************************/
1915 /* TCP Net console */
1927 static void tcp_chr_accept(void *opaque
);
1929 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1931 TCPCharDriver
*s
= chr
->opaque
;
1933 return send_all(s
->fd
, buf
, len
);
1935 /* XXX: indicate an error ? */
1940 static int tcp_chr_read_poll(void *opaque
)
1942 CharDriverState
*chr
= opaque
;
1943 TCPCharDriver
*s
= chr
->opaque
;
1946 s
->max_size
= qemu_chr_can_read(chr
);
1951 #define IAC_BREAK 243
1952 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1954 uint8_t *buf
, int *size
)
1956 /* Handle any telnet client's basic IAC options to satisfy char by
1957 * char mode with no echo. All IAC options will be removed from
1958 * the buf and the do_telnetopt variable will be used to track the
1959 * state of the width of the IAC information.
1961 * IAC commands come in sets of 3 bytes with the exception of the
1962 * "IAC BREAK" command and the double IAC.
1968 for (i
= 0; i
< *size
; i
++) {
1969 if (s
->do_telnetopt
> 1) {
1970 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1971 /* Double IAC means send an IAC */
1975 s
->do_telnetopt
= 1;
1977 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1978 /* Handle IAC break commands by sending a serial break */
1979 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1984 if (s
->do_telnetopt
>= 4) {
1985 s
->do_telnetopt
= 1;
1988 if ((unsigned char)buf
[i
] == IAC
) {
1989 s
->do_telnetopt
= 2;
2000 static int tcp_get_msgfd(CharDriverState
*chr
)
2002 TCPCharDriver
*s
= chr
->opaque
;
2009 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
2011 TCPCharDriver
*s
= chr
->opaque
;
2012 struct cmsghdr
*cmsg
;
2014 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
2017 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
2018 cmsg
->cmsg_level
!= SOL_SOCKET
||
2019 cmsg
->cmsg_type
!= SCM_RIGHTS
)
2022 fd
= *((int *)CMSG_DATA(cmsg
));
2032 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2034 TCPCharDriver
*s
= chr
->opaque
;
2035 struct msghdr msg
= { NULL
, };
2036 struct iovec iov
[1];
2038 struct cmsghdr cmsg
;
2039 char control
[CMSG_SPACE(sizeof(int))];
2043 iov
[0].iov_base
= buf
;
2044 iov
[0].iov_len
= len
;
2048 msg
.msg_control
= &msg_control
;
2049 msg
.msg_controllen
= sizeof(msg_control
);
2051 ret
= recvmsg(s
->fd
, &msg
, 0);
2052 if (ret
> 0 && s
->is_unix
)
2053 unix_process_msgfd(chr
, &msg
);
2058 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2060 TCPCharDriver
*s
= chr
->opaque
;
2061 return recv(s
->fd
, buf
, len
, 0);
2065 static void tcp_chr_read(void *opaque
)
2067 CharDriverState
*chr
= opaque
;
2068 TCPCharDriver
*s
= chr
->opaque
;
2069 uint8_t buf
[READ_BUF_LEN
];
2072 if (!s
->connected
|| s
->max_size
<= 0)
2075 if (len
> s
->max_size
)
2077 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2079 /* connection closed */
2081 if (s
->listen_fd
>= 0) {
2082 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2084 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2087 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2088 } else if (size
> 0) {
2089 if (s
->do_telnetopt
)
2090 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2092 qemu_chr_read(chr
, buf
, size
);
2096 static void tcp_chr_connect(void *opaque
)
2098 CharDriverState
*chr
= opaque
;
2099 TCPCharDriver
*s
= chr
->opaque
;
2102 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2103 tcp_chr_read
, NULL
, chr
);
2104 qemu_chr_generic_open(chr
);
2107 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2108 static void tcp_chr_telnet_init(int fd
)
2111 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2112 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2113 send(fd
, (char *)buf
, 3, 0);
2114 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2115 send(fd
, (char *)buf
, 3, 0);
2116 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2117 send(fd
, (char *)buf
, 3, 0);
2118 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2119 send(fd
, (char *)buf
, 3, 0);
2122 static void socket_set_nodelay(int fd
)
2125 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2128 static void tcp_chr_accept(void *opaque
)
2130 CharDriverState
*chr
= opaque
;
2131 TCPCharDriver
*s
= chr
->opaque
;
2132 struct sockaddr_in saddr
;
2134 struct sockaddr_un uaddr
;
2136 struct sockaddr
*addr
;
2143 len
= sizeof(uaddr
);
2144 addr
= (struct sockaddr
*)&uaddr
;
2148 len
= sizeof(saddr
);
2149 addr
= (struct sockaddr
*)&saddr
;
2151 fd
= qemu_accept(s
->listen_fd
, addr
, &len
);
2152 if (fd
< 0 && errno
!= EINTR
) {
2154 } else if (fd
>= 0) {
2155 if (s
->do_telnetopt
)
2156 tcp_chr_telnet_init(fd
);
2160 socket_set_nonblock(fd
);
2162 socket_set_nodelay(fd
);
2164 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2165 tcp_chr_connect(chr
);
2168 static void tcp_chr_close(CharDriverState
*chr
)
2170 TCPCharDriver
*s
= chr
->opaque
;
2172 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2175 if (s
->listen_fd
>= 0) {
2176 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2177 closesocket(s
->listen_fd
);
2180 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2183 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2185 CharDriverState
*chr
= NULL
;
2186 TCPCharDriver
*s
= NULL
;
2194 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2195 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2196 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2197 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2198 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2202 chr
= qemu_mallocz(sizeof(CharDriverState
));
2203 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2207 fd
= unix_listen_opts(opts
);
2209 fd
= unix_connect_opts(opts
);
2213 fd
= inet_listen_opts(opts
, 0);
2215 fd
= inet_connect_opts(opts
);
2221 if (!is_waitconnect
)
2222 socket_set_nonblock(fd
);
2228 s
->is_unix
= is_unix
;
2229 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2232 chr
->chr_write
= tcp_chr_write
;
2233 chr
->chr_close
= tcp_chr_close
;
2234 chr
->get_msgfd
= tcp_get_msgfd
;
2238 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2240 s
->do_telnetopt
= 1;
2245 socket_set_nodelay(fd
);
2246 tcp_chr_connect(chr
);
2249 /* for "info chardev" monitor command */
2250 chr
->filename
= qemu_malloc(256);
2252 snprintf(chr
->filename
, 256, "unix:%s%s",
2253 qemu_opt_get(opts
, "path"),
2254 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2255 } else if (is_telnet
) {
2256 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2257 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2258 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2260 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2261 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2262 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2265 if (is_listen
&& is_waitconnect
) {
2266 printf("QEMU waiting for connection on: %s\n",
2268 tcp_chr_accept(chr
);
2269 socket_set_nonblock(s
->listen_fd
);
2281 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2283 char host
[65], port
[33], width
[8], height
[8];
2288 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2292 if (strstart(filename
, "mon:", &p
)) {
2294 qemu_opt_set(opts
, "mux", "on");
2297 if (strcmp(filename
, "null") == 0 ||
2298 strcmp(filename
, "pty") == 0 ||
2299 strcmp(filename
, "msmouse") == 0 ||
2300 strcmp(filename
, "braille") == 0 ||
2301 strcmp(filename
, "stdio") == 0) {
2302 qemu_opt_set(opts
, "backend", filename
);
2305 if (strstart(filename
, "vc", &p
)) {
2306 qemu_opt_set(opts
, "backend", "vc");
2308 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2310 qemu_opt_set(opts
, "width", width
);
2311 qemu_opt_set(opts
, "height", height
);
2312 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2314 qemu_opt_set(opts
, "cols", width
);
2315 qemu_opt_set(opts
, "rows", height
);
2322 if (strcmp(filename
, "con:") == 0) {
2323 qemu_opt_set(opts
, "backend", "console");
2326 if (strstart(filename
, "COM", NULL
)) {
2327 qemu_opt_set(opts
, "backend", "serial");
2328 qemu_opt_set(opts
, "path", filename
);
2331 if (strstart(filename
, "file:", &p
)) {
2332 qemu_opt_set(opts
, "backend", "file");
2333 qemu_opt_set(opts
, "path", p
);
2336 if (strstart(filename
, "pipe:", &p
)) {
2337 qemu_opt_set(opts
, "backend", "pipe");
2338 qemu_opt_set(opts
, "path", p
);
2341 if (strstart(filename
, "tcp:", &p
) ||
2342 strstart(filename
, "telnet:", &p
)) {
2343 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2345 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2348 qemu_opt_set(opts
, "backend", "socket");
2349 qemu_opt_set(opts
, "host", host
);
2350 qemu_opt_set(opts
, "port", port
);
2351 if (p
[pos
] == ',') {
2352 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2355 if (strstart(filename
, "telnet:", &p
))
2356 qemu_opt_set(opts
, "telnet", "on");
2359 if (strstart(filename
, "udp:", &p
)) {
2360 qemu_opt_set(opts
, "backend", "udp");
2361 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2363 if (sscanf(p
, ":%32[^@,]%n", port
, &pos
) < 1) {
2367 qemu_opt_set(opts
, "host", host
);
2368 qemu_opt_set(opts
, "port", port
);
2369 if (p
[pos
] == '@') {
2371 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2373 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2377 qemu_opt_set(opts
, "localaddr", host
);
2378 qemu_opt_set(opts
, "localport", port
);
2382 if (strstart(filename
, "unix:", &p
)) {
2383 qemu_opt_set(opts
, "backend", "socket");
2384 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2388 if (strstart(filename
, "/dev/parport", NULL
) ||
2389 strstart(filename
, "/dev/ppi", NULL
)) {
2390 qemu_opt_set(opts
, "backend", "parport");
2391 qemu_opt_set(opts
, "path", filename
);
2394 if (strstart(filename
, "/dev/", NULL
)) {
2395 qemu_opt_set(opts
, "backend", "tty");
2396 qemu_opt_set(opts
, "path", filename
);
2401 qemu_opts_del(opts
);
2405 static const struct {
2407 CharDriverState
*(*open
)(QemuOpts
*opts
);
2408 } backend_table
[] = {
2409 { .name
= "null", .open
= qemu_chr_open_null
},
2410 { .name
= "socket", .open
= qemu_chr_open_socket
},
2411 { .name
= "udp", .open
= qemu_chr_open_udp
},
2412 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2413 { .name
= "vc", .open
= text_console_init
},
2415 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2416 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2417 { .name
= "console", .open
= qemu_chr_open_win_con
},
2418 { .name
= "serial", .open
= qemu_chr_open_win
},
2420 { .name
= "file", .open
= qemu_chr_open_file_out
},
2421 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2422 { .name
= "pty", .open
= qemu_chr_open_pty
},
2423 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2425 #ifdef CONFIG_BRLAPI
2426 { .name
= "braille", .open
= chr_baum_init
},
2428 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2429 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2430 || defined(__FreeBSD_kernel__)
2431 { .name
= "tty", .open
= qemu_chr_open_tty
},
2433 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2434 || defined(__FreeBSD_kernel__)
2435 { .name
= "parport", .open
= qemu_chr_open_pp
},
2439 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2440 void (*init
)(struct CharDriverState
*s
))
2442 CharDriverState
*chr
;
2445 if (qemu_opts_id(opts
) == NULL
) {
2446 fprintf(stderr
, "chardev: no id specified\n");
2450 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2451 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2454 if (i
== ARRAY_SIZE(backend_table
)) {
2455 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2456 qemu_opt_get(opts
, "backend"));
2460 chr
= backend_table
[i
].open(opts
);
2462 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2463 qemu_opt_get(opts
, "backend"));
2468 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2470 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2472 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2473 CharDriverState
*base
= chr
;
2474 int len
= strlen(qemu_opts_id(opts
)) + 6;
2475 base
->label
= qemu_malloc(len
);
2476 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2477 chr
= qemu_chr_open_mux(base
);
2478 chr
->filename
= base
->filename
;
2479 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2481 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2485 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2488 CharDriverState
*chr
;
2491 if (strstart(filename
, "chardev:", &p
)) {
2492 return qemu_chr_find(p
);
2495 opts
= qemu_chr_parse_compat(label
, filename
);
2499 chr
= qemu_chr_open_opts(opts
, init
);
2500 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2501 monitor_init(chr
, MONITOR_USE_READLINE
);
2506 void qemu_chr_close(CharDriverState
*chr
)
2508 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2510 chr
->chr_close(chr
);
2511 qemu_free(chr
->filename
);
2512 qemu_free(chr
->label
);
2516 static void qemu_chr_qlist_iter(QObject
*obj
, void *opaque
)
2519 Monitor
*mon
= opaque
;
2521 chr_dict
= qobject_to_qdict(obj
);
2522 monitor_printf(mon
, "%s: filename=%s\n", qdict_get_str(chr_dict
, "label"),
2523 qdict_get_str(chr_dict
, "filename"));
2526 void qemu_chr_info_print(Monitor
*mon
, const QObject
*ret_data
)
2528 qlist_iter(qobject_to_qlist(ret_data
), qemu_chr_qlist_iter
, mon
);
2532 * qemu_chr_info(): Character devices information
2534 * Each device is represented by a QDict. The returned QObject is a QList
2537 * The QDict contains the following:
2539 * - "label": device's label
2540 * - "filename": device's file
2544 * [ { "label": "monitor", "filename", "stdio" },
2545 * { "label": "serial0", "filename": "vc" } ]
2547 void qemu_chr_info(Monitor
*mon
, QObject
**ret_data
)
2550 CharDriverState
*chr
;
2552 chr_list
= qlist_new();
2554 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2555 QObject
*obj
= qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2556 chr
->label
, chr
->filename
);
2557 qlist_append_obj(chr_list
, obj
);
2560 *ret_data
= QOBJECT(chr_list
);
2563 CharDriverState
*qemu_chr_find(const char *name
)
2565 CharDriverState
*chr
;
2567 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2568 if (strcmp(chr
->label
, name
) != 0)