4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
34 #include "hw/msmouse.h"
45 #include <sys/times.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
55 #include <net/if_tap.h>
58 #include <linux/if_tun.h>
60 #include <arpa/inet.h>
63 #include <sys/select.h>
68 #include <dev/ppbus/ppi.h>
69 #include <dev/ppbus/ppbconf.h>
70 #elif defined(__DragonFly__)
72 #include <dev/misc/ppi/ppi.h>
73 #include <bus/ppbus/ppbconf.h>
77 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
78 #include <freebsd/stdlib.h>
83 #include <linux/ppdev.h>
84 #include <linux/parport.h>
88 #include <sys/ethernet.h>
89 #include <sys/sockio.h>
90 #include <netinet/arp.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_icmp.h> // must come after ip.h
95 #include <netinet/udp.h>
96 #include <netinet/tcp.h>
104 #include "qemu_socket.h"
106 /***********************************************************/
107 /* character device */
109 static QTAILQ_HEAD(CharDriverStateHead
, CharDriverState
) chardevs
=
110 QTAILQ_HEAD_INITIALIZER(chardevs
);
112 static void qemu_chr_event(CharDriverState
*s
, int event
)
116 s
->chr_event(s
->handler_opaque
, event
);
119 static void qemu_chr_reset_bh(void *opaque
)
121 CharDriverState
*s
= opaque
;
122 qemu_chr_event(s
, CHR_EVENT_RESET
);
123 qemu_bh_delete(s
->bh
);
127 void qemu_chr_reset(CharDriverState
*s
)
130 s
->bh
= qemu_bh_new(qemu_chr_reset_bh
, s
);
131 qemu_bh_schedule(s
->bh
);
135 void qemu_chr_initial_reset(void)
137 CharDriverState
*chr
;
139 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
144 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
146 return s
->chr_write(s
, buf
, len
);
149 int qemu_chr_ioctl(CharDriverState
*s
, int cmd
, void *arg
)
153 return s
->chr_ioctl(s
, cmd
, arg
);
156 int qemu_chr_can_read(CharDriverState
*s
)
158 if (!s
->chr_can_read
)
160 return s
->chr_can_read(s
->handler_opaque
);
163 void qemu_chr_read(CharDriverState
*s
, uint8_t *buf
, int len
)
165 s
->chr_read(s
->handler_opaque
, buf
, len
);
168 int qemu_chr_get_msgfd(CharDriverState
*s
)
170 return s
->get_msgfd
? s
->get_msgfd(s
) : -1;
173 void qemu_chr_accept_input(CharDriverState
*s
)
175 if (s
->chr_accept_input
)
176 s
->chr_accept_input(s
);
179 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
184 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
185 qemu_chr_write(s
, (uint8_t *)buf
, strlen(buf
));
189 void qemu_chr_send_event(CharDriverState
*s
, int event
)
191 if (s
->chr_send_event
)
192 s
->chr_send_event(s
, event
);
195 void qemu_chr_add_handlers(CharDriverState
*s
,
196 IOCanRWHandler
*fd_can_read
,
197 IOReadHandler
*fd_read
,
198 IOEventHandler
*fd_event
,
201 s
->chr_can_read
= fd_can_read
;
202 s
->chr_read
= fd_read
;
203 s
->chr_event
= fd_event
;
204 s
->handler_opaque
= opaque
;
205 if (s
->chr_update_read_handler
)
206 s
->chr_update_read_handler(s
);
209 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
214 static CharDriverState
*qemu_chr_open_null(QemuOpts
*opts
)
216 CharDriverState
*chr
;
218 chr
= qemu_mallocz(sizeof(CharDriverState
));
219 chr
->chr_write
= null_chr_write
;
223 /* MUX driver for serial I/O splitting */
225 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
226 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
228 IOCanRWHandler
*chr_can_read
[MAX_MUX
];
229 IOReadHandler
*chr_read
[MAX_MUX
];
230 IOEventHandler
*chr_event
[MAX_MUX
];
231 void *ext_opaque
[MAX_MUX
];
232 CharDriverState
*drv
;
237 /* Intermediate input buffer allows to catch escape sequences even if the
238 currently active device is not accepting any input - but only until it
240 unsigned char buffer
[MAX_MUX
][MUX_BUFFER_SIZE
];
245 int64_t timestamps_start
;
249 static int mux_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
251 MuxDriver
*d
= chr
->opaque
;
253 if (!d
->timestamps
) {
254 ret
= d
->drv
->chr_write(d
->drv
, buf
, len
);
259 for (i
= 0; i
< len
; i
++) {
265 ti
= qemu_get_clock(rt_clock
);
266 if (d
->timestamps_start
== -1)
267 d
->timestamps_start
= ti
;
268 ti
-= d
->timestamps_start
;
270 snprintf(buf1
, sizeof(buf1
),
271 "[%02d:%02d:%02d.%03d] ",
276 d
->drv
->chr_write(d
->drv
, (uint8_t *)buf1
, strlen(buf1
));
279 ret
+= d
->drv
->chr_write(d
->drv
, buf
+i
, 1);
280 if (buf
[i
] == '\n') {
288 static const char * const mux_help
[] = {
289 "% h print this help\n\r",
290 "% x exit emulator\n\r",
291 "% s save disk data back to file (if -snapshot)\n\r",
292 "% t toggle console timestamps\n\r"
293 "% b send break (magic sysrq)\n\r",
294 "% c switch between console and monitor\n\r",
299 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
300 static void mux_print_help(CharDriverState
*chr
)
303 char ebuf
[15] = "Escape-Char";
304 char cbuf
[50] = "\n\r";
306 if (term_escape_char
> 0 && term_escape_char
< 26) {
307 snprintf(cbuf
, sizeof(cbuf
), "\n\r");
308 snprintf(ebuf
, sizeof(ebuf
), "C-%c", term_escape_char
- 1 + 'a');
310 snprintf(cbuf
, sizeof(cbuf
),
311 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
314 chr
->chr_write(chr
, (uint8_t *)cbuf
, strlen(cbuf
));
315 for (i
= 0; mux_help
[i
] != NULL
; i
++) {
316 for (j
=0; mux_help
[i
][j
] != '\0'; j
++) {
317 if (mux_help
[i
][j
] == '%')
318 chr
->chr_write(chr
, (uint8_t *)ebuf
, strlen(ebuf
));
320 chr
->chr_write(chr
, (uint8_t *)&mux_help
[i
][j
], 1);
325 static void mux_chr_send_event(MuxDriver
*d
, int mux_nr
, int event
)
327 if (d
->chr_event
[mux_nr
])
328 d
->chr_event
[mux_nr
](d
->ext_opaque
[mux_nr
], event
);
331 static int mux_proc_byte(CharDriverState
*chr
, MuxDriver
*d
, int ch
)
333 if (d
->term_got_escape
) {
334 d
->term_got_escape
= 0;
335 if (ch
== term_escape_char
)
344 const char *term
= "QEMU: Terminated\n\r";
345 chr
->chr_write(chr
,(uint8_t *)term
,strlen(term
));
352 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
353 bdrv_commit(dinfo
->bdrv
);
358 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
361 /* Switch to the next registered device */
362 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
364 if (d
->focus
>= d
->mux_cnt
)
366 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
369 d
->timestamps
= !d
->timestamps
;
370 d
->timestamps_start
= -1;
374 } else if (ch
== term_escape_char
) {
375 d
->term_got_escape
= 1;
383 static void mux_chr_accept_input(CharDriverState
*chr
)
385 MuxDriver
*d
= chr
->opaque
;
388 while (d
->prod
[m
] != d
->cons
[m
] &&
389 d
->chr_can_read
[m
] &&
390 d
->chr_can_read
[m
](d
->ext_opaque
[m
])) {
391 d
->chr_read
[m
](d
->ext_opaque
[m
],
392 &d
->buffer
[m
][d
->cons
[m
]++ & MUX_BUFFER_MASK
], 1);
396 static int mux_chr_can_read(void *opaque
)
398 CharDriverState
*chr
= opaque
;
399 MuxDriver
*d
= chr
->opaque
;
402 if ((d
->prod
[m
] - d
->cons
[m
]) < MUX_BUFFER_SIZE
)
404 if (d
->chr_can_read
[m
])
405 return d
->chr_can_read
[m
](d
->ext_opaque
[m
]);
409 static void mux_chr_read(void *opaque
, const uint8_t *buf
, int size
)
411 CharDriverState
*chr
= opaque
;
412 MuxDriver
*d
= chr
->opaque
;
416 mux_chr_accept_input (opaque
);
418 for(i
= 0; i
< size
; i
++)
419 if (mux_proc_byte(chr
, d
, buf
[i
])) {
420 if (d
->prod
[m
] == d
->cons
[m
] &&
421 d
->chr_can_read
[m
] &&
422 d
->chr_can_read
[m
](d
->ext_opaque
[m
]))
423 d
->chr_read
[m
](d
->ext_opaque
[m
], &buf
[i
], 1);
425 d
->buffer
[m
][d
->prod
[m
]++ & MUX_BUFFER_MASK
] = buf
[i
];
429 static void mux_chr_event(void *opaque
, int event
)
431 CharDriverState
*chr
= opaque
;
432 MuxDriver
*d
= chr
->opaque
;
435 /* Send the event to all registered listeners */
436 for (i
= 0; i
< d
->mux_cnt
; i
++)
437 mux_chr_send_event(d
, i
, event
);
440 static void mux_chr_update_read_handler(CharDriverState
*chr
)
442 MuxDriver
*d
= chr
->opaque
;
444 if (d
->mux_cnt
>= MAX_MUX
) {
445 fprintf(stderr
, "Cannot add I/O handlers, MUX array is full\n");
448 d
->ext_opaque
[d
->mux_cnt
] = chr
->handler_opaque
;
449 d
->chr_can_read
[d
->mux_cnt
] = chr
->chr_can_read
;
450 d
->chr_read
[d
->mux_cnt
] = chr
->chr_read
;
451 d
->chr_event
[d
->mux_cnt
] = chr
->chr_event
;
452 /* Fix up the real driver with mux routines */
453 if (d
->mux_cnt
== 0) {
454 qemu_chr_add_handlers(d
->drv
, mux_chr_can_read
, mux_chr_read
,
457 if (d
->focus
!= -1) {
458 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_OUT
);
460 d
->focus
= d
->mux_cnt
;
462 mux_chr_send_event(d
, d
->focus
, CHR_EVENT_MUX_IN
);
465 static CharDriverState
*qemu_chr_open_mux(CharDriverState
*drv
)
467 CharDriverState
*chr
;
470 chr
= qemu_mallocz(sizeof(CharDriverState
));
471 d
= qemu_mallocz(sizeof(MuxDriver
));
476 chr
->chr_write
= mux_chr_write
;
477 chr
->chr_update_read_handler
= mux_chr_update_read_handler
;
478 chr
->chr_accept_input
= mux_chr_accept_input
;
484 int send_all(int fd
, const void *buf
, int len1
)
490 ret
= send(fd
, buf
, len
, 0);
492 errno
= WSAGetLastError();
493 if (errno
!= WSAEWOULDBLOCK
) {
496 } else if (ret
== 0) {
508 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
514 ret
= write(fd
, buf
, len
);
516 if (errno
!= EINTR
&& errno
!= EAGAIN
)
518 } else if (ret
== 0) {
528 int send_all(int fd
, const void *buf
, int len1
)
530 return unix_write(fd
, buf
, len1
);
541 #define STDIO_MAX_CLIENTS 1
542 static int stdio_nb_clients
= 0;
544 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
546 FDCharDriver
*s
= chr
->opaque
;
547 return send_all(s
->fd_out
, buf
, len
);
550 static int fd_chr_read_poll(void *opaque
)
552 CharDriverState
*chr
= opaque
;
553 FDCharDriver
*s
= chr
->opaque
;
555 s
->max_size
= qemu_chr_can_read(chr
);
559 static void fd_chr_read(void *opaque
)
561 CharDriverState
*chr
= opaque
;
562 FDCharDriver
*s
= chr
->opaque
;
567 if (len
> s
->max_size
)
571 size
= read(s
->fd_in
, buf
, len
);
573 /* FD has been closed. Remove it from the active list. */
574 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
575 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
579 qemu_chr_read(chr
, buf
, size
);
583 static void fd_chr_update_read_handler(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
, fd_chr_read_poll
,
591 fd_chr_read
, NULL
, chr
);
596 static void fd_chr_close(struct CharDriverState
*chr
)
598 FDCharDriver
*s
= chr
->opaque
;
601 if (display_type
== DT_NOGRAPHIC
&& s
->fd_in
== 0) {
603 qemu_set_fd_handler2(s
->fd_in
, NULL
, NULL
, NULL
, NULL
);
608 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
611 /* open a character device to a unix fd */
612 static CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
614 CharDriverState
*chr
;
617 chr
= qemu_mallocz(sizeof(CharDriverState
));
618 s
= qemu_mallocz(sizeof(FDCharDriver
));
622 chr
->chr_write
= fd_chr_write
;
623 chr
->chr_update_read_handler
= fd_chr_update_read_handler
;
624 chr
->chr_close
= fd_chr_close
;
631 static CharDriverState
*qemu_chr_open_file_out(QemuOpts
*opts
)
635 TFR(fd_out
= open(qemu_opt_get(opts
, "path"),
636 O_WRONLY
| O_TRUNC
| O_CREAT
| O_BINARY
, 0666));
639 return qemu_chr_open_fd(-1, fd_out
);
642 static CharDriverState
*qemu_chr_open_pipe(QemuOpts
*opts
)
645 char filename_in
[256], filename_out
[256];
646 const char *filename
= qemu_opt_get(opts
, "path");
648 if (filename
== NULL
) {
649 fprintf(stderr
, "chardev: pipe: no filename given\n");
653 snprintf(filename_in
, 256, "%s.in", filename
);
654 snprintf(filename_out
, 256, "%s.out", filename
);
655 TFR(fd_in
= open(filename_in
, O_RDWR
| O_BINARY
));
656 TFR(fd_out
= open(filename_out
, O_RDWR
| O_BINARY
));
657 if (fd_in
< 0 || fd_out
< 0) {
662 TFR(fd_in
= fd_out
= open(filename
, O_RDWR
| O_BINARY
));
666 return qemu_chr_open_fd(fd_in
, fd_out
);
670 /* for STDIO, we handle the case where several clients use it
673 #define TERM_FIFO_MAX_SIZE 1
675 static uint8_t term_fifo
[TERM_FIFO_MAX_SIZE
];
676 static int term_fifo_size
;
678 static int stdio_read_poll(void *opaque
)
680 CharDriverState
*chr
= opaque
;
682 /* try to flush the queue if needed */
683 if (term_fifo_size
!= 0 && qemu_chr_can_read(chr
) > 0) {
684 qemu_chr_read(chr
, term_fifo
, 1);
687 /* see if we can absorb more chars */
688 if (term_fifo_size
== 0)
694 static void stdio_read(void *opaque
)
698 CharDriverState
*chr
= opaque
;
700 size
= read(0, buf
, 1);
702 /* stdin has been closed. Remove it from the active list. */
703 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
704 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
708 if (qemu_chr_can_read(chr
) > 0) {
709 qemu_chr_read(chr
, buf
, 1);
710 } else if (term_fifo_size
== 0) {
711 term_fifo
[term_fifo_size
++] = buf
[0];
716 /* init terminal so that we can grab keys */
717 static struct termios oldtty
;
718 static int old_fd0_flags
;
719 static int term_atexit_done
;
721 static void term_exit(void)
723 tcsetattr (0, TCSANOW
, &oldtty
);
724 fcntl(0, F_SETFL
, old_fd0_flags
);
727 static void term_init(void)
733 old_fd0_flags
= fcntl(0, F_GETFL
);
735 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
736 |INLCR
|IGNCR
|ICRNL
|IXON
);
737 tty
.c_oflag
|= OPOST
;
738 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
739 /* if graphical mode, we allow Ctrl-C handling */
740 if (display_type
== DT_NOGRAPHIC
)
741 tty
.c_lflag
&= ~ISIG
;
742 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
747 tcsetattr (0, TCSANOW
, &tty
);
749 if (!term_atexit_done
++)
752 fcntl(0, F_SETFL
, O_NONBLOCK
);
755 static void qemu_chr_close_stdio(struct CharDriverState
*chr
)
759 qemu_set_fd_handler2(0, NULL
, NULL
, NULL
, NULL
);
763 static CharDriverState
*qemu_chr_open_stdio(QemuOpts
*opts
)
765 CharDriverState
*chr
;
767 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
769 chr
= qemu_chr_open_fd(0, 1);
770 chr
->chr_close
= qemu_chr_close_stdio
;
771 qemu_set_fd_handler2(0, stdio_read_poll
, stdio_read
, NULL
, chr
);
779 /* Once Solaris has openpty(), this is going to be removed. */
780 static int openpty(int *amaster
, int *aslave
, char *name
,
781 struct termios
*termp
, struct winsize
*winp
)
784 int mfd
= -1, sfd
= -1;
786 *amaster
= *aslave
= -1;
788 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
792 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1)
795 if ((slave
= ptsname(mfd
)) == NULL
)
798 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1)
801 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
802 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0))
810 ioctl(sfd
, TIOCSWINSZ
, winp
);
821 static void cfmakeraw (struct termios
*termios_p
)
823 termios_p
->c_iflag
&=
824 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
825 termios_p
->c_oflag
&= ~OPOST
;
826 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
827 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
828 termios_p
->c_cflag
|= CS8
;
830 termios_p
->c_cc
[VMIN
] = 0;
831 termios_p
->c_cc
[VTIME
] = 0;
835 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
836 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
846 static void pty_chr_update_read_handler(CharDriverState
*chr
);
847 static void pty_chr_state(CharDriverState
*chr
, int connected
);
849 static int pty_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
851 PtyCharDriver
*s
= chr
->opaque
;
854 /* guest sends data, check for (re-)connect */
855 pty_chr_update_read_handler(chr
);
858 return send_all(s
->fd
, buf
, len
);
861 static int pty_chr_read_poll(void *opaque
)
863 CharDriverState
*chr
= opaque
;
864 PtyCharDriver
*s
= chr
->opaque
;
866 s
->read_bytes
= qemu_chr_can_read(chr
);
867 return s
->read_bytes
;
870 static void pty_chr_read(void *opaque
)
872 CharDriverState
*chr
= opaque
;
873 PtyCharDriver
*s
= chr
->opaque
;
878 if (len
> s
->read_bytes
)
882 size
= read(s
->fd
, buf
, len
);
883 if ((size
== -1 && errno
== EIO
) ||
885 pty_chr_state(chr
, 0);
889 pty_chr_state(chr
, 1);
890 qemu_chr_read(chr
, buf
, size
);
894 static void pty_chr_update_read_handler(CharDriverState
*chr
)
896 PtyCharDriver
*s
= chr
->opaque
;
898 qemu_set_fd_handler2(s
->fd
, pty_chr_read_poll
,
899 pty_chr_read
, NULL
, chr
);
902 * Short timeout here: just need wait long enougth that qemu makes
903 * it through the poll loop once. When reconnected we want a
904 * short timeout so we notice it almost instantly. Otherwise
905 * read() gives us -EIO instantly, making pty_chr_state() reset the
906 * timeout to the normal (much longer) poll interval before the
909 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 10);
912 static void pty_chr_state(CharDriverState
*chr
, int connected
)
914 PtyCharDriver
*s
= chr
->opaque
;
917 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
920 /* (re-)connect poll interval for idle guests: once per second.
921 * We check more frequently in case the guests sends data to
922 * the virtual device linked to our pty. */
923 qemu_mod_timer(s
->timer
, qemu_get_clock(rt_clock
) + 1000);
931 static void pty_chr_timer(void *opaque
)
933 struct CharDriverState
*chr
= opaque
;
934 PtyCharDriver
*s
= chr
->opaque
;
939 /* If we arrive here without polling being cleared due
940 * read returning -EIO, then we are (re-)connected */
941 pty_chr_state(chr
, 1);
946 pty_chr_update_read_handler(chr
);
949 static void pty_chr_close(struct CharDriverState
*chr
)
951 PtyCharDriver
*s
= chr
->opaque
;
953 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
955 qemu_del_timer(s
->timer
);
956 qemu_free_timer(s
->timer
);
958 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
961 static CharDriverState
*qemu_chr_open_pty(QemuOpts
*opts
)
963 CharDriverState
*chr
;
967 #if defined(__OpenBSD__) || defined(__DragonFly__)
968 char pty_name
[PATH_MAX
];
969 #define q_ptsname(x) pty_name
971 char *pty_name
= NULL
;
972 #define q_ptsname(x) ptsname(x)
975 chr
= qemu_mallocz(sizeof(CharDriverState
));
976 s
= qemu_mallocz(sizeof(PtyCharDriver
));
978 if (openpty(&s
->fd
, &slave_fd
, pty_name
, NULL
, NULL
) < 0) {
982 /* Set raw attributes on the pty. */
983 tcgetattr(slave_fd
, &tty
);
985 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
988 len
= strlen(q_ptsname(s
->fd
)) + 5;
989 chr
->filename
= qemu_malloc(len
);
990 snprintf(chr
->filename
, len
, "pty:%s", q_ptsname(s
->fd
));
991 qemu_opt_set(opts
, "path", q_ptsname(s
->fd
));
992 fprintf(stderr
, "char device redirected to %s\n", q_ptsname(s
->fd
));
995 chr
->chr_write
= pty_chr_write
;
996 chr
->chr_update_read_handler
= pty_chr_update_read_handler
;
997 chr
->chr_close
= pty_chr_close
;
999 s
->timer
= qemu_new_timer(rt_clock
, pty_chr_timer
, chr
);
1004 static void tty_serial_init(int fd
, int speed
,
1005 int parity
, int data_bits
, int stop_bits
)
1011 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1012 speed
, parity
, data_bits
, stop_bits
);
1014 tcgetattr (fd
, &tty
);
1017 if (speed
<= 50 * MARGIN
)
1019 else if (speed
<= 75 * MARGIN
)
1021 else if (speed
<= 300 * MARGIN
)
1023 else if (speed
<= 600 * MARGIN
)
1025 else if (speed
<= 1200 * MARGIN
)
1027 else if (speed
<= 2400 * MARGIN
)
1029 else if (speed
<= 4800 * MARGIN
)
1031 else if (speed
<= 9600 * MARGIN
)
1033 else if (speed
<= 19200 * MARGIN
)
1035 else if (speed
<= 38400 * MARGIN
)
1037 else if (speed
<= 57600 * MARGIN
)
1039 else if (speed
<= 115200 * MARGIN
)
1044 cfsetispeed(&tty
, spd
);
1045 cfsetospeed(&tty
, spd
);
1047 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1048 |INLCR
|IGNCR
|ICRNL
|IXON
);
1049 tty
.c_oflag
|= OPOST
;
1050 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
|ISIG
);
1051 tty
.c_cflag
&= ~(CSIZE
|PARENB
|PARODD
|CRTSCTS
|CSTOPB
);
1072 tty
.c_cflag
|= PARENB
;
1075 tty
.c_cflag
|= PARENB
| PARODD
;
1079 tty
.c_cflag
|= CSTOPB
;
1081 tcsetattr (fd
, TCSANOW
, &tty
);
1084 static int tty_serial_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1086 FDCharDriver
*s
= chr
->opaque
;
1089 case CHR_IOCTL_SERIAL_SET_PARAMS
:
1091 QEMUSerialSetParams
*ssp
= arg
;
1092 tty_serial_init(s
->fd_in
, ssp
->speed
, ssp
->parity
,
1093 ssp
->data_bits
, ssp
->stop_bits
);
1096 case CHR_IOCTL_SERIAL_SET_BREAK
:
1098 int enable
= *(int *)arg
;
1100 tcsendbreak(s
->fd_in
, 1);
1103 case CHR_IOCTL_SERIAL_GET_TIOCM
:
1106 int *targ
= (int *)arg
;
1107 ioctl(s
->fd_in
, TIOCMGET
, &sarg
);
1109 if (sarg
& TIOCM_CTS
)
1110 *targ
|= CHR_TIOCM_CTS
;
1111 if (sarg
& TIOCM_CAR
)
1112 *targ
|= CHR_TIOCM_CAR
;
1113 if (sarg
& TIOCM_DSR
)
1114 *targ
|= CHR_TIOCM_DSR
;
1115 if (sarg
& TIOCM_RI
)
1116 *targ
|= CHR_TIOCM_RI
;
1117 if (sarg
& TIOCM_DTR
)
1118 *targ
|= CHR_TIOCM_DTR
;
1119 if (sarg
& TIOCM_RTS
)
1120 *targ
|= CHR_TIOCM_RTS
;
1123 case CHR_IOCTL_SERIAL_SET_TIOCM
:
1125 int sarg
= *(int *)arg
;
1127 ioctl(s
->fd_in
, TIOCMGET
, &targ
);
1128 targ
&= ~(CHR_TIOCM_CTS
| CHR_TIOCM_CAR
| CHR_TIOCM_DSR
1129 | CHR_TIOCM_RI
| CHR_TIOCM_DTR
| CHR_TIOCM_RTS
);
1130 if (sarg
& CHR_TIOCM_CTS
)
1132 if (sarg
& CHR_TIOCM_CAR
)
1134 if (sarg
& CHR_TIOCM_DSR
)
1136 if (sarg
& CHR_TIOCM_RI
)
1138 if (sarg
& CHR_TIOCM_DTR
)
1140 if (sarg
& CHR_TIOCM_RTS
)
1142 ioctl(s
->fd_in
, TIOCMSET
, &targ
);
1151 static CharDriverState
*qemu_chr_open_tty(QemuOpts
*opts
)
1153 const char *filename
= qemu_opt_get(opts
, "path");
1154 CharDriverState
*chr
;
1157 TFR(fd
= open(filename
, O_RDWR
| O_NONBLOCK
));
1158 tty_serial_init(fd
, 115200, 'N', 8, 1);
1159 chr
= qemu_chr_open_fd(fd
, fd
);
1164 chr
->chr_ioctl
= tty_serial_ioctl
;
1165 qemu_chr_reset(chr
);
1168 #else /* ! __linux__ && ! __sun__ */
1169 static CharDriverState
*qemu_chr_open_pty(void)
1173 #endif /* __linux__ || __sun__ */
1175 #if defined(__linux__)
1179 } ParallelCharDriver
;
1181 static int pp_hw_mode(ParallelCharDriver
*s
, uint16_t mode
)
1183 if (s
->mode
!= mode
) {
1185 if (ioctl(s
->fd
, PPSETMODE
, &m
) < 0)
1192 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1194 ParallelCharDriver
*drv
= chr
->opaque
;
1199 case CHR_IOCTL_PP_READ_DATA
:
1200 if (ioctl(fd
, PPRDATA
, &b
) < 0)
1202 *(uint8_t *)arg
= b
;
1204 case CHR_IOCTL_PP_WRITE_DATA
:
1205 b
= *(uint8_t *)arg
;
1206 if (ioctl(fd
, PPWDATA
, &b
) < 0)
1209 case CHR_IOCTL_PP_READ_CONTROL
:
1210 if (ioctl(fd
, PPRCONTROL
, &b
) < 0)
1212 /* Linux gives only the lowest bits, and no way to know data
1213 direction! For better compatibility set the fixed upper
1215 *(uint8_t *)arg
= b
| 0xc0;
1217 case CHR_IOCTL_PP_WRITE_CONTROL
:
1218 b
= *(uint8_t *)arg
;
1219 if (ioctl(fd
, PPWCONTROL
, &b
) < 0)
1222 case CHR_IOCTL_PP_READ_STATUS
:
1223 if (ioctl(fd
, PPRSTATUS
, &b
) < 0)
1225 *(uint8_t *)arg
= b
;
1227 case CHR_IOCTL_PP_DATA_DIR
:
1228 if (ioctl(fd
, PPDATADIR
, (int *)arg
) < 0)
1231 case CHR_IOCTL_PP_EPP_READ_ADDR
:
1232 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1233 struct ParallelIOArg
*parg
= arg
;
1234 int n
= read(fd
, parg
->buffer
, parg
->count
);
1235 if (n
!= parg
->count
) {
1240 case CHR_IOCTL_PP_EPP_READ
:
1241 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1242 struct ParallelIOArg
*parg
= arg
;
1243 int n
= read(fd
, parg
->buffer
, parg
->count
);
1244 if (n
!= parg
->count
) {
1249 case CHR_IOCTL_PP_EPP_WRITE_ADDR
:
1250 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
|IEEE1284_ADDR
)) {
1251 struct ParallelIOArg
*parg
= arg
;
1252 int n
= write(fd
, parg
->buffer
, parg
->count
);
1253 if (n
!= parg
->count
) {
1258 case CHR_IOCTL_PP_EPP_WRITE
:
1259 if (pp_hw_mode(drv
, IEEE1284_MODE_EPP
)) {
1260 struct ParallelIOArg
*parg
= arg
;
1261 int n
= write(fd
, parg
->buffer
, parg
->count
);
1262 if (n
!= parg
->count
) {
1273 static void pp_close(CharDriverState
*chr
)
1275 ParallelCharDriver
*drv
= chr
->opaque
;
1278 pp_hw_mode(drv
, IEEE1284_MODE_COMPAT
);
1279 ioctl(fd
, PPRELEASE
);
1282 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1285 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1287 const char *filename
= qemu_opt_get(opts
, "path");
1288 CharDriverState
*chr
;
1289 ParallelCharDriver
*drv
;
1292 TFR(fd
= open(filename
, O_RDWR
));
1296 if (ioctl(fd
, PPCLAIM
) < 0) {
1301 drv
= qemu_mallocz(sizeof(ParallelCharDriver
));
1303 drv
->mode
= IEEE1284_MODE_COMPAT
;
1305 chr
= qemu_mallocz(sizeof(CharDriverState
));
1306 chr
->chr_write
= null_chr_write
;
1307 chr
->chr_ioctl
= pp_ioctl
;
1308 chr
->chr_close
= pp_close
;
1311 qemu_chr_reset(chr
);
1315 #endif /* __linux__ */
1317 #if defined(__FreeBSD__) || defined(__DragonFly__)
1318 static int pp_ioctl(CharDriverState
*chr
, int cmd
, void *arg
)
1320 int fd
= (int)chr
->opaque
;
1324 case CHR_IOCTL_PP_READ_DATA
:
1325 if (ioctl(fd
, PPIGDATA
, &b
) < 0)
1327 *(uint8_t *)arg
= b
;
1329 case CHR_IOCTL_PP_WRITE_DATA
:
1330 b
= *(uint8_t *)arg
;
1331 if (ioctl(fd
, PPISDATA
, &b
) < 0)
1334 case CHR_IOCTL_PP_READ_CONTROL
:
1335 if (ioctl(fd
, PPIGCTRL
, &b
) < 0)
1337 *(uint8_t *)arg
= b
;
1339 case CHR_IOCTL_PP_WRITE_CONTROL
:
1340 b
= *(uint8_t *)arg
;
1341 if (ioctl(fd
, PPISCTRL
, &b
) < 0)
1344 case CHR_IOCTL_PP_READ_STATUS
:
1345 if (ioctl(fd
, PPIGSTATUS
, &b
) < 0)
1347 *(uint8_t *)arg
= b
;
1355 static CharDriverState
*qemu_chr_open_pp(QemuOpts
*opts
)
1357 const char *filename
= qemu_opt_get(opts
, "path");
1358 CharDriverState
*chr
;
1361 fd
= open(filename
, O_RDWR
);
1365 chr
= qemu_mallocz(sizeof(CharDriverState
));
1366 chr
->opaque
= (void *)fd
;
1367 chr
->chr_write
= null_chr_write
;
1368 chr
->chr_ioctl
= pp_ioctl
;
1377 HANDLE hcom
, hrecv
, hsend
;
1378 OVERLAPPED orecv
, osend
;
1383 #define NSENDBUF 2048
1384 #define NRECVBUF 2048
1385 #define MAXCONNECT 1
1386 #define NTIMEOUT 5000
1388 static int win_chr_poll(void *opaque
);
1389 static int win_chr_pipe_poll(void *opaque
);
1391 static void win_chr_close(CharDriverState
*chr
)
1393 WinCharState
*s
= chr
->opaque
;
1396 CloseHandle(s
->hsend
);
1400 CloseHandle(s
->hrecv
);
1404 CloseHandle(s
->hcom
);
1408 qemu_del_polling_cb(win_chr_pipe_poll
, chr
);
1410 qemu_del_polling_cb(win_chr_poll
, chr
);
1412 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1415 static int win_chr_init(CharDriverState
*chr
, const char *filename
)
1417 WinCharState
*s
= chr
->opaque
;
1419 COMMTIMEOUTS cto
= { 0, 0, 0, 0, 0};
1424 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1426 fprintf(stderr
, "Failed CreateEvent\n");
1429 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1431 fprintf(stderr
, "Failed CreateEvent\n");
1435 s
->hcom
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1436 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, 0);
1437 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1438 fprintf(stderr
, "Failed CreateFile (%lu)\n", GetLastError());
1443 if (!SetupComm(s
->hcom
, NRECVBUF
, NSENDBUF
)) {
1444 fprintf(stderr
, "Failed SetupComm\n");
1448 ZeroMemory(&comcfg
, sizeof(COMMCONFIG
));
1449 size
= sizeof(COMMCONFIG
);
1450 GetDefaultCommConfig(filename
, &comcfg
, &size
);
1451 comcfg
.dcb
.DCBlength
= sizeof(DCB
);
1452 CommConfigDialog(filename
, NULL
, &comcfg
);
1454 if (!SetCommState(s
->hcom
, &comcfg
.dcb
)) {
1455 fprintf(stderr
, "Failed SetCommState\n");
1459 if (!SetCommMask(s
->hcom
, EV_ERR
)) {
1460 fprintf(stderr
, "Failed SetCommMask\n");
1464 cto
.ReadIntervalTimeout
= MAXDWORD
;
1465 if (!SetCommTimeouts(s
->hcom
, &cto
)) {
1466 fprintf(stderr
, "Failed SetCommTimeouts\n");
1470 if (!ClearCommError(s
->hcom
, &err
, &comstat
)) {
1471 fprintf(stderr
, "Failed ClearCommError\n");
1474 qemu_add_polling_cb(win_chr_poll
, chr
);
1482 static int win_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len1
)
1484 WinCharState
*s
= chr
->opaque
;
1485 DWORD len
, ret
, size
, err
;
1488 ZeroMemory(&s
->osend
, sizeof(s
->osend
));
1489 s
->osend
.hEvent
= s
->hsend
;
1492 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, &s
->osend
);
1494 ret
= WriteFile(s
->hcom
, buf
, len
, &size
, NULL
);
1496 err
= GetLastError();
1497 if (err
== ERROR_IO_PENDING
) {
1498 ret
= GetOverlappedResult(s
->hcom
, &s
->osend
, &size
, TRUE
);
1516 static int win_chr_read_poll(CharDriverState
*chr
)
1518 WinCharState
*s
= chr
->opaque
;
1520 s
->max_size
= qemu_chr_can_read(chr
);
1524 static void win_chr_readfile(CharDriverState
*chr
)
1526 WinCharState
*s
= chr
->opaque
;
1531 ZeroMemory(&s
->orecv
, sizeof(s
->orecv
));
1532 s
->orecv
.hEvent
= s
->hrecv
;
1533 ret
= ReadFile(s
->hcom
, buf
, s
->len
, &size
, &s
->orecv
);
1535 err
= GetLastError();
1536 if (err
== ERROR_IO_PENDING
) {
1537 ret
= GetOverlappedResult(s
->hcom
, &s
->orecv
, &size
, TRUE
);
1542 qemu_chr_read(chr
, buf
, size
);
1546 static void win_chr_read(CharDriverState
*chr
)
1548 WinCharState
*s
= chr
->opaque
;
1550 if (s
->len
> s
->max_size
)
1551 s
->len
= s
->max_size
;
1555 win_chr_readfile(chr
);
1558 static int win_chr_poll(void *opaque
)
1560 CharDriverState
*chr
= opaque
;
1561 WinCharState
*s
= chr
->opaque
;
1565 ClearCommError(s
->hcom
, &comerr
, &status
);
1566 if (status
.cbInQue
> 0) {
1567 s
->len
= status
.cbInQue
;
1568 win_chr_read_poll(chr
);
1575 static CharDriverState
*qemu_chr_open_win(QemuOpts
*opts
)
1577 const char *filename
= qemu_opt_get(opts
, "path");
1578 CharDriverState
*chr
;
1581 chr
= qemu_mallocz(sizeof(CharDriverState
));
1582 s
= qemu_mallocz(sizeof(WinCharState
));
1584 chr
->chr_write
= win_chr_write
;
1585 chr
->chr_close
= win_chr_close
;
1587 if (win_chr_init(chr
, filename
) < 0) {
1592 qemu_chr_reset(chr
);
1596 static int win_chr_pipe_poll(void *opaque
)
1598 CharDriverState
*chr
= opaque
;
1599 WinCharState
*s
= chr
->opaque
;
1602 PeekNamedPipe(s
->hcom
, NULL
, 0, NULL
, &size
, NULL
);
1605 win_chr_read_poll(chr
);
1612 static int win_chr_pipe_init(CharDriverState
*chr
, const char *filename
)
1614 WinCharState
*s
= chr
->opaque
;
1622 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1624 fprintf(stderr
, "Failed CreateEvent\n");
1627 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1629 fprintf(stderr
, "Failed CreateEvent\n");
1633 snprintf(openname
, sizeof(openname
), "\\\\.\\pipe\\%s", filename
);
1634 s
->hcom
= CreateNamedPipe(openname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
1635 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
1637 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
1638 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
1639 fprintf(stderr
, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1644 ZeroMemory(&ov
, sizeof(ov
));
1645 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1646 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
1648 fprintf(stderr
, "Failed ConnectNamedPipe\n");
1652 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
1654 fprintf(stderr
, "Failed GetOverlappedResult\n");
1656 CloseHandle(ov
.hEvent
);
1663 CloseHandle(ov
.hEvent
);
1666 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
1675 static CharDriverState
*qemu_chr_open_win_pipe(QemuOpts
*opts
)
1677 const char *filename
= qemu_opt_get(opts
, "path");
1678 CharDriverState
*chr
;
1681 chr
= qemu_mallocz(sizeof(CharDriverState
));
1682 s
= qemu_mallocz(sizeof(WinCharState
));
1684 chr
->chr_write
= win_chr_write
;
1685 chr
->chr_close
= win_chr_close
;
1687 if (win_chr_pipe_init(chr
, filename
) < 0) {
1692 qemu_chr_reset(chr
);
1696 static CharDriverState
*qemu_chr_open_win_file(HANDLE fd_out
)
1698 CharDriverState
*chr
;
1701 chr
= qemu_mallocz(sizeof(CharDriverState
));
1702 s
= qemu_mallocz(sizeof(WinCharState
));
1705 chr
->chr_write
= win_chr_write
;
1706 qemu_chr_reset(chr
);
1710 static CharDriverState
*qemu_chr_open_win_con(QemuOpts
*opts
)
1712 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE
));
1715 static CharDriverState
*qemu_chr_open_win_file_out(QemuOpts
*opts
)
1717 const char *file_out
= qemu_opt_get(opts
, "path");
1720 fd_out
= CreateFile(file_out
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
1721 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1722 if (fd_out
== INVALID_HANDLE_VALUE
)
1725 return qemu_chr_open_win_file(fd_out
);
1727 #endif /* !_WIN32 */
1729 /***********************************************************/
1730 /* UDP Net console */
1740 static int udp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1742 NetCharDriver
*s
= chr
->opaque
;
1744 return send(s
->fd
, (const void *)buf
, len
, 0);
1747 static int udp_chr_read_poll(void *opaque
)
1749 CharDriverState
*chr
= opaque
;
1750 NetCharDriver
*s
= chr
->opaque
;
1752 s
->max_size
= qemu_chr_can_read(chr
);
1754 /* If there were any stray characters in the queue process them
1757 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1758 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1760 s
->max_size
= qemu_chr_can_read(chr
);
1765 static void udp_chr_read(void *opaque
)
1767 CharDriverState
*chr
= opaque
;
1768 NetCharDriver
*s
= chr
->opaque
;
1770 if (s
->max_size
== 0)
1772 s
->bufcnt
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1773 s
->bufptr
= s
->bufcnt
;
1778 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
1779 qemu_chr_read(chr
, &s
->buf
[s
->bufptr
], 1);
1781 s
->max_size
= qemu_chr_can_read(chr
);
1785 static void udp_chr_update_read_handler(CharDriverState
*chr
)
1787 NetCharDriver
*s
= chr
->opaque
;
1790 qemu_set_fd_handler2(s
->fd
, udp_chr_read_poll
,
1791 udp_chr_read
, NULL
, chr
);
1795 static void udp_chr_close(CharDriverState
*chr
)
1797 NetCharDriver
*s
= chr
->opaque
;
1799 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1803 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
1806 static CharDriverState
*qemu_chr_open_udp(QemuOpts
*opts
)
1808 CharDriverState
*chr
= NULL
;
1809 NetCharDriver
*s
= NULL
;
1812 chr
= qemu_mallocz(sizeof(CharDriverState
));
1813 s
= qemu_mallocz(sizeof(NetCharDriver
));
1815 fd
= inet_dgram_opts(opts
);
1817 fprintf(stderr
, "inet_dgram_opts failed\n");
1825 chr
->chr_write
= udp_chr_write
;
1826 chr
->chr_update_read_handler
= udp_chr_update_read_handler
;
1827 chr
->chr_close
= udp_chr_close
;
1840 /***********************************************************/
1841 /* TCP Net console */
1853 static void tcp_chr_accept(void *opaque
);
1855 static int tcp_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1857 TCPCharDriver
*s
= chr
->opaque
;
1859 return send_all(s
->fd
, buf
, len
);
1861 /* XXX: indicate an error ? */
1866 static int tcp_chr_read_poll(void *opaque
)
1868 CharDriverState
*chr
= opaque
;
1869 TCPCharDriver
*s
= chr
->opaque
;
1872 s
->max_size
= qemu_chr_can_read(chr
);
1877 #define IAC_BREAK 243
1878 static void tcp_chr_process_IAC_bytes(CharDriverState
*chr
,
1880 uint8_t *buf
, int *size
)
1882 /* Handle any telnet client's basic IAC options to satisfy char by
1883 * char mode with no echo. All IAC options will be removed from
1884 * the buf and the do_telnetopt variable will be used to track the
1885 * state of the width of the IAC information.
1887 * IAC commands come in sets of 3 bytes with the exception of the
1888 * "IAC BREAK" command and the double IAC.
1894 for (i
= 0; i
< *size
; i
++) {
1895 if (s
->do_telnetopt
> 1) {
1896 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
1897 /* Double IAC means send an IAC */
1901 s
->do_telnetopt
= 1;
1903 if ((unsigned char)buf
[i
] == IAC_BREAK
&& s
->do_telnetopt
== 2) {
1904 /* Handle IAC break commands by sending a serial break */
1905 qemu_chr_event(chr
, CHR_EVENT_BREAK
);
1910 if (s
->do_telnetopt
>= 4) {
1911 s
->do_telnetopt
= 1;
1914 if ((unsigned char)buf
[i
] == IAC
) {
1915 s
->do_telnetopt
= 2;
1926 static int tcp_get_msgfd(CharDriverState
*chr
)
1928 TCPCharDriver
*s
= chr
->opaque
;
1934 static void unix_process_msgfd(CharDriverState
*chr
, struct msghdr
*msg
)
1936 TCPCharDriver
*s
= chr
->opaque
;
1937 struct cmsghdr
*cmsg
;
1939 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1942 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
1943 cmsg
->cmsg_level
!= SOL_SOCKET
||
1944 cmsg
->cmsg_type
!= SCM_RIGHTS
)
1947 fd
= *((int *)CMSG_DATA(cmsg
));
1957 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1959 TCPCharDriver
*s
= chr
->opaque
;
1960 struct msghdr msg
= { NULL
, };
1961 struct iovec iov
[1];
1963 struct cmsghdr cmsg
;
1964 char control
[CMSG_SPACE(sizeof(int))];
1968 iov
[0].iov_base
= buf
;
1969 iov
[0].iov_len
= len
;
1973 msg
.msg_control
= &msg_control
;
1974 msg
.msg_controllen
= sizeof(msg_control
);
1976 ret
= recvmsg(s
->fd
, &msg
, 0);
1977 if (ret
> 0 && s
->is_unix
)
1978 unix_process_msgfd(chr
, &msg
);
1983 static ssize_t
tcp_chr_recv(CharDriverState
*chr
, char *buf
, size_t len
)
1985 TCPCharDriver
*s
= chr
->opaque
;
1986 return recv(s
->fd
, buf
, len
, 0);
1990 static void tcp_chr_read(void *opaque
)
1992 CharDriverState
*chr
= opaque
;
1993 TCPCharDriver
*s
= chr
->opaque
;
1997 if (!s
->connected
|| s
->max_size
<= 0)
2000 if (len
> s
->max_size
)
2002 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
2004 /* connection closed */
2006 if (s
->listen_fd
>= 0) {
2007 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2009 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2012 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2013 } else if (size
> 0) {
2014 if (s
->do_telnetopt
)
2015 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
2017 qemu_chr_read(chr
, buf
, size
);
2018 if (s
->msgfd
!= -1) {
2025 static void tcp_chr_connect(void *opaque
)
2027 CharDriverState
*chr
= opaque
;
2028 TCPCharDriver
*s
= chr
->opaque
;
2031 qemu_set_fd_handler2(s
->fd
, tcp_chr_read_poll
,
2032 tcp_chr_read
, NULL
, chr
);
2033 qemu_chr_reset(chr
);
2036 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2037 static void tcp_chr_telnet_init(int fd
)
2040 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2041 IACSET(buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2042 send(fd
, (char *)buf
, 3, 0);
2043 IACSET(buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2044 send(fd
, (char *)buf
, 3, 0);
2045 IACSET(buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2046 send(fd
, (char *)buf
, 3, 0);
2047 IACSET(buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2048 send(fd
, (char *)buf
, 3, 0);
2051 static void socket_set_nodelay(int fd
)
2054 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2057 static void tcp_chr_accept(void *opaque
)
2059 CharDriverState
*chr
= opaque
;
2060 TCPCharDriver
*s
= chr
->opaque
;
2061 struct sockaddr_in saddr
;
2063 struct sockaddr_un uaddr
;
2065 struct sockaddr
*addr
;
2072 len
= sizeof(uaddr
);
2073 addr
= (struct sockaddr
*)&uaddr
;
2077 len
= sizeof(saddr
);
2078 addr
= (struct sockaddr
*)&saddr
;
2080 fd
= accept(s
->listen_fd
, addr
, &len
);
2081 if (fd
< 0 && errno
!= EINTR
) {
2083 } else if (fd
>= 0) {
2084 if (s
->do_telnetopt
)
2085 tcp_chr_telnet_init(fd
);
2089 socket_set_nonblock(fd
);
2091 socket_set_nodelay(fd
);
2093 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2094 tcp_chr_connect(chr
);
2097 static void tcp_chr_close(CharDriverState
*chr
)
2099 TCPCharDriver
*s
= chr
->opaque
;
2101 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2104 if (s
->listen_fd
>= 0) {
2105 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
2106 closesocket(s
->listen_fd
);
2109 qemu_chr_event(chr
, CHR_EVENT_CLOSED
);
2112 static CharDriverState
*qemu_chr_open_socket(QemuOpts
*opts
)
2114 CharDriverState
*chr
= NULL
;
2115 TCPCharDriver
*s
= NULL
;
2123 is_listen
= qemu_opt_get_bool(opts
, "server", 0);
2124 is_waitconnect
= qemu_opt_get_bool(opts
, "wait", 1);
2125 is_telnet
= qemu_opt_get_bool(opts
, "telnet", 0);
2126 do_nodelay
= !qemu_opt_get_bool(opts
, "delay", 1);
2127 is_unix
= qemu_opt_get(opts
, "path") != NULL
;
2131 chr
= qemu_mallocz(sizeof(CharDriverState
));
2132 s
= qemu_mallocz(sizeof(TCPCharDriver
));
2136 fd
= unix_listen_opts(opts
);
2138 fd
= unix_connect_opts(opts
);
2142 fd
= inet_listen_opts(opts
, 0);
2144 fd
= inet_connect_opts(opts
);
2150 if (!is_waitconnect
)
2151 socket_set_nonblock(fd
);
2157 s
->is_unix
= is_unix
;
2158 s
->do_nodelay
= do_nodelay
&& !is_unix
;
2161 chr
->chr_write
= tcp_chr_write
;
2162 chr
->chr_close
= tcp_chr_close
;
2163 chr
->get_msgfd
= tcp_get_msgfd
;
2167 qemu_set_fd_handler(s
->listen_fd
, tcp_chr_accept
, NULL
, chr
);
2169 s
->do_telnetopt
= 1;
2174 socket_set_nodelay(fd
);
2175 tcp_chr_connect(chr
);
2178 /* for "info chardev" monitor command */
2179 chr
->filename
= qemu_malloc(256);
2181 snprintf(chr
->filename
, 256, "unix:%s%s",
2182 qemu_opt_get(opts
, "path"),
2183 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2184 } else if (is_telnet
) {
2185 snprintf(chr
->filename
, 256, "telnet:%s:%s%s",
2186 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2187 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2189 snprintf(chr
->filename
, 256, "tcp:%s:%s%s",
2190 qemu_opt_get(opts
, "host"), qemu_opt_get(opts
, "port"),
2191 qemu_opt_get_bool(opts
, "server", 0) ? ",server" : "");
2194 if (is_listen
&& is_waitconnect
) {
2195 printf("QEMU waiting for connection on: %s\n",
2197 tcp_chr_accept(chr
);
2198 socket_set_nonblock(s
->listen_fd
);
2210 static QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
)
2212 char host
[65], port
[33], width
[8], height
[8];
2217 opts
= qemu_opts_create(&qemu_chardev_opts
, label
, 1);
2221 if (strstart(filename
, "mon:", &p
)) {
2223 qemu_opt_set(opts
, "mux", "on");
2226 if (strcmp(filename
, "null") == 0 ||
2227 strcmp(filename
, "pty") == 0 ||
2228 strcmp(filename
, "msmouse") == 0 ||
2229 strcmp(filename
, "braille") == 0 ||
2230 strcmp(filename
, "stdio") == 0) {
2231 qemu_opt_set(opts
, "backend", filename
);
2234 if (strstart(filename
, "vc", &p
)) {
2235 qemu_opt_set(opts
, "backend", "vc");
2237 if (sscanf(p
+1, "%8[0-9]x%8[0-9]", width
, height
) == 2) {
2239 qemu_opt_set(opts
, "width", width
);
2240 qemu_opt_set(opts
, "height", height
);
2241 } else if (sscanf(p
+1, "%8[0-9]Cx%8[0-9]C", width
, height
) == 2) {
2243 qemu_opt_set(opts
, "cols", width
);
2244 qemu_opt_set(opts
, "rows", height
);
2251 if (strcmp(filename
, "con:") == 0) {
2252 qemu_opt_set(opts
, "backend", "console");
2255 if (strstart(filename
, "COM", NULL
)) {
2256 qemu_opt_set(opts
, "backend", "serial");
2257 qemu_opt_set(opts
, "path", filename
);
2260 if (strstart(filename
, "file:", &p
)) {
2261 qemu_opt_set(opts
, "backend", "file");
2262 qemu_opt_set(opts
, "path", p
);
2265 if (strstart(filename
, "pipe:", &p
)) {
2266 qemu_opt_set(opts
, "backend", "pipe");
2267 qemu_opt_set(opts
, "path", p
);
2270 if (strstart(filename
, "tcp:", &p
) ||
2271 strstart(filename
, "telnet:", &p
)) {
2272 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2274 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1)
2277 qemu_opt_set(opts
, "backend", "socket");
2278 qemu_opt_set(opts
, "host", host
);
2279 qemu_opt_set(opts
, "port", port
);
2280 if (p
[pos
] == ',') {
2281 if (qemu_opts_do_parse(opts
, p
+pos
+1, NULL
) != 0)
2284 if (strstart(filename
, "telnet:", &p
))
2285 qemu_opt_set(opts
, "telnet", "on");
2288 if (strstart(filename
, "udp:", &p
)) {
2289 qemu_opt_set(opts
, "backend", "udp");
2290 if (sscanf(p
, "%64[^:]:%32[^@,]%n", host
, port
, &pos
) < 2) {
2292 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2293 fprintf(stderr
, "udp #1\n");
2297 qemu_opt_set(opts
, "host", host
);
2298 qemu_opt_set(opts
, "port", port
);
2299 if (p
[pos
] == '@') {
2301 if (sscanf(p
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) < 2) {
2303 if (sscanf(p
, ":%32[^,]%n", port
, &pos
) < 1) {
2304 fprintf(stderr
, "udp #2\n");
2308 qemu_opt_set(opts
, "localaddr", host
);
2309 qemu_opt_set(opts
, "localport", port
);
2313 if (strstart(filename
, "unix:", &p
)) {
2314 qemu_opt_set(opts
, "backend", "socket");
2315 if (qemu_opts_do_parse(opts
, p
, "path") != 0)
2319 if (strstart(filename
, "/dev/parport", NULL
) ||
2320 strstart(filename
, "/dev/ppi", NULL
)) {
2321 qemu_opt_set(opts
, "backend", "parport");
2322 qemu_opt_set(opts
, "path", filename
);
2325 if (strstart(filename
, "/dev/", NULL
)) {
2326 qemu_opt_set(opts
, "backend", "tty");
2327 qemu_opt_set(opts
, "path", filename
);
2332 fprintf(stderr
, "%s: fail on \"%s\"\n", __FUNCTION__
, filename
);
2333 qemu_opts_del(opts
);
2337 static const struct {
2339 CharDriverState
*(*open
)(QemuOpts
*opts
);
2340 } backend_table
[] = {
2341 { .name
= "null", .open
= qemu_chr_open_null
},
2342 { .name
= "socket", .open
= qemu_chr_open_socket
},
2343 { .name
= "udp", .open
= qemu_chr_open_udp
},
2344 { .name
= "msmouse", .open
= qemu_chr_open_msmouse
},
2345 { .name
= "vc", .open
= text_console_init
},
2347 { .name
= "file", .open
= qemu_chr_open_win_file_out
},
2348 { .name
= "pipe", .open
= qemu_chr_open_win_pipe
},
2349 { .name
= "console", .open
= qemu_chr_open_win_con
},
2350 { .name
= "serial", .open
= qemu_chr_open_win
},
2352 { .name
= "file", .open
= qemu_chr_open_file_out
},
2353 { .name
= "pipe", .open
= qemu_chr_open_pipe
},
2354 { .name
= "pty", .open
= qemu_chr_open_pty
},
2355 { .name
= "stdio", .open
= qemu_chr_open_stdio
},
2357 #ifdef CONFIG_BRLAPI
2358 { .name
= "braille", .open
= chr_baum_init
},
2360 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2361 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2362 { .name
= "tty", .open
= qemu_chr_open_tty
},
2364 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2365 { .name
= "parport", .open
= qemu_chr_open_pp
},
2369 CharDriverState
*qemu_chr_open_opts(QemuOpts
*opts
,
2370 void (*init
)(struct CharDriverState
*s
))
2372 CharDriverState
*chr
;
2375 if (qemu_opts_id(opts
) == NULL
) {
2376 fprintf(stderr
, "chardev: no id specified\n");
2380 for (i
= 0; i
< ARRAY_SIZE(backend_table
); i
++) {
2381 if (strcmp(backend_table
[i
].name
, qemu_opt_get(opts
, "backend")) == 0)
2384 if (i
== ARRAY_SIZE(backend_table
)) {
2385 fprintf(stderr
, "chardev: backend \"%s\" not found\n",
2386 qemu_opt_get(opts
, "backend"));
2390 chr
= backend_table
[i
].open(opts
);
2392 fprintf(stderr
, "chardev: opening backend \"%s\" failed\n",
2393 qemu_opt_get(opts
, "backend"));
2398 chr
->filename
= qemu_strdup(qemu_opt_get(opts
, "backend"));
2400 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2402 if (qemu_opt_get_bool(opts
, "mux", 0)) {
2403 CharDriverState
*base
= chr
;
2404 int len
= strlen(qemu_opts_id(opts
)) + 6;
2405 base
->label
= qemu_malloc(len
);
2406 snprintf(base
->label
, len
, "%s-base", qemu_opts_id(opts
));
2407 chr
= qemu_chr_open_mux(base
);
2408 chr
->filename
= base
->filename
;
2409 QTAILQ_INSERT_TAIL(&chardevs
, chr
, next
);
2411 chr
->label
= qemu_strdup(qemu_opts_id(opts
));
2415 CharDriverState
*qemu_chr_open(const char *label
, const char *filename
, void (*init
)(struct CharDriverState
*s
))
2418 CharDriverState
*chr
;
2421 if (strstart(filename
, "chardev:", &p
)) {
2422 return qemu_chr_find(p
);
2425 opts
= qemu_chr_parse_compat(label
, filename
);
2429 chr
= qemu_chr_open_opts(opts
, init
);
2430 if (chr
&& qemu_opt_get_bool(opts
, "mux", 0)) {
2431 monitor_init(chr
, MONITOR_USE_READLINE
);
2436 void qemu_chr_close(CharDriverState
*chr
)
2438 QTAILQ_REMOVE(&chardevs
, chr
, next
);
2440 chr
->chr_close(chr
);
2441 qemu_free(chr
->filename
);
2442 qemu_free(chr
->label
);
2446 void qemu_chr_info(Monitor
*mon
)
2448 CharDriverState
*chr
;
2450 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2451 monitor_printf(mon
, "%s: filename=%s\n", chr
->label
, chr
->filename
);
2455 CharDriverState
*qemu_chr_find(const char *name
)
2457 CharDriverState
*chr
;
2459 QTAILQ_FOREACH(chr
, &chardevs
, next
) {
2460 if (strcmp(chr
->label
, name
) != 0)