eepro100: intermediate merge fixup
[qemu-kvm/markmc.git] / qemu-char.c
blob0fd402c46d1940e24302d815edf1d586acb775ea
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "net.h"
26 #include "monitor.h"
27 #include "console.h"
28 #include "sysemu.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
31 #include "block.h"
32 #include "hw/usb.h"
33 #include "hw/baum.h"
34 #include "hw/msmouse.h"
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <time.h>
40 #include <errno.h>
41 #include <sys/time.h>
42 #include <zlib.h>
44 #ifndef _WIN32
45 #include <sys/times.h>
46 #include <sys/wait.h>
47 #include <termios.h>
48 #include <sys/mman.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <net/if.h>
54 #ifdef __NetBSD__
55 #include <net/if_tap.h>
56 #endif
57 #ifdef __linux__
58 #include <linux/if_tun.h>
59 #endif
60 #include <arpa/inet.h>
61 #include <dirent.h>
62 #include <netdb.h>
63 #include <sys/select.h>
64 #ifdef CONFIG_BSD
65 #include <sys/stat.h>
66 #ifdef __FreeBSD__
67 #include <libutil.h>
68 #include <dev/ppbus/ppi.h>
69 #include <dev/ppbus/ppbconf.h>
70 #elif defined(__DragonFly__)
71 #include <libutil.h>
72 #include <dev/misc/ppi/ppi.h>
73 #include <bus/ppbus/ppbconf.h>
74 #else
75 #include <util.h>
76 #endif
77 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
78 #include <freebsd/stdlib.h>
79 #else
80 #ifdef __linux__
81 #include <pty.h>
83 #include <linux/ppdev.h>
84 #include <linux/parport.h>
85 #endif
86 #ifdef __sun__
87 #include <sys/stat.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>
97 #include <net/if.h>
98 #include <syslog.h>
99 #include <stropts.h>
100 #endif
101 #endif
102 #endif
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)
114 if (!s->chr_event)
115 return;
116 s->chr_event(s->handler_opaque, event);
119 static void qemu_chr_reset_bh(void *opaque)
121 CharDriverState *s = opaque;
123 if (s->initial_reset_issued) {
124 qemu_chr_event(s, CHR_EVENT_OPENED);
125 } else {
126 s->initial_reset_issued = true;
128 qemu_bh_delete(s->bh);
129 s->bh = NULL;
132 void qemu_chr_reset(CharDriverState *s)
134 if (s->bh == NULL) {
135 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
136 qemu_bh_schedule(s->bh);
140 void qemu_chr_initial_reset(void)
142 CharDriverState *chr;
144 QTAILQ_FOREACH(chr, &chardevs, next) {
145 qemu_chr_reset(chr);
149 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
151 return s->chr_write(s, buf, len);
154 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
156 if (!s->chr_ioctl)
157 return -ENOTSUP;
158 return s->chr_ioctl(s, cmd, arg);
161 int qemu_chr_can_read(CharDriverState *s)
163 if (!s->chr_can_read)
164 return 0;
165 return s->chr_can_read(s->handler_opaque);
168 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
170 s->chr_read(s->handler_opaque, buf, len);
173 int qemu_chr_get_msgfd(CharDriverState *s)
175 return s->get_msgfd ? s->get_msgfd(s) : -1;
178 void qemu_chr_accept_input(CharDriverState *s)
180 if (s->chr_accept_input)
181 s->chr_accept_input(s);
184 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
186 char buf[4096];
187 va_list ap;
188 va_start(ap, fmt);
189 vsnprintf(buf, sizeof(buf), fmt, ap);
190 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
191 va_end(ap);
194 void qemu_chr_send_event(CharDriverState *s, int event)
196 if (s->chr_send_event)
197 s->chr_send_event(s, event);
200 void qemu_chr_add_handlers(CharDriverState *s,
201 IOCanRWHandler *fd_can_read,
202 IOReadHandler *fd_read,
203 IOEventHandler *fd_event,
204 void *opaque)
206 s->chr_can_read = fd_can_read;
207 s->chr_read = fd_read;
208 s->chr_event = fd_event;
209 s->handler_opaque = opaque;
210 if (s->chr_update_read_handler)
211 s->chr_update_read_handler(s);
214 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
216 return len;
219 static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
221 CharDriverState *chr;
223 chr = qemu_mallocz(sizeof(CharDriverState));
224 chr->chr_write = null_chr_write;
225 return chr;
228 /* MUX driver for serial I/O splitting */
229 #define MAX_MUX 4
230 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
231 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
232 typedef struct {
233 IOCanRWHandler *chr_can_read[MAX_MUX];
234 IOReadHandler *chr_read[MAX_MUX];
235 IOEventHandler *chr_event[MAX_MUX];
236 void *ext_opaque[MAX_MUX];
237 CharDriverState *drv;
238 int focus;
239 int mux_cnt;
240 int term_got_escape;
241 int max_size;
242 /* Intermediate input buffer allows to catch escape sequences even if the
243 currently active device is not accepting any input - but only until it
244 is full as well. */
245 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
246 int prod[MAX_MUX];
247 int cons[MAX_MUX];
248 int timestamps;
249 int linestart;
250 int64_t timestamps_start;
251 } MuxDriver;
254 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
256 MuxDriver *d = chr->opaque;
257 int ret;
258 if (!d->timestamps) {
259 ret = d->drv->chr_write(d->drv, buf, len);
260 } else {
261 int i;
263 ret = 0;
264 for (i = 0; i < len; i++) {
265 if (d->linestart) {
266 char buf1[64];
267 int64_t ti;
268 int secs;
270 ti = qemu_get_clock(rt_clock);
271 if (d->timestamps_start == -1)
272 d->timestamps_start = ti;
273 ti -= d->timestamps_start;
274 secs = ti / 1000;
275 snprintf(buf1, sizeof(buf1),
276 "[%02d:%02d:%02d.%03d] ",
277 secs / 3600,
278 (secs / 60) % 60,
279 secs % 60,
280 (int)(ti % 1000));
281 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
282 d->linestart = 0;
284 ret += d->drv->chr_write(d->drv, buf+i, 1);
285 if (buf[i] == '\n') {
286 d->linestart = 1;
290 return ret;
293 static const char * const mux_help[] = {
294 "% h print this help\n\r",
295 "% x exit emulator\n\r",
296 "% s save disk data back to file (if -snapshot)\n\r",
297 "% t toggle console timestamps\n\r"
298 "% b send break (magic sysrq)\n\r",
299 "% c switch between console and monitor\n\r",
300 "% % sends %\n\r",
301 NULL
304 int term_escape_char = 0x01; /* ctrl-a is used for escape */
305 static void mux_print_help(CharDriverState *chr)
307 int i, j;
308 char ebuf[15] = "Escape-Char";
309 char cbuf[50] = "\n\r";
311 if (term_escape_char > 0 && term_escape_char < 26) {
312 snprintf(cbuf, sizeof(cbuf), "\n\r");
313 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
314 } else {
315 snprintf(cbuf, sizeof(cbuf),
316 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
317 term_escape_char);
319 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
320 for (i = 0; mux_help[i] != NULL; i++) {
321 for (j=0; mux_help[i][j] != '\0'; j++) {
322 if (mux_help[i][j] == '%')
323 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
324 else
325 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
330 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
332 if (d->chr_event[mux_nr])
333 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
336 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
338 if (d->term_got_escape) {
339 d->term_got_escape = 0;
340 if (ch == term_escape_char)
341 goto send_char;
342 switch(ch) {
343 case '?':
344 case 'h':
345 mux_print_help(chr);
346 break;
347 case 'x':
349 const char *term = "QEMU: Terminated\n\r";
350 chr->chr_write(chr,(uint8_t *)term,strlen(term));
351 exit(0);
352 break;
354 case 's':
356 DriveInfo *dinfo;
357 QTAILQ_FOREACH(dinfo, &drives, next) {
358 bdrv_commit(dinfo->bdrv);
361 break;
362 case 'b':
363 qemu_chr_event(chr, CHR_EVENT_BREAK);
364 break;
365 case 'c':
366 /* Switch to the next registered device */
367 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
368 d->focus++;
369 if (d->focus >= d->mux_cnt)
370 d->focus = 0;
371 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
372 break;
373 case 't':
374 d->timestamps = !d->timestamps;
375 d->timestamps_start = -1;
376 d->linestart = 0;
377 break;
379 } else if (ch == term_escape_char) {
380 d->term_got_escape = 1;
381 } else {
382 send_char:
383 return 1;
385 return 0;
388 static void mux_chr_accept_input(CharDriverState *chr)
390 MuxDriver *d = chr->opaque;
391 int m = d->focus;
393 while (d->prod[m] != d->cons[m] &&
394 d->chr_can_read[m] &&
395 d->chr_can_read[m](d->ext_opaque[m])) {
396 d->chr_read[m](d->ext_opaque[m],
397 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
401 static int mux_chr_can_read(void *opaque)
403 CharDriverState *chr = opaque;
404 MuxDriver *d = chr->opaque;
405 int m = d->focus;
407 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
408 return 1;
409 if (d->chr_can_read[m])
410 return d->chr_can_read[m](d->ext_opaque[m]);
411 return 0;
414 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
416 CharDriverState *chr = opaque;
417 MuxDriver *d = chr->opaque;
418 int m = d->focus;
419 int i;
421 mux_chr_accept_input (opaque);
423 for(i = 0; i < size; i++)
424 if (mux_proc_byte(chr, d, buf[i])) {
425 if (d->prod[m] == d->cons[m] &&
426 d->chr_can_read[m] &&
427 d->chr_can_read[m](d->ext_opaque[m]))
428 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
429 else
430 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
434 static void mux_chr_event(void *opaque, int event)
436 CharDriverState *chr = opaque;
437 MuxDriver *d = chr->opaque;
438 int i;
440 /* Send the event to all registered listeners */
441 for (i = 0; i < d->mux_cnt; i++)
442 mux_chr_send_event(d, i, event);
445 static void mux_chr_update_read_handler(CharDriverState *chr)
447 MuxDriver *d = chr->opaque;
449 if (d->mux_cnt >= MAX_MUX) {
450 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
451 return;
453 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
454 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
455 d->chr_read[d->mux_cnt] = chr->chr_read;
456 d->chr_event[d->mux_cnt] = chr->chr_event;
457 /* Fix up the real driver with mux routines */
458 if (d->mux_cnt == 0) {
459 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
460 mux_chr_event, chr);
462 if (d->focus != -1) {
463 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
465 d->focus = d->mux_cnt;
466 d->mux_cnt++;
467 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
470 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
472 CharDriverState *chr;
473 MuxDriver *d;
475 chr = qemu_mallocz(sizeof(CharDriverState));
476 d = qemu_mallocz(sizeof(MuxDriver));
478 chr->opaque = d;
479 d->drv = drv;
480 d->focus = -1;
481 chr->chr_write = mux_chr_write;
482 chr->chr_update_read_handler = mux_chr_update_read_handler;
483 chr->chr_accept_input = mux_chr_accept_input;
484 return chr;
488 #ifdef _WIN32
489 int send_all(int fd, const void *buf, int len1)
491 int ret, len;
493 len = len1;
494 while (len > 0) {
495 ret = send(fd, buf, len, 0);
496 if (ret < 0) {
497 errno = WSAGetLastError();
498 if (errno != WSAEWOULDBLOCK) {
499 return -1;
501 } else if (ret == 0) {
502 break;
503 } else {
504 buf += ret;
505 len -= ret;
508 return len1 - len;
511 #else
513 static int unix_write(int fd, const uint8_t *buf, int len1)
515 int ret, len;
517 len = len1;
518 while (len > 0) {
519 ret = write(fd, buf, len);
520 if (ret < 0) {
521 if (errno != EINTR && errno != EAGAIN)
522 return -1;
523 } else if (ret == 0) {
524 break;
525 } else {
526 buf += ret;
527 len -= ret;
530 return len1 - len;
533 int send_all(int fd, const void *buf, int len1)
535 return unix_write(fd, buf, len1);
537 #endif /* !_WIN32 */
539 #ifndef _WIN32
541 typedef struct {
542 int fd_in, fd_out;
543 int max_size;
544 } FDCharDriver;
546 #define STDIO_MAX_CLIENTS 1
547 static int stdio_nb_clients = 0;
549 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
551 FDCharDriver *s = chr->opaque;
552 return send_all(s->fd_out, buf, len);
555 static int fd_chr_read_poll(void *opaque)
557 CharDriverState *chr = opaque;
558 FDCharDriver *s = chr->opaque;
560 s->max_size = qemu_chr_can_read(chr);
561 return s->max_size;
564 static void fd_chr_read(void *opaque)
566 CharDriverState *chr = opaque;
567 FDCharDriver *s = chr->opaque;
568 int size, len;
569 uint8_t buf[1024];
571 len = sizeof(buf);
572 if (len > s->max_size)
573 len = s->max_size;
574 if (len == 0)
575 return;
576 size = read(s->fd_in, buf, len);
577 if (size == 0) {
578 /* FD has been closed. Remove it from the active list. */
579 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
580 qemu_chr_event(chr, CHR_EVENT_CLOSED);
581 return;
583 if (size > 0) {
584 qemu_chr_read(chr, buf, size);
588 static void fd_chr_update_read_handler(CharDriverState *chr)
590 FDCharDriver *s = chr->opaque;
592 if (s->fd_in >= 0) {
593 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
594 } else {
595 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
596 fd_chr_read, NULL, chr);
601 static void fd_chr_close(struct CharDriverState *chr)
603 FDCharDriver *s = chr->opaque;
605 if (s->fd_in >= 0) {
606 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
607 } else {
608 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
612 qemu_free(s);
613 qemu_chr_event(chr, CHR_EVENT_CLOSED);
616 /* open a character device to a unix fd */
617 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
619 CharDriverState *chr;
620 FDCharDriver *s;
622 chr = qemu_mallocz(sizeof(CharDriverState));
623 s = qemu_mallocz(sizeof(FDCharDriver));
624 s->fd_in = fd_in;
625 s->fd_out = fd_out;
626 chr->opaque = s;
627 chr->chr_write = fd_chr_write;
628 chr->chr_update_read_handler = fd_chr_update_read_handler;
629 chr->chr_close = fd_chr_close;
631 qemu_chr_reset(chr);
633 return chr;
636 static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
638 int fd_out;
640 TFR(fd_out = open(qemu_opt_get(opts, "path"),
641 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
642 if (fd_out < 0)
643 return NULL;
644 return qemu_chr_open_fd(-1, fd_out);
647 static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
649 int fd_in, fd_out;
650 char filename_in[256], filename_out[256];
651 const char *filename = qemu_opt_get(opts, "path");
653 if (filename == NULL) {
654 fprintf(stderr, "chardev: pipe: no filename given\n");
655 return NULL;
658 snprintf(filename_in, 256, "%s.in", filename);
659 snprintf(filename_out, 256, "%s.out", filename);
660 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
661 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
662 if (fd_in < 0 || fd_out < 0) {
663 if (fd_in >= 0)
664 close(fd_in);
665 if (fd_out >= 0)
666 close(fd_out);
667 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
668 if (fd_in < 0)
669 return NULL;
671 return qemu_chr_open_fd(fd_in, fd_out);
675 /* for STDIO, we handle the case where several clients use it
676 (nographic mode) */
678 #define TERM_FIFO_MAX_SIZE 1
680 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
681 static int term_fifo_size;
683 static int stdio_read_poll(void *opaque)
685 CharDriverState *chr = opaque;
687 /* try to flush the queue if needed */
688 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
689 qemu_chr_read(chr, term_fifo, 1);
690 term_fifo_size = 0;
692 /* see if we can absorb more chars */
693 if (term_fifo_size == 0)
694 return 1;
695 else
696 return 0;
699 static void stdio_read(void *opaque)
701 int size;
702 uint8_t buf[1];
703 CharDriverState *chr = opaque;
705 size = read(0, buf, 1);
706 if (size == 0) {
707 /* stdin has been closed. Remove it from the active list. */
708 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
709 qemu_chr_event(chr, CHR_EVENT_CLOSED);
710 return;
712 if (size > 0) {
713 if (qemu_chr_can_read(chr) > 0) {
714 qemu_chr_read(chr, buf, 1);
715 } else if (term_fifo_size == 0) {
716 term_fifo[term_fifo_size++] = buf[0];
721 /* init terminal so that we can grab keys */
722 static struct termios oldtty;
723 static int old_fd0_flags;
724 static int term_atexit_done;
726 static void term_exit(void)
728 tcsetattr (0, TCSANOW, &oldtty);
729 fcntl(0, F_SETFL, old_fd0_flags);
732 static void term_init(void)
734 struct termios tty;
736 tcgetattr (0, &tty);
737 oldtty = tty;
738 old_fd0_flags = fcntl(0, F_GETFL);
740 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
741 |INLCR|IGNCR|ICRNL|IXON);
742 tty.c_oflag |= OPOST;
743 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
744 /* if graphical mode, we allow Ctrl-C handling */
745 if (display_type == DT_NOGRAPHIC)
746 tty.c_lflag &= ~ISIG;
747 tty.c_cflag &= ~(CSIZE|PARENB);
748 tty.c_cflag |= CS8;
749 tty.c_cc[VMIN] = 1;
750 tty.c_cc[VTIME] = 0;
752 tcsetattr (0, TCSANOW, &tty);
754 if (!term_atexit_done++)
755 atexit(term_exit);
757 fcntl(0, F_SETFL, O_NONBLOCK);
760 static void qemu_chr_close_stdio(struct CharDriverState *chr)
762 term_exit();
763 stdio_nb_clients--;
764 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
765 fd_chr_close(chr);
768 static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
770 CharDriverState *chr;
772 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
773 return NULL;
774 chr = qemu_chr_open_fd(0, 1);
775 chr->chr_close = qemu_chr_close_stdio;
776 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
777 stdio_nb_clients++;
778 term_init();
780 return chr;
783 #ifdef __sun__
784 /* Once Solaris has openpty(), this is going to be removed. */
785 static int openpty(int *amaster, int *aslave, char *name,
786 struct termios *termp, struct winsize *winp)
788 const char *slave;
789 int mfd = -1, sfd = -1;
791 *amaster = *aslave = -1;
793 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
794 if (mfd < 0)
795 goto err;
797 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
798 goto err;
800 if ((slave = ptsname(mfd)) == NULL)
801 goto err;
803 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
804 goto err;
806 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
807 (termp != NULL && tcgetattr(sfd, termp) < 0))
808 goto err;
810 if (amaster)
811 *amaster = mfd;
812 if (aslave)
813 *aslave = sfd;
814 if (winp)
815 ioctl(sfd, TIOCSWINSZ, winp);
817 return 0;
819 err:
820 if (sfd != -1)
821 close(sfd);
822 close(mfd);
823 return -1;
826 static void cfmakeraw (struct termios *termios_p)
828 termios_p->c_iflag &=
829 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
830 termios_p->c_oflag &= ~OPOST;
831 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
832 termios_p->c_cflag &= ~(CSIZE|PARENB);
833 termios_p->c_cflag |= CS8;
835 termios_p->c_cc[VMIN] = 0;
836 termios_p->c_cc[VTIME] = 0;
838 #endif
840 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
841 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
843 typedef struct {
844 int fd;
845 int connected;
846 int polling;
847 int read_bytes;
848 QEMUTimer *timer;
849 } PtyCharDriver;
851 static void pty_chr_update_read_handler(CharDriverState *chr);
852 static void pty_chr_state(CharDriverState *chr, int connected);
854 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
856 PtyCharDriver *s = chr->opaque;
858 if (!s->connected) {
859 /* guest sends data, check for (re-)connect */
860 pty_chr_update_read_handler(chr);
861 return 0;
863 return send_all(s->fd, buf, len);
866 static int pty_chr_read_poll(void *opaque)
868 CharDriverState *chr = opaque;
869 PtyCharDriver *s = chr->opaque;
871 s->read_bytes = qemu_chr_can_read(chr);
872 return s->read_bytes;
875 static void pty_chr_read(void *opaque)
877 CharDriverState *chr = opaque;
878 PtyCharDriver *s = chr->opaque;
879 int size, len;
880 uint8_t buf[1024];
882 len = sizeof(buf);
883 if (len > s->read_bytes)
884 len = s->read_bytes;
885 if (len == 0)
886 return;
887 size = read(s->fd, buf, len);
888 if ((size == -1 && errno == EIO) ||
889 (size == 0)) {
890 pty_chr_state(chr, 0);
891 return;
893 if (size > 0) {
894 pty_chr_state(chr, 1);
895 qemu_chr_read(chr, buf, size);
899 static void pty_chr_update_read_handler(CharDriverState *chr)
901 PtyCharDriver *s = chr->opaque;
903 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
904 pty_chr_read, NULL, chr);
905 s->polling = 1;
907 * Short timeout here: just need wait long enougth that qemu makes
908 * it through the poll loop once. When reconnected we want a
909 * short timeout so we notice it almost instantly. Otherwise
910 * read() gives us -EIO instantly, making pty_chr_state() reset the
911 * timeout to the normal (much longer) poll interval before the
912 * timer triggers.
914 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
917 static void pty_chr_state(CharDriverState *chr, int connected)
919 PtyCharDriver *s = chr->opaque;
921 if (!connected) {
922 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
923 s->connected = 0;
924 s->polling = 0;
925 /* (re-)connect poll interval for idle guests: once per second.
926 * We check more frequently in case the guests sends data to
927 * the virtual device linked to our pty. */
928 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
929 } else {
930 if (!s->connected)
931 qemu_chr_reset(chr);
932 s->connected = 1;
936 static void pty_chr_timer(void *opaque)
938 struct CharDriverState *chr = opaque;
939 PtyCharDriver *s = chr->opaque;
941 if (s->connected)
942 return;
943 if (s->polling) {
944 /* If we arrive here without polling being cleared due
945 * read returning -EIO, then we are (re-)connected */
946 pty_chr_state(chr, 1);
947 return;
950 /* Next poll ... */
951 pty_chr_update_read_handler(chr);
954 static void pty_chr_close(struct CharDriverState *chr)
956 PtyCharDriver *s = chr->opaque;
958 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
959 close(s->fd);
960 qemu_del_timer(s->timer);
961 qemu_free_timer(s->timer);
962 qemu_free(s);
963 qemu_chr_event(chr, CHR_EVENT_CLOSED);
966 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
968 CharDriverState *chr;
969 PtyCharDriver *s;
970 struct termios tty;
971 int slave_fd, len;
972 #if defined(__OpenBSD__) || defined(__DragonFly__)
973 char pty_name[PATH_MAX];
974 #define q_ptsname(x) pty_name
975 #else
976 char *pty_name = NULL;
977 #define q_ptsname(x) ptsname(x)
978 #endif
980 chr = qemu_mallocz(sizeof(CharDriverState));
981 s = qemu_mallocz(sizeof(PtyCharDriver));
983 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
984 return NULL;
987 /* Set raw attributes on the pty. */
988 tcgetattr(slave_fd, &tty);
989 cfmakeraw(&tty);
990 tcsetattr(slave_fd, TCSAFLUSH, &tty);
991 close(slave_fd);
993 len = strlen(q_ptsname(s->fd)) + 5;
994 chr->filename = qemu_malloc(len);
995 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
996 qemu_opt_set(opts, "path", q_ptsname(s->fd));
997 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
999 chr->opaque = s;
1000 chr->chr_write = pty_chr_write;
1001 chr->chr_update_read_handler = pty_chr_update_read_handler;
1002 chr->chr_close = pty_chr_close;
1004 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
1006 return chr;
1009 static void tty_serial_init(int fd, int speed,
1010 int parity, int data_bits, int stop_bits)
1012 struct termios tty;
1013 speed_t spd;
1015 #if 0
1016 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1017 speed, parity, data_bits, stop_bits);
1018 #endif
1019 tcgetattr (fd, &tty);
1021 #define MARGIN 1.1
1022 if (speed <= 50 * MARGIN)
1023 spd = B50;
1024 else if (speed <= 75 * MARGIN)
1025 spd = B75;
1026 else if (speed <= 300 * MARGIN)
1027 spd = B300;
1028 else if (speed <= 600 * MARGIN)
1029 spd = B600;
1030 else if (speed <= 1200 * MARGIN)
1031 spd = B1200;
1032 else if (speed <= 2400 * MARGIN)
1033 spd = B2400;
1034 else if (speed <= 4800 * MARGIN)
1035 spd = B4800;
1036 else if (speed <= 9600 * MARGIN)
1037 spd = B9600;
1038 else if (speed <= 19200 * MARGIN)
1039 spd = B19200;
1040 else if (speed <= 38400 * MARGIN)
1041 spd = B38400;
1042 else if (speed <= 57600 * MARGIN)
1043 spd = B57600;
1044 else if (speed <= 115200 * MARGIN)
1045 spd = B115200;
1046 else
1047 spd = B115200;
1049 cfsetispeed(&tty, spd);
1050 cfsetospeed(&tty, spd);
1052 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1053 |INLCR|IGNCR|ICRNL|IXON);
1054 tty.c_oflag |= OPOST;
1055 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1056 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1057 switch(data_bits) {
1058 default:
1059 case 8:
1060 tty.c_cflag |= CS8;
1061 break;
1062 case 7:
1063 tty.c_cflag |= CS7;
1064 break;
1065 case 6:
1066 tty.c_cflag |= CS6;
1067 break;
1068 case 5:
1069 tty.c_cflag |= CS5;
1070 break;
1072 switch(parity) {
1073 default:
1074 case 'N':
1075 break;
1076 case 'E':
1077 tty.c_cflag |= PARENB;
1078 break;
1079 case 'O':
1080 tty.c_cflag |= PARENB | PARODD;
1081 break;
1083 if (stop_bits == 2)
1084 tty.c_cflag |= CSTOPB;
1086 tcsetattr (fd, TCSANOW, &tty);
1089 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1091 FDCharDriver *s = chr->opaque;
1093 switch(cmd) {
1094 case CHR_IOCTL_SERIAL_SET_PARAMS:
1096 QEMUSerialSetParams *ssp = arg;
1097 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1098 ssp->data_bits, ssp->stop_bits);
1100 break;
1101 case CHR_IOCTL_SERIAL_SET_BREAK:
1103 int enable = *(int *)arg;
1104 if (enable)
1105 tcsendbreak(s->fd_in, 1);
1107 break;
1108 case CHR_IOCTL_SERIAL_GET_TIOCM:
1110 int sarg = 0;
1111 int *targ = (int *)arg;
1112 ioctl(s->fd_in, TIOCMGET, &sarg);
1113 *targ = 0;
1114 if (sarg & TIOCM_CTS)
1115 *targ |= CHR_TIOCM_CTS;
1116 if (sarg & TIOCM_CAR)
1117 *targ |= CHR_TIOCM_CAR;
1118 if (sarg & TIOCM_DSR)
1119 *targ |= CHR_TIOCM_DSR;
1120 if (sarg & TIOCM_RI)
1121 *targ |= CHR_TIOCM_RI;
1122 if (sarg & TIOCM_DTR)
1123 *targ |= CHR_TIOCM_DTR;
1124 if (sarg & TIOCM_RTS)
1125 *targ |= CHR_TIOCM_RTS;
1127 break;
1128 case CHR_IOCTL_SERIAL_SET_TIOCM:
1130 int sarg = *(int *)arg;
1131 int targ = 0;
1132 ioctl(s->fd_in, TIOCMGET, &targ);
1133 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1134 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1135 if (sarg & CHR_TIOCM_CTS)
1136 targ |= TIOCM_CTS;
1137 if (sarg & CHR_TIOCM_CAR)
1138 targ |= TIOCM_CAR;
1139 if (sarg & CHR_TIOCM_DSR)
1140 targ |= TIOCM_DSR;
1141 if (sarg & CHR_TIOCM_RI)
1142 targ |= TIOCM_RI;
1143 if (sarg & CHR_TIOCM_DTR)
1144 targ |= TIOCM_DTR;
1145 if (sarg & CHR_TIOCM_RTS)
1146 targ |= TIOCM_RTS;
1147 ioctl(s->fd_in, TIOCMSET, &targ);
1149 break;
1150 default:
1151 return -ENOTSUP;
1153 return 0;
1156 static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
1158 const char *filename = qemu_opt_get(opts, "path");
1159 CharDriverState *chr;
1160 int fd;
1162 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1163 tty_serial_init(fd, 115200, 'N', 8, 1);
1164 chr = qemu_chr_open_fd(fd, fd);
1165 if (!chr) {
1166 close(fd);
1167 return NULL;
1169 chr->chr_ioctl = tty_serial_ioctl;
1170 qemu_chr_reset(chr);
1171 return chr;
1173 #else /* ! __linux__ && ! __sun__ */
1174 static CharDriverState *qemu_chr_open_pty(void)
1176 return NULL;
1178 #endif /* __linux__ || __sun__ */
1180 #if defined(__linux__)
1181 typedef struct {
1182 int fd;
1183 int mode;
1184 } ParallelCharDriver;
1186 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1188 if (s->mode != mode) {
1189 int m = mode;
1190 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1191 return 0;
1192 s->mode = mode;
1194 return 1;
1197 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1199 ParallelCharDriver *drv = chr->opaque;
1200 int fd = drv->fd;
1201 uint8_t b;
1203 switch(cmd) {
1204 case CHR_IOCTL_PP_READ_DATA:
1205 if (ioctl(fd, PPRDATA, &b) < 0)
1206 return -ENOTSUP;
1207 *(uint8_t *)arg = b;
1208 break;
1209 case CHR_IOCTL_PP_WRITE_DATA:
1210 b = *(uint8_t *)arg;
1211 if (ioctl(fd, PPWDATA, &b) < 0)
1212 return -ENOTSUP;
1213 break;
1214 case CHR_IOCTL_PP_READ_CONTROL:
1215 if (ioctl(fd, PPRCONTROL, &b) < 0)
1216 return -ENOTSUP;
1217 /* Linux gives only the lowest bits, and no way to know data
1218 direction! For better compatibility set the fixed upper
1219 bits. */
1220 *(uint8_t *)arg = b | 0xc0;
1221 break;
1222 case CHR_IOCTL_PP_WRITE_CONTROL:
1223 b = *(uint8_t *)arg;
1224 if (ioctl(fd, PPWCONTROL, &b) < 0)
1225 return -ENOTSUP;
1226 break;
1227 case CHR_IOCTL_PP_READ_STATUS:
1228 if (ioctl(fd, PPRSTATUS, &b) < 0)
1229 return -ENOTSUP;
1230 *(uint8_t *)arg = b;
1231 break;
1232 case CHR_IOCTL_PP_DATA_DIR:
1233 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1234 return -ENOTSUP;
1235 break;
1236 case CHR_IOCTL_PP_EPP_READ_ADDR:
1237 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1238 struct ParallelIOArg *parg = arg;
1239 int n = read(fd, parg->buffer, parg->count);
1240 if (n != parg->count) {
1241 return -EIO;
1244 break;
1245 case CHR_IOCTL_PP_EPP_READ:
1246 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1247 struct ParallelIOArg *parg = arg;
1248 int n = read(fd, parg->buffer, parg->count);
1249 if (n != parg->count) {
1250 return -EIO;
1253 break;
1254 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1255 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1256 struct ParallelIOArg *parg = arg;
1257 int n = write(fd, parg->buffer, parg->count);
1258 if (n != parg->count) {
1259 return -EIO;
1262 break;
1263 case CHR_IOCTL_PP_EPP_WRITE:
1264 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1265 struct ParallelIOArg *parg = arg;
1266 int n = write(fd, parg->buffer, parg->count);
1267 if (n != parg->count) {
1268 return -EIO;
1271 break;
1272 default:
1273 return -ENOTSUP;
1275 return 0;
1278 static void pp_close(CharDriverState *chr)
1280 ParallelCharDriver *drv = chr->opaque;
1281 int fd = drv->fd;
1283 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1284 ioctl(fd, PPRELEASE);
1285 close(fd);
1286 qemu_free(drv);
1287 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1290 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1292 const char *filename = qemu_opt_get(opts, "path");
1293 CharDriverState *chr;
1294 ParallelCharDriver *drv;
1295 int fd;
1297 TFR(fd = open(filename, O_RDWR));
1298 if (fd < 0)
1299 return NULL;
1301 if (ioctl(fd, PPCLAIM) < 0) {
1302 close(fd);
1303 return NULL;
1306 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1307 drv->fd = fd;
1308 drv->mode = IEEE1284_MODE_COMPAT;
1310 chr = qemu_mallocz(sizeof(CharDriverState));
1311 chr->chr_write = null_chr_write;
1312 chr->chr_ioctl = pp_ioctl;
1313 chr->chr_close = pp_close;
1314 chr->opaque = drv;
1316 qemu_chr_reset(chr);
1318 return chr;
1320 #endif /* __linux__ */
1322 #if defined(__FreeBSD__) || defined(__DragonFly__)
1323 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1325 int fd = (int)chr->opaque;
1326 uint8_t b;
1328 switch(cmd) {
1329 case CHR_IOCTL_PP_READ_DATA:
1330 if (ioctl(fd, PPIGDATA, &b) < 0)
1331 return -ENOTSUP;
1332 *(uint8_t *)arg = b;
1333 break;
1334 case CHR_IOCTL_PP_WRITE_DATA:
1335 b = *(uint8_t *)arg;
1336 if (ioctl(fd, PPISDATA, &b) < 0)
1337 return -ENOTSUP;
1338 break;
1339 case CHR_IOCTL_PP_READ_CONTROL:
1340 if (ioctl(fd, PPIGCTRL, &b) < 0)
1341 return -ENOTSUP;
1342 *(uint8_t *)arg = b;
1343 break;
1344 case CHR_IOCTL_PP_WRITE_CONTROL:
1345 b = *(uint8_t *)arg;
1346 if (ioctl(fd, PPISCTRL, &b) < 0)
1347 return -ENOTSUP;
1348 break;
1349 case CHR_IOCTL_PP_READ_STATUS:
1350 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1351 return -ENOTSUP;
1352 *(uint8_t *)arg = b;
1353 break;
1354 default:
1355 return -ENOTSUP;
1357 return 0;
1360 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1362 const char *filename = qemu_opt_get(opts, "path");
1363 CharDriverState *chr;
1364 int fd;
1366 fd = open(filename, O_RDWR);
1367 if (fd < 0)
1368 return NULL;
1370 chr = qemu_mallocz(sizeof(CharDriverState));
1371 chr->opaque = (void *)fd;
1372 chr->chr_write = null_chr_write;
1373 chr->chr_ioctl = pp_ioctl;
1374 return chr;
1376 #endif
1378 #else /* _WIN32 */
1380 typedef struct {
1381 int max_size;
1382 HANDLE hcom, hrecv, hsend;
1383 OVERLAPPED orecv, osend;
1384 BOOL fpipe;
1385 DWORD len;
1386 } WinCharState;
1388 #define NSENDBUF 2048
1389 #define NRECVBUF 2048
1390 #define MAXCONNECT 1
1391 #define NTIMEOUT 5000
1393 static int win_chr_poll(void *opaque);
1394 static int win_chr_pipe_poll(void *opaque);
1396 static void win_chr_close(CharDriverState *chr)
1398 WinCharState *s = chr->opaque;
1400 if (s->hsend) {
1401 CloseHandle(s->hsend);
1402 s->hsend = NULL;
1404 if (s->hrecv) {
1405 CloseHandle(s->hrecv);
1406 s->hrecv = NULL;
1408 if (s->hcom) {
1409 CloseHandle(s->hcom);
1410 s->hcom = NULL;
1412 if (s->fpipe)
1413 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1414 else
1415 qemu_del_polling_cb(win_chr_poll, chr);
1417 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1420 static int win_chr_init(CharDriverState *chr, const char *filename)
1422 WinCharState *s = chr->opaque;
1423 COMMCONFIG comcfg;
1424 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1425 COMSTAT comstat;
1426 DWORD size;
1427 DWORD err;
1429 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1430 if (!s->hsend) {
1431 fprintf(stderr, "Failed CreateEvent\n");
1432 goto fail;
1434 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1435 if (!s->hrecv) {
1436 fprintf(stderr, "Failed CreateEvent\n");
1437 goto fail;
1440 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1441 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1442 if (s->hcom == INVALID_HANDLE_VALUE) {
1443 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1444 s->hcom = NULL;
1445 goto fail;
1448 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1449 fprintf(stderr, "Failed SetupComm\n");
1450 goto fail;
1453 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1454 size = sizeof(COMMCONFIG);
1455 GetDefaultCommConfig(filename, &comcfg, &size);
1456 comcfg.dcb.DCBlength = sizeof(DCB);
1457 CommConfigDialog(filename, NULL, &comcfg);
1459 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1460 fprintf(stderr, "Failed SetCommState\n");
1461 goto fail;
1464 if (!SetCommMask(s->hcom, EV_ERR)) {
1465 fprintf(stderr, "Failed SetCommMask\n");
1466 goto fail;
1469 cto.ReadIntervalTimeout = MAXDWORD;
1470 if (!SetCommTimeouts(s->hcom, &cto)) {
1471 fprintf(stderr, "Failed SetCommTimeouts\n");
1472 goto fail;
1475 if (!ClearCommError(s->hcom, &err, &comstat)) {
1476 fprintf(stderr, "Failed ClearCommError\n");
1477 goto fail;
1479 qemu_add_polling_cb(win_chr_poll, chr);
1480 return 0;
1482 fail:
1483 win_chr_close(chr);
1484 return -1;
1487 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1489 WinCharState *s = chr->opaque;
1490 DWORD len, ret, size, err;
1492 len = len1;
1493 ZeroMemory(&s->osend, sizeof(s->osend));
1494 s->osend.hEvent = s->hsend;
1495 while (len > 0) {
1496 if (s->hsend)
1497 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1498 else
1499 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1500 if (!ret) {
1501 err = GetLastError();
1502 if (err == ERROR_IO_PENDING) {
1503 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1504 if (ret) {
1505 buf += size;
1506 len -= size;
1507 } else {
1508 break;
1510 } else {
1511 break;
1513 } else {
1514 buf += size;
1515 len -= size;
1518 return len1 - len;
1521 static int win_chr_read_poll(CharDriverState *chr)
1523 WinCharState *s = chr->opaque;
1525 s->max_size = qemu_chr_can_read(chr);
1526 return s->max_size;
1529 static void win_chr_readfile(CharDriverState *chr)
1531 WinCharState *s = chr->opaque;
1532 int ret, err;
1533 uint8_t buf[1024];
1534 DWORD size;
1536 ZeroMemory(&s->orecv, sizeof(s->orecv));
1537 s->orecv.hEvent = s->hrecv;
1538 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1539 if (!ret) {
1540 err = GetLastError();
1541 if (err == ERROR_IO_PENDING) {
1542 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1546 if (size > 0) {
1547 qemu_chr_read(chr, buf, size);
1551 static void win_chr_read(CharDriverState *chr)
1553 WinCharState *s = chr->opaque;
1555 if (s->len > s->max_size)
1556 s->len = s->max_size;
1557 if (s->len == 0)
1558 return;
1560 win_chr_readfile(chr);
1563 static int win_chr_poll(void *opaque)
1565 CharDriverState *chr = opaque;
1566 WinCharState *s = chr->opaque;
1567 COMSTAT status;
1568 DWORD comerr;
1570 ClearCommError(s->hcom, &comerr, &status);
1571 if (status.cbInQue > 0) {
1572 s->len = status.cbInQue;
1573 win_chr_read_poll(chr);
1574 win_chr_read(chr);
1575 return 1;
1577 return 0;
1580 static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1582 const char *filename = qemu_opt_get(opts, "path");
1583 CharDriverState *chr;
1584 WinCharState *s;
1586 chr = qemu_mallocz(sizeof(CharDriverState));
1587 s = qemu_mallocz(sizeof(WinCharState));
1588 chr->opaque = s;
1589 chr->chr_write = win_chr_write;
1590 chr->chr_close = win_chr_close;
1592 if (win_chr_init(chr, filename) < 0) {
1593 free(s);
1594 free(chr);
1595 return NULL;
1597 qemu_chr_reset(chr);
1598 return chr;
1601 static int win_chr_pipe_poll(void *opaque)
1603 CharDriverState *chr = opaque;
1604 WinCharState *s = chr->opaque;
1605 DWORD size;
1607 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1608 if (size > 0) {
1609 s->len = size;
1610 win_chr_read_poll(chr);
1611 win_chr_read(chr);
1612 return 1;
1614 return 0;
1617 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1619 WinCharState *s = chr->opaque;
1620 OVERLAPPED ov;
1621 int ret;
1622 DWORD size;
1623 char openname[256];
1625 s->fpipe = TRUE;
1627 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1628 if (!s->hsend) {
1629 fprintf(stderr, "Failed CreateEvent\n");
1630 goto fail;
1632 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1633 if (!s->hrecv) {
1634 fprintf(stderr, "Failed CreateEvent\n");
1635 goto fail;
1638 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1639 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1640 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1641 PIPE_WAIT,
1642 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1643 if (s->hcom == INVALID_HANDLE_VALUE) {
1644 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1645 s->hcom = NULL;
1646 goto fail;
1649 ZeroMemory(&ov, sizeof(ov));
1650 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1651 ret = ConnectNamedPipe(s->hcom, &ov);
1652 if (ret) {
1653 fprintf(stderr, "Failed ConnectNamedPipe\n");
1654 goto fail;
1657 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1658 if (!ret) {
1659 fprintf(stderr, "Failed GetOverlappedResult\n");
1660 if (ov.hEvent) {
1661 CloseHandle(ov.hEvent);
1662 ov.hEvent = NULL;
1664 goto fail;
1667 if (ov.hEvent) {
1668 CloseHandle(ov.hEvent);
1669 ov.hEvent = NULL;
1671 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1672 return 0;
1674 fail:
1675 win_chr_close(chr);
1676 return -1;
1680 static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
1682 const char *filename = qemu_opt_get(opts, "path");
1683 CharDriverState *chr;
1684 WinCharState *s;
1686 chr = qemu_mallocz(sizeof(CharDriverState));
1687 s = qemu_mallocz(sizeof(WinCharState));
1688 chr->opaque = s;
1689 chr->chr_write = win_chr_write;
1690 chr->chr_close = win_chr_close;
1692 if (win_chr_pipe_init(chr, filename) < 0) {
1693 free(s);
1694 free(chr);
1695 return NULL;
1697 qemu_chr_reset(chr);
1698 return chr;
1701 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1703 CharDriverState *chr;
1704 WinCharState *s;
1706 chr = qemu_mallocz(sizeof(CharDriverState));
1707 s = qemu_mallocz(sizeof(WinCharState));
1708 s->hcom = fd_out;
1709 chr->opaque = s;
1710 chr->chr_write = win_chr_write;
1711 qemu_chr_reset(chr);
1712 return chr;
1715 static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
1717 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1720 static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
1722 const char *file_out = qemu_opt_get(opts, "path");
1723 HANDLE fd_out;
1725 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1726 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1727 if (fd_out == INVALID_HANDLE_VALUE)
1728 return NULL;
1730 return qemu_chr_open_win_file(fd_out);
1732 #endif /* !_WIN32 */
1734 /***********************************************************/
1735 /* UDP Net console */
1737 typedef struct {
1738 int fd;
1739 uint8_t buf[1024];
1740 int bufcnt;
1741 int bufptr;
1742 int max_size;
1743 } NetCharDriver;
1745 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1747 NetCharDriver *s = chr->opaque;
1749 return send(s->fd, (const void *)buf, len, 0);
1752 static int udp_chr_read_poll(void *opaque)
1754 CharDriverState *chr = opaque;
1755 NetCharDriver *s = chr->opaque;
1757 s->max_size = qemu_chr_can_read(chr);
1759 /* If there were any stray characters in the queue process them
1760 * first
1762 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1763 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1764 s->bufptr++;
1765 s->max_size = qemu_chr_can_read(chr);
1767 return s->max_size;
1770 static void udp_chr_read(void *opaque)
1772 CharDriverState *chr = opaque;
1773 NetCharDriver *s = chr->opaque;
1775 if (s->max_size == 0)
1776 return;
1777 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1778 s->bufptr = s->bufcnt;
1779 if (s->bufcnt <= 0)
1780 return;
1782 s->bufptr = 0;
1783 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1784 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1785 s->bufptr++;
1786 s->max_size = qemu_chr_can_read(chr);
1790 static void udp_chr_update_read_handler(CharDriverState *chr)
1792 NetCharDriver *s = chr->opaque;
1794 if (s->fd >= 0) {
1795 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1796 udp_chr_read, NULL, chr);
1800 static void udp_chr_close(CharDriverState *chr)
1802 NetCharDriver *s = chr->opaque;
1803 if (s->fd >= 0) {
1804 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1805 closesocket(s->fd);
1807 qemu_free(s);
1808 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1811 static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
1813 CharDriverState *chr = NULL;
1814 NetCharDriver *s = NULL;
1815 int fd = -1;
1817 chr = qemu_mallocz(sizeof(CharDriverState));
1818 s = qemu_mallocz(sizeof(NetCharDriver));
1820 fd = inet_dgram_opts(opts);
1821 if (fd < 0) {
1822 fprintf(stderr, "inet_dgram_opts failed\n");
1823 goto return_err;
1826 s->fd = fd;
1827 s->bufcnt = 0;
1828 s->bufptr = 0;
1829 chr->opaque = s;
1830 chr->chr_write = udp_chr_write;
1831 chr->chr_update_read_handler = udp_chr_update_read_handler;
1832 chr->chr_close = udp_chr_close;
1833 return chr;
1835 return_err:
1836 if (chr)
1837 free(chr);
1838 if (s)
1839 free(s);
1840 if (fd >= 0)
1841 closesocket(fd);
1842 return NULL;
1845 /***********************************************************/
1846 /* TCP Net console */
1848 typedef struct {
1849 int fd, listen_fd;
1850 int connected;
1851 int max_size;
1852 int do_telnetopt;
1853 int do_nodelay;
1854 int is_unix;
1855 int msgfd;
1856 } TCPCharDriver;
1858 static void tcp_chr_accept(void *opaque);
1860 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1862 TCPCharDriver *s = chr->opaque;
1863 if (s->connected) {
1864 return send_all(s->fd, buf, len);
1865 } else {
1866 /* XXX: indicate an error ? */
1867 return len;
1871 static int tcp_chr_read_poll(void *opaque)
1873 CharDriverState *chr = opaque;
1874 TCPCharDriver *s = chr->opaque;
1875 if (!s->connected)
1876 return 0;
1877 s->max_size = qemu_chr_can_read(chr);
1878 return s->max_size;
1881 #define IAC 255
1882 #define IAC_BREAK 243
1883 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1884 TCPCharDriver *s,
1885 uint8_t *buf, int *size)
1887 /* Handle any telnet client's basic IAC options to satisfy char by
1888 * char mode with no echo. All IAC options will be removed from
1889 * the buf and the do_telnetopt variable will be used to track the
1890 * state of the width of the IAC information.
1892 * IAC commands come in sets of 3 bytes with the exception of the
1893 * "IAC BREAK" command and the double IAC.
1896 int i;
1897 int j = 0;
1899 for (i = 0; i < *size; i++) {
1900 if (s->do_telnetopt > 1) {
1901 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1902 /* Double IAC means send an IAC */
1903 if (j != i)
1904 buf[j] = buf[i];
1905 j++;
1906 s->do_telnetopt = 1;
1907 } else {
1908 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1909 /* Handle IAC break commands by sending a serial break */
1910 qemu_chr_event(chr, CHR_EVENT_BREAK);
1911 s->do_telnetopt++;
1913 s->do_telnetopt++;
1915 if (s->do_telnetopt >= 4) {
1916 s->do_telnetopt = 1;
1918 } else {
1919 if ((unsigned char)buf[i] == IAC) {
1920 s->do_telnetopt = 2;
1921 } else {
1922 if (j != i)
1923 buf[j] = buf[i];
1924 j++;
1928 *size = j;
1931 static int tcp_get_msgfd(CharDriverState *chr)
1933 TCPCharDriver *s = chr->opaque;
1935 return s->msgfd;
1938 #ifndef _WIN32
1939 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1941 TCPCharDriver *s = chr->opaque;
1942 struct cmsghdr *cmsg;
1944 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1945 int fd;
1947 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1948 cmsg->cmsg_level != SOL_SOCKET ||
1949 cmsg->cmsg_type != SCM_RIGHTS)
1950 continue;
1952 fd = *((int *)CMSG_DATA(cmsg));
1953 if (fd < 0)
1954 continue;
1956 if (s->msgfd != -1)
1957 close(s->msgfd);
1958 s->msgfd = fd;
1962 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1964 TCPCharDriver *s = chr->opaque;
1965 struct msghdr msg = { NULL, };
1966 struct iovec iov[1];
1967 union {
1968 struct cmsghdr cmsg;
1969 char control[CMSG_SPACE(sizeof(int))];
1970 } msg_control;
1971 ssize_t ret;
1973 iov[0].iov_base = buf;
1974 iov[0].iov_len = len;
1976 msg.msg_iov = iov;
1977 msg.msg_iovlen = 1;
1978 msg.msg_control = &msg_control;
1979 msg.msg_controllen = sizeof(msg_control);
1981 ret = recvmsg(s->fd, &msg, 0);
1982 if (ret > 0 && s->is_unix)
1983 unix_process_msgfd(chr, &msg);
1985 return ret;
1987 #else
1988 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1990 TCPCharDriver *s = chr->opaque;
1991 return recv(s->fd, buf, len, 0);
1993 #endif
1995 static void tcp_chr_read(void *opaque)
1997 CharDriverState *chr = opaque;
1998 TCPCharDriver *s = chr->opaque;
1999 uint8_t buf[1024];
2000 int len, size;
2002 if (!s->connected || s->max_size <= 0)
2003 return;
2004 len = sizeof(buf);
2005 if (len > s->max_size)
2006 len = s->max_size;
2007 size = tcp_chr_recv(chr, (void *)buf, len);
2008 if (size == 0) {
2009 /* connection closed */
2010 s->connected = 0;
2011 if (s->listen_fd >= 0) {
2012 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2014 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2015 closesocket(s->fd);
2016 s->fd = -1;
2017 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2018 } else if (size > 0) {
2019 if (s->do_telnetopt)
2020 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2021 if (size > 0)
2022 qemu_chr_read(chr, buf, size);
2023 if (s->msgfd != -1) {
2024 close(s->msgfd);
2025 s->msgfd = -1;
2030 static void tcp_chr_connect(void *opaque)
2032 CharDriverState *chr = opaque;
2033 TCPCharDriver *s = chr->opaque;
2035 s->connected = 1;
2036 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2037 tcp_chr_read, NULL, chr);
2038 qemu_chr_reset(chr);
2041 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2042 static void tcp_chr_telnet_init(int fd)
2044 char buf[3];
2045 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2046 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2047 send(fd, (char *)buf, 3, 0);
2048 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2049 send(fd, (char *)buf, 3, 0);
2050 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2051 send(fd, (char *)buf, 3, 0);
2052 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2053 send(fd, (char *)buf, 3, 0);
2056 static void socket_set_nodelay(int fd)
2058 int val = 1;
2059 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2062 static void tcp_chr_accept(void *opaque)
2064 CharDriverState *chr = opaque;
2065 TCPCharDriver *s = chr->opaque;
2066 struct sockaddr_in saddr;
2067 #ifndef _WIN32
2068 struct sockaddr_un uaddr;
2069 #endif
2070 struct sockaddr *addr;
2071 socklen_t len;
2072 int fd;
2074 for(;;) {
2075 #ifndef _WIN32
2076 if (s->is_unix) {
2077 len = sizeof(uaddr);
2078 addr = (struct sockaddr *)&uaddr;
2079 } else
2080 #endif
2082 len = sizeof(saddr);
2083 addr = (struct sockaddr *)&saddr;
2085 fd = accept(s->listen_fd, addr, &len);
2086 if (fd < 0 && errno != EINTR) {
2087 return;
2088 } else if (fd >= 0) {
2089 if (s->do_telnetopt)
2090 tcp_chr_telnet_init(fd);
2091 break;
2094 socket_set_nonblock(fd);
2095 if (s->do_nodelay)
2096 socket_set_nodelay(fd);
2097 s->fd = fd;
2098 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2099 tcp_chr_connect(chr);
2102 static void tcp_chr_close(CharDriverState *chr)
2104 TCPCharDriver *s = chr->opaque;
2105 if (s->fd >= 0) {
2106 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2107 closesocket(s->fd);
2109 if (s->listen_fd >= 0) {
2110 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2111 closesocket(s->listen_fd);
2113 qemu_free(s);
2114 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2117 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2119 CharDriverState *chr = NULL;
2120 TCPCharDriver *s = NULL;
2121 int fd = -1;
2122 int is_listen;
2123 int is_waitconnect;
2124 int do_nodelay;
2125 int is_unix;
2126 int is_telnet;
2128 is_listen = qemu_opt_get_bool(opts, "server", 0);
2129 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2130 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2131 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2132 is_unix = qemu_opt_get(opts, "path") != NULL;
2133 if (!is_listen)
2134 is_waitconnect = 0;
2136 chr = qemu_mallocz(sizeof(CharDriverState));
2137 s = qemu_mallocz(sizeof(TCPCharDriver));
2139 if (is_unix) {
2140 if (is_listen) {
2141 fd = unix_listen_opts(opts);
2142 } else {
2143 fd = unix_connect_opts(opts);
2145 } else {
2146 if (is_listen) {
2147 fd = inet_listen_opts(opts, 0);
2148 } else {
2149 fd = inet_connect_opts(opts);
2152 if (fd < 0)
2153 goto fail;
2155 if (!is_waitconnect)
2156 socket_set_nonblock(fd);
2158 s->connected = 0;
2159 s->fd = -1;
2160 s->listen_fd = -1;
2161 s->msgfd = -1;
2162 s->is_unix = is_unix;
2163 s->do_nodelay = do_nodelay && !is_unix;
2165 chr->opaque = s;
2166 chr->chr_write = tcp_chr_write;
2167 chr->chr_close = tcp_chr_close;
2168 chr->get_msgfd = tcp_get_msgfd;
2170 if (is_listen) {
2171 s->listen_fd = fd;
2172 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2173 if (is_telnet)
2174 s->do_telnetopt = 1;
2176 } else {
2177 s->connected = 1;
2178 s->fd = fd;
2179 socket_set_nodelay(fd);
2180 tcp_chr_connect(chr);
2183 /* for "info chardev" monitor command */
2184 chr->filename = qemu_malloc(256);
2185 if (is_unix) {
2186 snprintf(chr->filename, 256, "unix:%s%s",
2187 qemu_opt_get(opts, "path"),
2188 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2189 } else if (is_telnet) {
2190 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2191 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2192 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2193 } else {
2194 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2195 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2196 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2199 if (is_listen && is_waitconnect) {
2200 printf("QEMU waiting for connection on: %s\n",
2201 chr->filename);
2202 tcp_chr_accept(chr);
2203 socket_set_nonblock(s->listen_fd);
2205 return chr;
2207 fail:
2208 if (fd >= 0)
2209 closesocket(fd);
2210 qemu_free(s);
2211 qemu_free(chr);
2212 return NULL;
2215 static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2217 char host[65], port[33], width[8], height[8];
2218 int pos;
2219 const char *p;
2220 QemuOpts *opts;
2222 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2223 if (NULL == opts)
2224 return NULL;
2226 if (strstart(filename, "mon:", &p)) {
2227 filename = p;
2228 qemu_opt_set(opts, "mux", "on");
2231 if (strcmp(filename, "null") == 0 ||
2232 strcmp(filename, "pty") == 0 ||
2233 strcmp(filename, "msmouse") == 0 ||
2234 strcmp(filename, "braille") == 0 ||
2235 strcmp(filename, "stdio") == 0) {
2236 qemu_opt_set(opts, "backend", filename);
2237 return opts;
2239 if (strstart(filename, "vc", &p)) {
2240 qemu_opt_set(opts, "backend", "vc");
2241 if (*p == ':') {
2242 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2243 /* pixels */
2244 qemu_opt_set(opts, "width", width);
2245 qemu_opt_set(opts, "height", height);
2246 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2247 /* chars */
2248 qemu_opt_set(opts, "cols", width);
2249 qemu_opt_set(opts, "rows", height);
2250 } else {
2251 goto fail;
2254 return opts;
2256 if (strcmp(filename, "con:") == 0) {
2257 qemu_opt_set(opts, "backend", "console");
2258 return opts;
2260 if (strstart(filename, "COM", NULL)) {
2261 qemu_opt_set(opts, "backend", "serial");
2262 qemu_opt_set(opts, "path", filename);
2263 return opts;
2265 if (strstart(filename, "file:", &p)) {
2266 qemu_opt_set(opts, "backend", "file");
2267 qemu_opt_set(opts, "path", p);
2268 return opts;
2270 if (strstart(filename, "pipe:", &p)) {
2271 qemu_opt_set(opts, "backend", "pipe");
2272 qemu_opt_set(opts, "path", p);
2273 return opts;
2275 if (strstart(filename, "tcp:", &p) ||
2276 strstart(filename, "telnet:", &p)) {
2277 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2278 host[0] = 0;
2279 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2280 goto fail;
2282 qemu_opt_set(opts, "backend", "socket");
2283 qemu_opt_set(opts, "host", host);
2284 qemu_opt_set(opts, "port", port);
2285 if (p[pos] == ',') {
2286 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2287 goto fail;
2289 if (strstart(filename, "telnet:", &p))
2290 qemu_opt_set(opts, "telnet", "on");
2291 return opts;
2293 if (strstart(filename, "udp:", &p)) {
2294 qemu_opt_set(opts, "backend", "udp");
2295 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2296 host[0] = 0;
2297 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2298 fprintf(stderr, "udp #1\n");
2299 goto fail;
2302 qemu_opt_set(opts, "host", host);
2303 qemu_opt_set(opts, "port", port);
2304 if (p[pos] == '@') {
2305 p += pos + 1;
2306 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2307 host[0] = 0;
2308 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2309 fprintf(stderr, "udp #2\n");
2310 goto fail;
2313 qemu_opt_set(opts, "localaddr", host);
2314 qemu_opt_set(opts, "localport", port);
2316 return opts;
2318 if (strstart(filename, "unix:", &p)) {
2319 qemu_opt_set(opts, "backend", "socket");
2320 if (qemu_opts_do_parse(opts, p, "path") != 0)
2321 goto fail;
2322 return opts;
2324 if (strstart(filename, "/dev/parport", NULL) ||
2325 strstart(filename, "/dev/ppi", NULL)) {
2326 qemu_opt_set(opts, "backend", "parport");
2327 qemu_opt_set(opts, "path", filename);
2328 return opts;
2330 if (strstart(filename, "/dev/", NULL)) {
2331 qemu_opt_set(opts, "backend", "tty");
2332 qemu_opt_set(opts, "path", filename);
2333 return opts;
2336 fail:
2337 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
2338 qemu_opts_del(opts);
2339 return NULL;
2342 static const struct {
2343 const char *name;
2344 CharDriverState *(*open)(QemuOpts *opts);
2345 } backend_table[] = {
2346 { .name = "null", .open = qemu_chr_open_null },
2347 { .name = "socket", .open = qemu_chr_open_socket },
2348 { .name = "udp", .open = qemu_chr_open_udp },
2349 { .name = "msmouse", .open = qemu_chr_open_msmouse },
2350 { .name = "vc", .open = text_console_init },
2351 #ifdef _WIN32
2352 { .name = "file", .open = qemu_chr_open_win_file_out },
2353 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2354 { .name = "console", .open = qemu_chr_open_win_con },
2355 { .name = "serial", .open = qemu_chr_open_win },
2356 #else
2357 { .name = "file", .open = qemu_chr_open_file_out },
2358 { .name = "pipe", .open = qemu_chr_open_pipe },
2359 { .name = "pty", .open = qemu_chr_open_pty },
2360 { .name = "stdio", .open = qemu_chr_open_stdio },
2361 #endif
2362 #ifdef CONFIG_BRLAPI
2363 { .name = "braille", .open = chr_baum_init },
2364 #endif
2365 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2366 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2367 { .name = "tty", .open = qemu_chr_open_tty },
2368 #endif
2369 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2370 { .name = "parport", .open = qemu_chr_open_pp },
2371 #endif
2374 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2375 void (*init)(struct CharDriverState *s))
2377 CharDriverState *chr;
2378 int i;
2380 if (qemu_opts_id(opts) == NULL) {
2381 fprintf(stderr, "chardev: no id specified\n");
2382 return NULL;
2385 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2386 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2387 break;
2389 if (i == ARRAY_SIZE(backend_table)) {
2390 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2391 qemu_opt_get(opts, "backend"));
2392 return NULL;
2395 chr = backend_table[i].open(opts);
2396 if (!chr) {
2397 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2398 qemu_opt_get(opts, "backend"));
2399 return NULL;
2402 if (!chr->filename)
2403 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2404 chr->init = init;
2405 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2407 if (qemu_opt_get_bool(opts, "mux", 0)) {
2408 CharDriverState *base = chr;
2409 int len = strlen(qemu_opts_id(opts)) + 6;
2410 base->label = qemu_malloc(len);
2411 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2412 chr = qemu_chr_open_mux(base);
2413 chr->filename = base->filename;
2414 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2416 chr->label = qemu_strdup(qemu_opts_id(opts));
2417 return chr;
2420 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2422 const char *p;
2423 CharDriverState *chr;
2424 QemuOpts *opts;
2426 if (strstart(filename, "chardev:", &p)) {
2427 return qemu_chr_find(p);
2430 opts = qemu_chr_parse_compat(label, filename);
2431 if (!opts)
2432 return NULL;
2434 chr = qemu_chr_open_opts(opts, init);
2435 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2436 monitor_init(chr, MONITOR_USE_READLINE);
2438 return chr;
2441 void qemu_chr_close(CharDriverState *chr)
2443 QTAILQ_REMOVE(&chardevs, chr, next);
2444 if (chr->chr_close)
2445 chr->chr_close(chr);
2446 qemu_free(chr->filename);
2447 qemu_free(chr->label);
2448 qemu_free(chr);
2451 void qemu_chr_info(Monitor *mon)
2453 CharDriverState *chr;
2455 QTAILQ_FOREACH(chr, &chardevs, next) {
2456 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
2460 CharDriverState *qemu_chr_find(const char *name)
2462 CharDriverState *chr;
2464 QTAILQ_FOREACH(chr, &chardevs, next) {
2465 if (strcmp(chr->label, name) != 0)
2466 continue;
2467 return chr;
2469 return NULL;