4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
34 #include "hw/msmouse.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
54 #include <arpa/inet.h>
57 #include <sys/select.h>
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>
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
98 #include "qemu_socket.h"
100 #define READ_BUF_LEN 4096
102 /***********************************************************/
103 /* character device */
105 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
106 QTAILQ_HEAD_INITIALIZER(chardevs
);
108 static void qemu_chr_event(CharDriverState
*s
, int event
)
112 s
->chr_event(s
->handler_opaque
, event
);
115 static void qemu_chr_generic_open_bh(void *opaque
)
117 CharDriverState
*s
= opaque
;
118 qemu_chr_event(s
, CHR_EVENT_OPENED
);
119 qemu_bh_delete(s
->bh
);
123 void qemu_chr_generic_open(CharDriverState
*s
)
126 s
->bh
= qemu_bh_new(qemu_chr_generic_open_bh
, s
);
127 qemu_bh_schedule(s
->bh
);
131 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
133 return s
->chr_write(s
, buf
, len
);
136 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
140 return s
->chr_ioctl(s
, cmd
, arg
);
143 int qemu_chr_can_read(CharDriverState
*s
)
145 if (!s
->chr_can_read
)
147 return s
->chr_can_read(s
->handler_opaque
);
150 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
152 s
->chr_read(s
->handler_opaque
, buf
, len
);
155 int qemu_chr_get_msgfd(CharDriverState
*s
)
157 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
160 void qemu_chr_accept_input(CharDriverState
*s
)
162 if (s
->chr_accept_input
)
163 s
->chr_accept_input(s
);
166 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
168 char buf
[READ_BUF_LEN
];
171 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
172 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
176 void qemu_chr_send_event(CharDriverState
*s
, int event
)
178 if (s
->chr_send_event
)
179 s
->chr_send_event(s
, event
);
182 void qemu_chr_add_handlers(CharDriverState
*s
,
183 IOCanRWHandler
*fd_can_read
,
184 IOReadHandler
*fd_read
,
185 IOEventHandler
*fd_event
,
188 s
->chr_can_read
= fd_can_read
;
189 s
->chr_read
= fd_read
;
190 s
->chr_event
= fd_event
;
191 s
->handler_opaque
= opaque
;
192 if (s
->chr_update_read_handler
)
193 s
->chr_update_read_handler(s
);
196 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
201 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
203 CharDriverState
*chr
;
205 chr
= qemu_mallocz(sizeof(CharDriverState
));
206 chr
->chr_write
= null_chr_write
;
210 /* MUX driver for serial I/O splitting */
212 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
213 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
215 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
216 IOReadHandler
*chr_read
[MAX_MUX
];
217 IOEventHandler
*chr_event
[MAX_MUX
];
218 void *ext_opaque
[MAX_MUX
];
219 CharDriverState
*drv
;
224 /* Intermediate input buffer allows to catch escape sequences even if the
225 currently active device is not accepting any input - but only until it
227 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
232 int64_t timestamps_start
;
236 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
238 MuxDriver
*d
= chr
->opaque
;
240 if (!d
->timestamps
) {
241 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
246 for (i
= 0; i
< len
; i
++) {
252 ti
= qemu_get_clock(rt_clock
);
253 if (d
->timestamps_start
== -1)
254 d
->timestamps_start
= ti
;
255 ti
-= d
->timestamps_start
;
257 snprintf(buf1
, sizeof(buf1
),
258 "[%02d:%02d:%02d.%03d] ",
263 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
266 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
267 if (buf
[i
] == '\n') {
275 static const char * const mux_help
[] = {
276 "% h print this help\n\r",
277 "% x exit emulator\n\r",
278 "% s save disk data back to file (if -snapshot)\n\r",
279 "% t toggle console timestamps\n\r"
280 "% b send break (magic sysrq)\n\r",
281 "% c switch between console and monitor\n\r",
286 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
287 static void mux_print_help(CharDriverState
*chr
)
290 char ebuf
[15] = "Escape-Char";
291 char cbuf
[50] = "\n\r";
293 if (term_escape_char
> 0 && term_escape_char
< 26) {
294 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
295 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
297 snprintf(cbuf
, sizeof(cbuf
),
298 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
301 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
302 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
303 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
304 if (mux_help
[i
][j
] == '%')
305 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
307 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
312 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
314 if (d
->chr_event
[mux_nr
])
315 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
318 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
320 if (d
->term_got_escape
) {
321 d
->term_got_escape
= 0;
322 if (ch
== term_escape_char
)
331 const char *term
= "QEMU: Terminated\n\r";
332 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
339 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
340 bdrv_commit(dinfo
->bdrv
);
345 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
348 /* Switch to the next registered device */
349 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
351 if (d
->focus
>= d
->mux_cnt
)
353 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
356 d
->timestamps
= !d
->timestamps
;
357 d
->timestamps_start
= -1;
361 } else if (ch
== term_escape_char
) {
362 d
->term_got_escape
= 1;
370 static void mux_chr_accept_input(CharDriverState
*chr
)
372 MuxDriver
*d
= chr
->opaque
;
375 while (d
->prod
[m
] != d
->cons
[m
] &&
376 d
->chr_can_read
[m
] &&
377 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
378 d
->chr_read
[m
](d
->ext_opaque
[m
],
379 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
383 static int mux_chr_can_read(void *opaque
)
385 CharDriverState
*chr
= opaque
;
386 MuxDriver
*d
= chr
->opaque
;
389 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
391 if (d
->chr_can_read
[m
])
392 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
396 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
398 CharDriverState
*chr
= opaque
;
399 MuxDriver
*d
= chr
->opaque
;
403 mux_chr_accept_input (opaque
);
405 for(i
= 0; i
< size
; i
++)
406 if (mux_proc_byte(chr
, d
, buf
[i
])) {
407 if (d
->prod
[m
] == d
->cons
[m
] &&
408 d
->chr_can_read
[m
] &&
409 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
410 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
412 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
416 static void mux_chr_event(void *opaque
, int event
)
418 CharDriverState
*chr
= opaque
;
419 MuxDriver
*d
= chr
->opaque
;
422 /* Send the event to all registered listeners */
423 for (i
= 0; i
< d
->mux_cnt
; i
++)
424 mux_chr_send_event(d
, i
, event
);
427 static void mux_chr_update_read_handler(CharDriverState
*chr
)
429 MuxDriver
*d
= chr
->opaque
;
431 if (d
->mux_cnt
>= MAX_MUX
) {
432 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
435 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
436 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
437 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
438 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
439 /* Fix up the real driver with mux routines */
440 if (d
->mux_cnt
== 0) {
441 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
444 if (d
->focus
!= -1) {
445 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
447 d
->focus
= d
->mux_cnt
;
449 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
452 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
454 CharDriverState
*chr
;
457 chr
= qemu_mallocz(sizeof(CharDriverState
));
458 d
= qemu_mallocz(sizeof(MuxDriver
));
463 chr
->chr_write
= mux_chr_write
;
464 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
465 chr
->chr_accept_input
= mux_chr_accept_input
;
471 int send_all(int fd
, const void *buf
, int len1
)
477 ret
= send(fd
, buf
, len
, 0);
479 errno
= WSAGetLastError();
480 if (errno
!= WSAEWOULDBLOCK
) {
483 } else if (ret
== 0) {
495 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
501 ret
= write(fd
, buf
, len
);
503 if (errno
!= EINTR
&& errno
!= EAGAIN
)
505 } else if (ret
== 0) {
515 int send_all(int fd
, const void *buf
, int len1
)
517 return unix_write(fd
, buf
, len1
);
528 #define STDIO_MAX_CLIENTS 1
529 static int stdio_nb_clients
= 0;
531 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
533 FDCharDriver
*s
= chr
->opaque
;
534 return send_all(s
->fd_out
, buf
, len
);
537 static int fd_chr_read_poll(void *opaque
)
539 CharDriverState
*chr
= opaque
;
540 FDCharDriver
*s
= chr
->opaque
;
542 s
->max_size
= qemu_chr_can_read(chr
);
546 static void fd_chr_read(void *opaque
)
548 CharDriverState
*chr
= opaque
;
549 FDCharDriver
*s
= chr
->opaque
;
551 uint8_t buf
[READ_BUF_LEN
];
554 if (len
> s
->max_size
)
558 size
= read(s
->fd_in
, buf
, len
);
560 /* FD has been closed. Remove it from the active list. */
561 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
562 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
566 qemu_chr_read(chr
, buf
, size
);
570 static void fd_chr_update_read_handler(CharDriverState
*chr
)
572 FDCharDriver
*s
= chr
->opaque
;
575 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
577 qemu_set_fd_handler2(s
->fd_in
, fd_chr_read_poll
,
578 fd_chr_read
, NULL
, chr
);
583 static void fd_chr_close(struct CharDriverState
*chr
)
585 FDCharDriver
*s
= chr
->opaque
;
588 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
590 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
595 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
598 /* open a character device to a unix fd */
599 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
601 CharDriverState
*chr
;
604 chr
= qemu_mallocz(sizeof(CharDriverState
));
605 s
= qemu_mallocz(sizeof(FDCharDriver
));
609 chr
->chr_write
= fd_chr_write
;
610 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
611 chr
->chr_close
= fd_chr_close
;
613 qemu_chr_generic_open(chr
);
618 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
622 TFR(fd_out
= open(qemu_opt_get(opts
, "path"),
623 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
626 return qemu_chr_open_fd(-1, fd_out
);
629 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
632 char filename_in
[256], filename_out
[256];
633 const char *filename
= qemu_opt_get(opts
, "path");
635 if (filename
== NULL
) {
636 fprintf(stderr
, "chardev: pipe: no filename given\n");
640 snprintf(filename_in
, 256, "%s.in", filename
);
641 snprintf(filename_out
, 256, "%s.out", filename
);
642 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
643 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
644 if (fd_in
< 0 || fd_out
< 0) {
649 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
653 return qemu_chr_open_fd(fd_in
, fd_out
);
657 /* for STDIO, we handle the case where several clients use it
660 #define TERM_FIFO_MAX_SIZE 1
662 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
663 static int term_fifo_size
;
665 static int stdio_read_poll(void *opaque
)
667 CharDriverState
*chr
= opaque
;
669 /* try to flush the queue if needed */
670 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
671 qemu_chr_read(chr
, term_fifo
, 1);
674 /* see if we can absorb more chars */
675 if (term_fifo_size
== 0)
681 static void stdio_read(void *opaque
)
685 CharDriverState
*chr
= opaque
;
687 size
= read(0, buf
, 1);
689 /* stdin has been closed. Remove it from the active list. */
690 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
691 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
695 if (qemu_chr_can_read(chr
) > 0) {
696 qemu_chr_read(chr
, buf
, 1);
697 } else if (term_fifo_size
== 0) {
698 term_fifo
[term_fifo_size
++] = buf
[0];
703 /* init terminal so that we can grab keys */
704 static struct termios oldtty
;
705 static int old_fd0_flags
;
706 static int term_atexit_done
;
708 static void term_exit(void)
710 tcsetattr (0, TCSANOW
, &oldtty
);
711 fcntl(0, F_SETFL
, old_fd0_flags
);
714 static void term_init(QemuOpts
*opts
)
720 old_fd0_flags
= fcntl(0, F_GETFL
);
722 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
723 |INLCR
|IGNCR
|ICRNL
|IXON
);
724 tty
.c_oflag
|= OPOST
;
725 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
726 /* if graphical mode, we allow Ctrl-C handling */
727 if (!qemu_opt_get_bool(opts
, "signal", display_type
!= DT_NOGRAPHIC
))
728 tty
.c_lflag
&= ~ISIG
;
729 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
734 tcsetattr (0, TCSANOW
, &tty
);
736 if (!term_atexit_done
++)
739 fcntl(0, F_SETFL
, O_NONBLOCK
);
742 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
746 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
750 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
752 CharDriverState
*chr
;
754 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
756 chr
= qemu_chr_open_fd(0, 1);
757 chr
->chr_close
= qemu_chr_close_stdio
;
758 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
766 /* Once Solaris has openpty(), this is going to be removed. */
767 static int openpty(int *amaster
, int *aslave
, char *name
,
768 struct termios
*termp
, struct winsize
*winp
)
771 int mfd
= -1, sfd
= -1;
773 *amaster
= *aslave
= -1;
775 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
779 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
782 if ((slave
= ptsname(mfd
)) == NULL
)
785 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
788 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
789 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
797 ioctl(sfd
, TIOCSWINSZ
, winp
);
808 static void cfmakeraw (struct termios
*termios_p
)
810 termios_p
->c_iflag
&=
811 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
812 termios_p
->c_oflag
&= ~OPOST
;
813 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
814 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
815 termios_p
->c_cflag
|= CS8
;
817 termios_p
->c_cc
[VMIN
] = 0;
818 termios_p
->c_cc
[VTIME
] = 0;
822 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
823 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
824 || defined(__GLIBC__)
834 static void pty_chr_update_read_handler(CharDriverState
*chr
);
835 static void pty_chr_state(CharDriverState
*chr
, int connected
);
837 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
839 PtyCharDriver
*s
= chr
->opaque
;
842 /* guest sends data, check for (re-)connect */
843 pty_chr_update_read_handler(chr
);
846 return send_all(s
->fd
, buf
, len
);
849 static int pty_chr_read_poll(void *opaque
)
851 CharDriverState
*chr
= opaque
;
852 PtyCharDriver
*s
= chr
->opaque
;
854 s
->read_bytes
= qemu_chr_can_read(chr
);
855 return s
->read_bytes
;
858 static void pty_chr_read(void *opaque
)
860 CharDriverState
*chr
= opaque
;
861 PtyCharDriver
*s
= chr
->opaque
;
863 uint8_t buf
[READ_BUF_LEN
];
866 if (len
> s
->read_bytes
)
870 size
= read(s
->fd
, buf
, len
);
871 if ((size
== -1 && errno
== EIO
) ||
873 pty_chr_state(chr
, 0);
877 pty_chr_state(chr
, 1);
878 qemu_chr_read(chr
, buf
, size
);
882 static void pty_chr_update_read_handler(CharDriverState
*chr
)
884 PtyCharDriver
*s
= chr
->opaque
;
886 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
887 pty_chr_read
, NULL
, chr
);
890 * Short timeout here: just need wait long enougth that qemu makes
891 * it through the poll loop once. When reconnected we want a
892 * short timeout so we notice it almost instantly. Otherwise
893 * read() gives us -EIO instantly, making pty_chr_state() reset the
894 * timeout to the normal (much longer) poll interval before the
897 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
900 static void pty_chr_state(CharDriverState
*chr
, int connected
)
902 PtyCharDriver
*s
= chr
->opaque
;
905 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
908 /* (re-)connect poll interval for idle guests: once per second.
909 * We check more frequently in case the guests sends data to
910 * the virtual device linked to our pty. */
911 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
914 qemu_chr_generic_open(chr
);
919 static void pty_chr_timer(void *opaque
)
921 struct CharDriverState
*chr
= opaque
;
922 PtyCharDriver
*s
= chr
->opaque
;
927 /* If we arrive here without polling being cleared due
928 * read returning -EIO, then we are (re-)connected */
929 pty_chr_state(chr
, 1);
934 pty_chr_update_read_handler(chr
);
937 static void pty_chr_close(struct CharDriverState
*chr
)
939 PtyCharDriver
*s
= chr
->opaque
;
941 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
943 qemu_del_timer(s
->timer
);
944 qemu_free_timer(s
->timer
);
946 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
949 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
951 CharDriverState
*chr
;
955 #if defined(__OpenBSD__) || defined(__DragonFly__)
956 char pty_name
[PATH_MAX
];
957 #define q_ptsname(x) pty_name
959 char *pty_name
= NULL
;
960 #define q_ptsname(x) ptsname(x)
963 chr
= qemu_mallocz(sizeof(CharDriverState
));
964 s
= qemu_mallocz(sizeof(PtyCharDriver
));
966 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
970 /* Set raw attributes on the pty. */
971 tcgetattr(slave_fd
, &tty
);
973 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
976 len
= strlen(q_ptsname(s
->fd
)) + 5;
977 chr
->filename
= qemu_malloc(len
);
978 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
979 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
980 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
983 chr
->chr_write
= pty_chr_write
;
984 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
985 chr
->chr_close
= pty_chr_close
;
987 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
992 static void tty_serial_init(int fd
, int speed
,
993 int parity
, int data_bits
, int stop_bits
)
999 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1000 speed
, parity
, data_bits
, stop_bits
);
1002 tcgetattr (fd
, &tty
);
1004 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1005 speed
= speed
* 10 / 11;
1022 /* Non-Posix values follow. They may be unsupported on some systems. */
1024 check_speed(115200);
1026 check_speed(230400);
1029 check_speed(460800);
1032 check_speed(500000);
1035 check_speed(576000);
1038 check_speed(921600);
1041 check_speed(1000000);
1044 check_speed(1152000);
1047 check_speed(1500000);
1050 check_speed(2000000);
1053 check_speed(2500000);
1056 check_speed(3000000);
1059 check_speed(3500000);
1062 check_speed(4000000);
1067 cfsetispeed(&tty
, spd
);
1068 cfsetospeed(&tty
, spd
);
1070 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1071 |INLCR
|IGNCR
|ICRNL
|IXON
);
1072 tty
.c_oflag
|= OPOST
;
1073 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1074 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1095 tty
.c_cflag
|= PARENB
;
1098 tty
.c_cflag
|= PARENB
| PARODD
;
1102 tty
.c_cflag
|= CSTOPB
;
1104 tcsetattr (fd
, TCSANOW
, &tty
);
1107 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1109 FDCharDriver
*s
= chr
->opaque
;
1112 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1114 QEMUSerialSetParams
*ssp
= arg
;
1115 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1116 ssp
->data_bits
, ssp
->stop_bits
);
1119 case CHR_IOCTL_SERIAL_SET_BREAK
:
1121 int enable
= *(int *)arg
;
1123 tcsendbreak(s
->fd_in
, 1);
1126 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1129 int *targ
= (int *)arg
;
1130 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1132 if (sarg
& TIOCM_CTS
)
1133 *targ
|= CHR_TIOCM_CTS
;
1134 if (sarg
& TIOCM_CAR
)
1135 *targ
|= CHR_TIOCM_CAR
;
1136 if (sarg
& TIOCM_DSR
)
1137 *targ
|= CHR_TIOCM_DSR
;
1138 if (sarg
& TIOCM_RI
)
1139 *targ
|= CHR_TIOCM_RI
;
1140 if (sarg
& TIOCM_DTR
)
1141 *targ
|= CHR_TIOCM_DTR
;
1142 if (sarg
& TIOCM_RTS
)
1143 *targ
|= CHR_TIOCM_RTS
;
1146 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1148 int sarg
= *(int *)arg
;
1150 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1151 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1152 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1153 if (sarg
& CHR_TIOCM_CTS
)
1155 if (sarg
& CHR_TIOCM_CAR
)
1157 if (sarg
& CHR_TIOCM_DSR
)
1159 if (sarg
& CHR_TIOCM_RI
)
1161 if (sarg
& CHR_TIOCM_DTR
)
1163 if (sarg
& CHR_TIOCM_RTS
)
1165 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1174 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1176 const char *filename
= qemu_opt_get(opts
, "path");
1177 CharDriverState
*chr
;
1180 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1181 tty_serial_init(fd
, 115200, 'N', 8, 1);
1182 chr
= qemu_chr_open_fd(fd
, fd
);
1187 chr
->chr_ioctl
= tty_serial_ioctl
;
1188 qemu_chr_generic_open(chr
);
1191 #else /* ! __linux__ && ! __sun__ */
1192 static CharDriverState
*qemu_chr_open_pty(void)
1196 #endif /* __linux__ || __sun__ */
1198 #if defined(__linux__)
1202 } ParallelCharDriver
;
1204 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1206 if (s
->mode
!= mode
) {
1208 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1215 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1217 ParallelCharDriver
*drv
= chr
->opaque
;
1222 case CHR_IOCTL_PP_READ_DATA
:
1223 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1225 *(uint8_t *)arg
= b
;
1227 case CHR_IOCTL_PP_WRITE_DATA
:
1228 b
= *(uint8_t *)arg
;
1229 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1232 case CHR_IOCTL_PP_READ_CONTROL
:
1233 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1235 /* Linux gives only the lowest bits, and no way to know data
1236 direction! For better compatibility set the fixed upper
1238 *(uint8_t *)arg
= b
| 0xc0;
1240 case CHR_IOCTL_PP_WRITE_CONTROL
:
1241 b
= *(uint8_t *)arg
;
1242 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1245 case CHR_IOCTL_PP_READ_STATUS
:
1246 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1248 *(uint8_t *)arg
= b
;
1250 case CHR_IOCTL_PP_DATA_DIR
:
1251 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1254 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1255 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1256 struct ParallelIOArg
*parg
= arg
;
1257 int n
= read(fd
, parg
->buffer
, parg
->count
);
1258 if (n
!= parg
->count
) {
1263 case CHR_IOCTL_PP_EPP_READ
:
1264 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1265 struct ParallelIOArg
*parg
= arg
;
1266 int n
= read(fd
, parg
->buffer
, parg
->count
);
1267 if (n
!= parg
->count
) {
1272 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1273 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1274 struct ParallelIOArg
*parg
= arg
;
1275 int n
= write(fd
, parg
->buffer
, parg
->count
);
1276 if (n
!= parg
->count
) {
1281 case CHR_IOCTL_PP_EPP_WRITE
:
1282 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1283 struct ParallelIOArg
*parg
= arg
;
1284 int n
= write(fd
, parg
->buffer
, parg
->count
);
1285 if (n
!= parg
->count
) {
1296 static void pp_close(CharDriverState
*chr
)
1298 ParallelCharDriver
*drv
= chr
->opaque
;
1301 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1302 ioctl(fd
, PPRELEASE
);
1305 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1308 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1310 const char *filename
= qemu_opt_get(opts
, "path");
1311 CharDriverState
*chr
;
1312 ParallelCharDriver
*drv
;
1315 TFR(fd
= open(filename
, O_RDWR
));
1319 if (ioctl(fd
, PPCLAIM
) < 0) {
1324 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1326 drv
->mode
= IEEE1284_MODE_COMPAT
;
1328 chr
= qemu_mallocz(sizeof(CharDriverState
));
1329 chr
->chr_write
= null_chr_write
;
1330 chr
->chr_ioctl
= pp_ioctl
;
1331 chr
->chr_close
= pp_close
;
1334 qemu_chr_generic_open(chr
);
1338 #endif /* __linux__ */
1340 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1341 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1343 int fd
= (int)chr
->opaque
;
1347 case CHR_IOCTL_PP_READ_DATA
:
1348 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1350 *(uint8_t *)arg
= b
;
1352 case CHR_IOCTL_PP_WRITE_DATA
:
1353 b
= *(uint8_t *)arg
;
1354 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1357 case CHR_IOCTL_PP_READ_CONTROL
:
1358 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1360 *(uint8_t *)arg
= b
;
1362 case CHR_IOCTL_PP_WRITE_CONTROL
:
1363 b
= *(uint8_t *)arg
;
1364 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1367 case CHR_IOCTL_PP_READ_STATUS
:
1368 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1370 *(uint8_t *)arg
= b
;
1378 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1380 const char *filename
= qemu_opt_get(opts
, "path");
1381 CharDriverState
*chr
;
1384 fd
= open(filename
, O_RDWR
);
1388 chr
= qemu_mallocz(sizeof(CharDriverState
));
1389 chr
->opaque
= (void *)fd
;
1390 chr
->chr_write
= null_chr_write
;
1391 chr
->chr_ioctl
= pp_ioctl
;
1400 HANDLE hcom
, hrecv
, hsend
;
1401 OVERLAPPED orecv
, osend
;
1406 #define NSENDBUF 2048
1407 #define NRECVBUF 2048
1408 #define MAXCONNECT 1
1409 #define NTIMEOUT 5000
1411 static int win_chr_poll(void *opaque
);
1412 static int win_chr_pipe_poll(void *opaque
);
1414 static void win_chr_close(CharDriverState
*chr
)
1416 WinCharState
*s
= chr
->opaque
;
1419 CloseHandle(s
->hsend
);
1423 CloseHandle(s
->hrecv
);
1427 CloseHandle(s
->hcom
);
1431 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1433 qemu_del_polling_cb(win_chr_poll
, chr
);
1435 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1438 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1440 WinCharState
*s
= chr
->opaque
;
1442 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1447 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1449 fprintf(stderr
, "Failed CreateEvent\n");
1452 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1454 fprintf(stderr
, "Failed CreateEvent\n");
1458 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1459 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1460 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1461 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1466 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1467 fprintf(stderr
, "Failed SetupComm\n");
1471 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1472 size
= sizeof(COMMCONFIG
);
1473 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1474 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1475 CommConfigDialog(filename
, NULL
, &comcfg
);
1477 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1478 fprintf(stderr
, "Failed SetCommState\n");
1482 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1483 fprintf(stderr
, "Failed SetCommMask\n");
1487 cto
.ReadIntervalTimeout
= MAXDWORD
;
1488 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1489 fprintf(stderr
, "Failed SetCommTimeouts\n");
1493 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1494 fprintf(stderr
, "Failed ClearCommError\n");
1497 qemu_add_polling_cb(win_chr_poll
, chr
);
1505 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1507 WinCharState
*s
= chr
->opaque
;
1508 DWORD len
, ret
, size
, err
;
1511 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1512 s
->osend
.hEvent
= s
->hsend
;
1515 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1517 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1519 err
= GetLastError();
1520 if (err
== ERROR_IO_PENDING
) {
1521 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1539 static int win_chr_read_poll(CharDriverState
*chr
)
1541 WinCharState
*s
= chr
->opaque
;
1543 s
->max_size
= qemu_chr_can_read(chr
);
1547 static void win_chr_readfile(CharDriverState
*chr
)
1549 WinCharState
*s
= chr
->opaque
;
1551 uint8_t buf
[READ_BUF_LEN
];
1554 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1555 s
->orecv
.hEvent
= s
->hrecv
;
1556 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1558 err
= GetLastError();
1559 if (err
== ERROR_IO_PENDING
) {
1560 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1565 qemu_chr_read(chr
, buf
, size
);
1569 static void win_chr_read(CharDriverState
*chr
)
1571 WinCharState
*s
= chr
->opaque
;
1573 if (s
->len
> s
->max_size
)
1574 s
->len
= s
->max_size
;
1578 win_chr_readfile(chr
);
1581 static int win_chr_poll(void *opaque
)
1583 CharDriverState
*chr
= opaque
;
1584 WinCharState
*s
= chr
->opaque
;
1588 ClearCommError(s
->hcom
, &comerr
, &status
);
1589 if (status
.cbInQue
> 0) {
1590 s
->len
= status
.cbInQue
;
1591 win_chr_read_poll(chr
);
1598 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1600 const char *filename
= qemu_opt_get(opts
, "path");
1601 CharDriverState
*chr
;
1604 chr
= qemu_mallocz(sizeof(CharDriverState
));
1605 s
= qemu_mallocz(sizeof(WinCharState
));
1607 chr
->chr_write
= win_chr_write
;
1608 chr
->chr_close
= win_chr_close
;
1610 if (win_chr_init(chr
, filename
) < 0) {
1615 qemu_chr_generic_open(chr
);
1619 static int win_chr_pipe_poll(void *opaque
)
1621 CharDriverState
*chr
= opaque
;
1622 WinCharState
*s
= chr
->opaque
;
1625 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1628 win_chr_read_poll(chr
);
1635 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1637 WinCharState
*s
= chr
->opaque
;
1645 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1647 fprintf(stderr
, "Failed CreateEvent\n");
1650 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1652 fprintf(stderr
, "Failed CreateEvent\n");
1656 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1657 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1658 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1660 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1661 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1662 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1667 ZeroMemory(&ov
, sizeof(ov
));
1668 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1669 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1671 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1675 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1677 fprintf(stderr
, "Failed GetOverlappedResult\n");
1679 CloseHandle(ov
.hEvent
);
1686 CloseHandle(ov
.hEvent
);
1689 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1698 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1700 const char *filename
= qemu_opt_get(opts
, "path");
1701 CharDriverState
*chr
;
1704 chr
= qemu_mallocz(sizeof(CharDriverState
));
1705 s
= qemu_mallocz(sizeof(WinCharState
));
1707 chr
->chr_write
= win_chr_write
;
1708 chr
->chr_close
= win_chr_close
;
1710 if (win_chr_pipe_init(chr
, filename
) < 0) {
1715 qemu_chr_generic_open(chr
);
1719 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1721 CharDriverState
*chr
;
1724 chr
= qemu_mallocz(sizeof(CharDriverState
));
1725 s
= qemu_mallocz(sizeof(WinCharState
));
1728 chr
->chr_write
= win_chr_write
;
1729 qemu_chr_generic_open(chr
);
1733 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1735 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1738 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1740 const char *file_out
= qemu_opt_get(opts
, "path");
1743 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1744 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1745 if (fd_out
== INVALID_HANDLE_VALUE
)
1748 return qemu_chr_open_win_file(fd_out
);
1750 #endif /* !_WIN32 */
1752 /***********************************************************/
1753 /* UDP Net console */
1757 uint8_t buf
[READ_BUF_LEN
];
1763 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1765 NetCharDriver
*s
= chr
->opaque
;
1767 return send(s
->fd
, (const void *)buf
, len
, 0);
1770 static int udp_chr_read_poll(void *opaque
)
1772 CharDriverState
*chr
= opaque
;
1773 NetCharDriver
*s
= chr
->opaque
;
1775 s
->max_size
= qemu_chr_can_read(chr
);
1777 /* If there were any stray characters in the queue process them
1780 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1781 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1783 s
->max_size
= qemu_chr_can_read(chr
);
1788 static void udp_chr_read(void *opaque
)
1790 CharDriverState
*chr
= opaque
;
1791 NetCharDriver
*s
= chr
->opaque
;
1793 if (s
->max_size
== 0)
1795 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1796 s
->bufptr
= s
->bufcnt
;
1801 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1802 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1804 s
->max_size
= qemu_chr_can_read(chr
);
1808 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1810 NetCharDriver
*s
= chr
->opaque
;
1813 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1814 udp_chr_read
, NULL
, chr
);
1818 static void udp_chr_close(CharDriverState
*chr
)
1820 NetCharDriver
*s
= chr
->opaque
;
1822 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1826 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1829 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1831 CharDriverState
*chr
= NULL
;
1832 NetCharDriver
*s
= NULL
;
1835 chr
= qemu_mallocz(sizeof(CharDriverState
));
1836 s
= qemu_mallocz(sizeof(NetCharDriver
));
1838 fd
= inet_dgram_opts(opts
);
1840 fprintf(stderr
, "inet_dgram_opts failed\n");
1848 chr
->chr_write
= udp_chr_write
;
1849 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1850 chr
->chr_close
= udp_chr_close
;
1863 /***********************************************************/
1864 /* TCP Net console */
1876 static void tcp_chr_accept(void *opaque
);
1878 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1880 TCPCharDriver
*s
= chr
->opaque
;
1882 return send_all(s
->fd
, buf
, len
);
1884 /* XXX: indicate an error ? */
1889 static int tcp_chr_read_poll(void *opaque
)
1891 CharDriverState
*chr
= opaque
;
1892 TCPCharDriver
*s
= chr
->opaque
;
1895 s
->max_size
= qemu_chr_can_read(chr
);
1900 #define IAC_BREAK 243
1901 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1903 uint8_t *buf
, int *size
)
1905 /* Handle any telnet client's basic IAC options to satisfy char by
1906 * char mode with no echo. All IAC options will be removed from
1907 * the buf and the do_telnetopt variable will be used to track the
1908 * state of the width of the IAC information.
1910 * IAC commands come in sets of 3 bytes with the exception of the
1911 * "IAC BREAK" command and the double IAC.
1917 for (i
= 0; i
< *size
; i
++) {
1918 if (s
->do_telnetopt
> 1) {
1919 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1920 /* Double IAC means send an IAC */
1924 s
->do_telnetopt
= 1;
1926 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1927 /* Handle IAC break commands by sending a serial break */
1928 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1933 if (s
->do_telnetopt
>= 4) {
1934 s
->do_telnetopt
= 1;
1937 if ((unsigned char)buf
[i
] == IAC
) {
1938 s
->do_telnetopt
= 2;
1949 static int tcp_get_msgfd(CharDriverState
*chr
)
1951 TCPCharDriver
*s
= chr
->opaque
;
1957 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1959 TCPCharDriver
*s
= chr
->opaque
;
1960 struct cmsghdr
*cmsg
;
1962 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1965 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1966 cmsg
->cmsg_level
!= SOL_SOCKET
||
1967 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1970 fd
= *((int *)CMSG_DATA(cmsg
));
1980 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1982 TCPCharDriver
*s
= chr
->opaque
;
1983 struct msghdr msg
= { NULL
, };
1984 struct iovec iov
[1];
1986 struct cmsghdr cmsg
;
1987 char control
[CMSG_SPACE(sizeof(int))];
1991 iov
[0].iov_base
= buf
;
1992 iov
[0].iov_len
= len
;
1996 msg
.msg_control
= &msg_control
;
1997 msg
.msg_controllen
= sizeof(msg_control
);
1999 ret
= recvmsg(s
->fd
, &msg
, 0);
2000 if (ret
> 0 && s
->is_unix
)
2001 unix_process_msgfd(chr
, &msg
);
2006 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
2008 TCPCharDriver
*s
= chr
->opaque
;
2009 return recv(s
->fd
, buf
, len
, 0);
2013 static void tcp_chr_read(void *opaque
)
2015 CharDriverState
*chr
= opaque
;
2016 TCPCharDriver
*s
= chr
->opaque
;
2017 uint8_t buf
[READ_BUF_LEN
];
2020 if (!s
->connected
|| s
->max_size
<= 0)
2023 if (len
> s
->max_size
)
2025 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2027 /* connection closed */
2029 if (s
->listen_fd
>= 0) {
2030 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2032 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2035 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2036 } else if (size
> 0) {
2037 if (s
->do_telnetopt
)
2038 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2040 qemu_chr_read(chr
, buf
, size
);
2041 if (s
->msgfd
!= -1) {
2048 static void tcp_chr_connect(void *opaque
)
2050 CharDriverState
*chr
= opaque
;
2051 TCPCharDriver
*s
= chr
->opaque
;
2054 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2055 tcp_chr_read
, NULL
, chr
);
2056 qemu_chr_generic_open(chr
);
2059 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2060 static void tcp_chr_telnet_init(int fd
)
2063 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2064 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2065 send(fd
, (char *)buf
, 3, 0);
2066 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2067 send(fd
, (char *)buf
, 3, 0);
2068 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2069 send(fd
, (char *)buf
, 3, 0);
2070 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2071 send(fd
, (char *)buf
, 3, 0);
2074 static void socket_set_nodelay(int fd
)
2077 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2080 static void tcp_chr_accept(void *opaque
)
2082 CharDriverState
*chr
= opaque
;
2083 TCPCharDriver
*s
= chr
->opaque
;
2084 struct sockaddr_in saddr
;
2086 struct sockaddr_un uaddr
;
2088 struct sockaddr
*addr
;
2095 len
= sizeof(uaddr
);
2096 addr
= (struct sockaddr
*)&uaddr
;
2100 len
= sizeof(saddr
);
2101 addr
= (struct sockaddr
*)&saddr
;
2103 fd
= accept(s
->listen_fd
, addr
, &len
);
2104 if (fd
< 0 && errno
!= EINTR
) {
2106 } else if (fd
>= 0) {
2107 if (s
->do_telnetopt
)
2108 tcp_chr_telnet_init(fd
);
2112 socket_set_nonblock(fd
);
2114 socket_set_nodelay(fd
);
2116 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2117 tcp_chr_connect(chr
);
2120 static void tcp_chr_close(CharDriverState
*chr
)
2122 TCPCharDriver
*s
= chr
->opaque
;
2124 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2127 if (s
->listen_fd
>= 0) {
2128 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2129 closesocket(s
->listen_fd
);
2132 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2135 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2137 CharDriverState
*chr
= NULL
;
2138 TCPCharDriver
*s
= NULL
;
2146 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2147 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2148 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2149 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2150 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2154 chr
= qemu_mallocz(sizeof(CharDriverState
));
2155 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2159 fd
= unix_listen_opts(opts
);
2161 fd
= unix_connect_opts(opts
);
2165 fd
= inet_listen_opts(opts
, 0);
2167 fd
= inet_connect_opts(opts
);
2173 if (!is_waitconnect
)
2174 socket_set_nonblock(fd
);
2180 s
->is_unix
= is_unix
;
2181 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2184 chr
->chr_write
= tcp_chr_write
;
2185 chr
->chr_close
= tcp_chr_close
;
2186 chr
->get_msgfd
= tcp_get_msgfd
;
2190 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2192 s
->do_telnetopt
= 1;
2197 socket_set_nodelay(fd
);
2198 tcp_chr_connect(chr
);
2201 /* for "info chardev" monitor command */
2202 chr
->filename
= qemu_malloc(256);
2204 snprintf(chr
->filename
, 256, "unix:%s%s",
2205 qemu_opt_get(opts
, "path"),
2206 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2207 } else if (is_telnet
) {
2208 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2209 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2210 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2212 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2213 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2214 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2217 if (is_listen
&& is_waitconnect
) {
2218 printf("QEMU waiting for connection on: %s\n",
2220 tcp_chr_accept(chr
);
2221 socket_set_nonblock(s
->listen_fd
);
2233 static QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2235 char host
[65], port
[33], width
[8], height
[8];
2240 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2244 if (strstart(filename
, "mon:", &p
)) {
2246 qemu_opt_set(opts
, "mux", "on");
2249 if (strcmp(filename
, "null") == 0 ||
2250 strcmp(filename
, "pty") == 0 ||
2251 strcmp(filename
, "msmouse") == 0 ||
2252 strcmp(filename
, "braille") == 0 ||
2253 strcmp(filename
, "stdio") == 0) {
2254 qemu_opt_set(opts
, "backend", filename
);
2257 if (strstart(filename
, "vc", &p
)) {
2258 qemu_opt_set(opts
, "backend", "vc");
2260 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2262 qemu_opt_set(opts
, "width", width
);
2263 qemu_opt_set(opts
, "height", height
);
2264 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2266 qemu_opt_set(opts
, "cols", width
);
2267 qemu_opt_set(opts
, "rows", height
);
2274 if (strcmp(filename
, "con:") == 0) {
2275 qemu_opt_set(opts
, "backend", "console");
2278 if (strstart(filename
, "COM", NULL
)) {
2279 qemu_opt_set(opts
, "backend", "serial");
2280 qemu_opt_set(opts
, "path", filename
);
2283 if (strstart(filename
, "file:", &p
)) {
2284 qemu_opt_set(opts
, "backend", "file");
2285 qemu_opt_set(opts
, "path", p
);
2288 if (strstart(filename
, "pipe:", &p
)) {
2289 qemu_opt_set(opts
, "backend", "pipe");
2290 qemu_opt_set(opts
, "path", p
);
2293 if (strstart(filename
, "tcp:", &p
) ||
2294 strstart(filename
, "telnet:", &p
)) {
2295 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2297 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2300 qemu_opt_set(opts
, "backend", "socket");
2301 qemu_opt_set(opts
, "host", host
);
2302 qemu_opt_set(opts
, "port", port
);
2303 if (p
[pos
] == ',') {
2304 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2307 if (strstart(filename
, "telnet:", &p
))
2308 qemu_opt_set(opts
, "telnet", "on");
2311 if (strstart(filename
, "udp:", &p
)) {
2312 qemu_opt_set(opts
, "backend", "udp");
2313 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2315 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2316 fprintf(stderr
, "udp #1\n");
2320 qemu_opt_set(opts
, "host", host
);
2321 qemu_opt_set(opts
, "port", port
);
2322 if (p
[pos
] == '@') {
2324 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2326 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2327 fprintf(stderr
, "udp #2\n");
2331 qemu_opt_set(opts
, "localaddr", host
);
2332 qemu_opt_set(opts
, "localport", port
);
2336 if (strstart(filename
, "unix:", &p
)) {
2337 qemu_opt_set(opts
, "backend", "socket");
2338 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2342 if (strstart(filename
, "/dev/parport", NULL
) ||
2343 strstart(filename
, "/dev/ppi", NULL
)) {
2344 qemu_opt_set(opts
, "backend", "parport");
2345 qemu_opt_set(opts
, "path", filename
);
2348 if (strstart(filename
, "/dev/", NULL
)) {
2349 qemu_opt_set(opts
, "backend", "tty");
2350 qemu_opt_set(opts
, "path", filename
);
2355 fprintf(stderr
, "%s: fail on \"%s\"\n", __FUNCTION__
, filename
);
2356 qemu_opts_del(opts
);
2360 static const struct {
2362 CharDriverState
*(*open
)(QemuOpts
*opts
);
2363 } backend_table
[] = {
2364 { .name
= "null", .open
= qemu_chr_open_null
},
2365 { .name
= "socket", .open
= qemu_chr_open_socket
},
2366 { .name
= "udp", .open
= qemu_chr_open_udp
},
2367 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2368 { .name
= "vc", .open
= text_console_init
},
2370 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2371 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2372 { .name
= "console", .open
= qemu_chr_open_win_con
},
2373 { .name
= "serial", .open
= qemu_chr_open_win
},
2375 { .name
= "file", .open
= qemu_chr_open_file_out
},
2376 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2377 { .name
= "pty", .open
= qemu_chr_open_pty
},
2378 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2380 #ifdef CONFIG_BRLAPI
2381 { .name
= "braille", .open
= chr_baum_init
},
2383 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2384 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2385 || defined(__FreeBSD_kernel__)
2386 { .name
= "tty", .open
= qemu_chr_open_tty
},
2388 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2389 || defined(__FreeBSD_kernel__)
2390 { .name
= "parport", .open
= qemu_chr_open_pp
},
2394 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2395 void (*init
)(struct CharDriverState
*s
))
2397 CharDriverState
*chr
;
2400 if (qemu_opts_id(opts
) == NULL
) {
2401 fprintf(stderr
, "chardev: no id specified\n");
2405 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2406 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2409 if (i
== ARRAY_SIZE(backend_table
)) {
2410 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2411 qemu_opt_get(opts
, "backend"));
2415 chr
= backend_table
[i
].open(opts
);
2417 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2418 qemu_opt_get(opts
, "backend"));
2423 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2425 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2427 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2428 CharDriverState
*base
= chr
;
2429 int len
= strlen(qemu_opts_id(opts
)) + 6;
2430 base
->label
= qemu_malloc(len
);
2431 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2432 chr
= qemu_chr_open_mux(base
);
2433 chr
->filename
= base
->filename
;
2434 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2436 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2440 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2443 CharDriverState
*chr
;
2446 if (strstart(filename
, "chardev:", &p
)) {
2447 return qemu_chr_find(p
);
2450 opts
= qemu_chr_parse_compat(label
, filename
);
2454 chr
= qemu_chr_open_opts(opts
, init
);
2455 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2456 monitor_init(chr
, MONITOR_USE_READLINE
);
2461 void qemu_chr_close(CharDriverState
*chr
)
2463 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2465 chr
->chr_close(chr
);
2466 qemu_free(chr
->filename
);
2467 qemu_free(chr
->label
);
2471 void qemu_chr_info(Monitor
*mon
)
2473 CharDriverState
*chr
;
2475 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2476 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);
2480 CharDriverState
*qemu_chr_find(const char *name
)
2482 CharDriverState
*chr
;
2484 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2485 if (strcmp(chr
->label
, name
) != 0)