convert pty chardev to QemuOpts.
[qemu.git] / qemu-char.c
bloba557b544ac02cc97e36e2a62c983fa4124f01ee2
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 TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
110 TAILQ_HEAD_INITIALIZER(chardevs);
111 static int initial_reset_issued;
113 static void qemu_chr_event(CharDriverState *s, int event)
115 if (!s->chr_event)
116 return;
117 s->chr_event(s->handler_opaque, event);
120 static void qemu_chr_reset_bh(void *opaque)
122 CharDriverState *s = opaque;
123 qemu_chr_event(s, CHR_EVENT_RESET);
124 qemu_bh_delete(s->bh);
125 s->bh = NULL;
128 void qemu_chr_reset(CharDriverState *s)
130 if (s->bh == NULL && initial_reset_issued) {
131 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
132 qemu_bh_schedule(s->bh);
136 void qemu_chr_initial_reset(void)
138 CharDriverState *chr;
140 initial_reset_issued = 1;
142 TAILQ_FOREACH(chr, &chardevs, next) {
143 qemu_chr_reset(chr);
147 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
149 return s->chr_write(s, buf, len);
152 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
154 if (!s->chr_ioctl)
155 return -ENOTSUP;
156 return s->chr_ioctl(s, cmd, arg);
159 int qemu_chr_can_read(CharDriverState *s)
161 if (!s->chr_can_read)
162 return 0;
163 return s->chr_can_read(s->handler_opaque);
166 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
168 s->chr_read(s->handler_opaque, buf, len);
171 int qemu_chr_get_msgfd(CharDriverState *s)
173 return s->get_msgfd ? s->get_msgfd(s) : -1;
176 void qemu_chr_accept_input(CharDriverState *s)
178 if (s->chr_accept_input)
179 s->chr_accept_input(s);
182 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
184 char buf[4096];
185 va_list ap;
186 va_start(ap, fmt);
187 vsnprintf(buf, sizeof(buf), fmt, ap);
188 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
189 va_end(ap);
192 void qemu_chr_send_event(CharDriverState *s, int event)
194 if (s->chr_send_event)
195 s->chr_send_event(s, event);
198 void qemu_chr_add_handlers(CharDriverState *s,
199 IOCanRWHandler *fd_can_read,
200 IOReadHandler *fd_read,
201 IOEventHandler *fd_event,
202 void *opaque)
204 s->chr_can_read = fd_can_read;
205 s->chr_read = fd_read;
206 s->chr_event = fd_event;
207 s->handler_opaque = opaque;
208 if (s->chr_update_read_handler)
209 s->chr_update_read_handler(s);
212 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
214 return len;
217 static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
219 CharDriverState *chr;
221 chr = qemu_mallocz(sizeof(CharDriverState));
222 chr->chr_write = null_chr_write;
223 return chr;
226 /* MUX driver for serial I/O splitting */
227 #define MAX_MUX 4
228 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
229 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
230 typedef struct {
231 IOCanRWHandler *chr_can_read[MAX_MUX];
232 IOReadHandler *chr_read[MAX_MUX];
233 IOEventHandler *chr_event[MAX_MUX];
234 void *ext_opaque[MAX_MUX];
235 CharDriverState *drv;
236 int mux_cnt;
237 int term_got_escape;
238 int max_size;
239 /* Intermediate input buffer allows to catch escape sequences even if the
240 currently active device is not accepting any input - but only until it
241 is full as well. */
242 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
243 int prod[MAX_MUX];
244 int cons[MAX_MUX];
245 int timestamps;
246 int linestart;
247 int64_t timestamps_start;
248 } MuxDriver;
251 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
253 MuxDriver *d = chr->opaque;
254 int ret;
255 if (!d->timestamps) {
256 ret = d->drv->chr_write(d->drv, buf, len);
257 } else {
258 int i;
260 ret = 0;
261 for (i = 0; i < len; i++) {
262 if (d->linestart) {
263 char buf1[64];
264 int64_t ti;
265 int secs;
267 ti = qemu_get_clock(rt_clock);
268 if (d->timestamps_start == -1)
269 d->timestamps_start = ti;
270 ti -= d->timestamps_start;
271 secs = ti / 1000;
272 snprintf(buf1, sizeof(buf1),
273 "[%02d:%02d:%02d.%03d] ",
274 secs / 3600,
275 (secs / 60) % 60,
276 secs % 60,
277 (int)(ti % 1000));
278 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
279 d->linestart = 0;
281 ret += d->drv->chr_write(d->drv, buf+i, 1);
282 if (buf[i] == '\n') {
283 d->linestart = 1;
287 return ret;
290 static const char * const mux_help[] = {
291 "% h print this help\n\r",
292 "% x exit emulator\n\r",
293 "% s save disk data back to file (if -snapshot)\n\r",
294 "% t toggle console timestamps\n\r"
295 "% b send break (magic sysrq)\n\r",
296 "% c switch between console and monitor\n\r",
297 "% % sends %\n\r",
298 NULL
301 int term_escape_char = 0x01; /* ctrl-a is used for escape */
302 static void mux_print_help(CharDriverState *chr)
304 int i, j;
305 char ebuf[15] = "Escape-Char";
306 char cbuf[50] = "\n\r";
308 if (term_escape_char > 0 && term_escape_char < 26) {
309 snprintf(cbuf, sizeof(cbuf), "\n\r");
310 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
311 } else {
312 snprintf(cbuf, sizeof(cbuf),
313 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
314 term_escape_char);
316 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
317 for (i = 0; mux_help[i] != NULL; i++) {
318 for (j=0; mux_help[i][j] != '\0'; j++) {
319 if (mux_help[i][j] == '%')
320 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
321 else
322 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
327 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
329 if (d->chr_event[mux_nr])
330 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
333 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
335 if (d->term_got_escape) {
336 d->term_got_escape = 0;
337 if (ch == term_escape_char)
338 goto send_char;
339 switch(ch) {
340 case '?':
341 case 'h':
342 mux_print_help(chr);
343 break;
344 case 'x':
346 const char *term = "QEMU: Terminated\n\r";
347 chr->chr_write(chr,(uint8_t *)term,strlen(term));
348 exit(0);
349 break;
351 case 's':
353 DriveInfo *dinfo;
354 TAILQ_FOREACH(dinfo, &drives, next) {
355 bdrv_commit(dinfo->bdrv);
358 break;
359 case 'b':
360 qemu_chr_event(chr, CHR_EVENT_BREAK);
361 break;
362 case 'c':
363 /* Switch to the next registered device */
364 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_OUT);
365 chr->focus++;
366 if (chr->focus >= d->mux_cnt)
367 chr->focus = 0;
368 mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_IN);
369 break;
370 case 't':
371 d->timestamps = !d->timestamps;
372 d->timestamps_start = -1;
373 d->linestart = 0;
374 break;
376 } else if (ch == term_escape_char) {
377 d->term_got_escape = 1;
378 } else {
379 send_char:
380 return 1;
382 return 0;
385 static void mux_chr_accept_input(CharDriverState *chr)
387 int m = chr->focus;
388 MuxDriver *d = chr->opaque;
390 while (d->prod[m] != d->cons[m] &&
391 d->chr_can_read[m] &&
392 d->chr_can_read[m](d->ext_opaque[m])) {
393 d->chr_read[m](d->ext_opaque[m],
394 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
398 static int mux_chr_can_read(void *opaque)
400 CharDriverState *chr = opaque;
401 MuxDriver *d = chr->opaque;
402 int m = chr->focus;
404 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
405 return 1;
406 if (d->chr_can_read[m])
407 return d->chr_can_read[m](d->ext_opaque[m]);
408 return 0;
411 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
413 CharDriverState *chr = opaque;
414 MuxDriver *d = chr->opaque;
415 int m = chr->focus;
416 int i;
418 mux_chr_accept_input (opaque);
420 for(i = 0; i < size; i++)
421 if (mux_proc_byte(chr, d, buf[i])) {
422 if (d->prod[m] == d->cons[m] &&
423 d->chr_can_read[m] &&
424 d->chr_can_read[m](d->ext_opaque[m]))
425 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
426 else
427 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
431 static void mux_chr_event(void *opaque, int event)
433 CharDriverState *chr = opaque;
434 MuxDriver *d = chr->opaque;
435 int i;
437 /* Send the event to all registered listeners */
438 for (i = 0; i < d->mux_cnt; i++)
439 mux_chr_send_event(d, i, event);
442 static void mux_chr_update_read_handler(CharDriverState *chr)
444 MuxDriver *d = chr->opaque;
446 if (d->mux_cnt >= MAX_MUX) {
447 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
448 return;
450 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
451 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
452 d->chr_read[d->mux_cnt] = chr->chr_read;
453 d->chr_event[d->mux_cnt] = chr->chr_event;
454 /* Fix up the real driver with mux routines */
455 if (d->mux_cnt == 0) {
456 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
457 mux_chr_event, chr);
459 chr->focus = d->mux_cnt;
460 d->mux_cnt++;
463 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
465 CharDriverState *chr;
466 MuxDriver *d;
468 chr = qemu_mallocz(sizeof(CharDriverState));
469 d = qemu_mallocz(sizeof(MuxDriver));
471 chr->opaque = d;
472 d->drv = drv;
473 chr->focus = -1;
474 chr->chr_write = mux_chr_write;
475 chr->chr_update_read_handler = mux_chr_update_read_handler;
476 chr->chr_accept_input = mux_chr_accept_input;
477 return chr;
481 #ifdef _WIN32
482 int send_all(int fd, const void *buf, int len1)
484 int ret, len;
486 len = len1;
487 while (len > 0) {
488 ret = send(fd, buf, len, 0);
489 if (ret < 0) {
490 errno = WSAGetLastError();
491 if (errno != WSAEWOULDBLOCK) {
492 return -1;
494 } else if (ret == 0) {
495 break;
496 } else {
497 buf += ret;
498 len -= ret;
501 return len1 - len;
504 #else
506 static int unix_write(int fd, const uint8_t *buf, int len1)
508 int ret, len;
510 len = len1;
511 while (len > 0) {
512 ret = write(fd, buf, len);
513 if (ret < 0) {
514 if (errno != EINTR && errno != EAGAIN)
515 return -1;
516 } else if (ret == 0) {
517 break;
518 } else {
519 buf += ret;
520 len -= ret;
523 return len1 - len;
526 int send_all(int fd, const void *buf, int len1)
528 return unix_write(fd, buf, len1);
530 #endif /* !_WIN32 */
532 #ifndef _WIN32
534 typedef struct {
535 int fd_in, fd_out;
536 int max_size;
537 } FDCharDriver;
539 #define STDIO_MAX_CLIENTS 1
540 static int stdio_nb_clients = 0;
542 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
544 FDCharDriver *s = chr->opaque;
545 return send_all(s->fd_out, buf, len);
548 static int fd_chr_read_poll(void *opaque)
550 CharDriverState *chr = opaque;
551 FDCharDriver *s = chr->opaque;
553 s->max_size = qemu_chr_can_read(chr);
554 return s->max_size;
557 static void fd_chr_read(void *opaque)
559 CharDriverState *chr = opaque;
560 FDCharDriver *s = chr->opaque;
561 int size, len;
562 uint8_t buf[1024];
564 len = sizeof(buf);
565 if (len > s->max_size)
566 len = s->max_size;
567 if (len == 0)
568 return;
569 size = read(s->fd_in, buf, len);
570 if (size == 0) {
571 /* FD has been closed. Remove it from the active list. */
572 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
573 qemu_chr_event(chr, CHR_EVENT_CLOSED);
574 return;
576 if (size > 0) {
577 qemu_chr_read(chr, buf, size);
581 static void fd_chr_update_read_handler(CharDriverState *chr)
583 FDCharDriver *s = chr->opaque;
585 if (s->fd_in >= 0) {
586 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
587 } else {
588 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
589 fd_chr_read, NULL, chr);
594 static void fd_chr_close(struct CharDriverState *chr)
596 FDCharDriver *s = chr->opaque;
598 if (s->fd_in >= 0) {
599 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
600 } else {
601 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
605 qemu_free(s);
606 qemu_chr_event(chr, CHR_EVENT_CLOSED);
609 /* open a character device to a unix fd */
610 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
612 CharDriverState *chr;
613 FDCharDriver *s;
615 chr = qemu_mallocz(sizeof(CharDriverState));
616 s = qemu_mallocz(sizeof(FDCharDriver));
617 s->fd_in = fd_in;
618 s->fd_out = fd_out;
619 chr->opaque = s;
620 chr->chr_write = fd_chr_write;
621 chr->chr_update_read_handler = fd_chr_update_read_handler;
622 chr->chr_close = fd_chr_close;
624 qemu_chr_reset(chr);
626 return chr;
629 static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
631 int fd_out;
633 TFR(fd_out = open(qemu_opt_get(opts, "path"),
634 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
635 if (fd_out < 0)
636 return NULL;
637 return qemu_chr_open_fd(-1, fd_out);
640 static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
642 int fd_in, fd_out;
643 char filename_in[256], filename_out[256];
644 const char *filename = qemu_opt_get(opts, "path");
646 if (filename == NULL) {
647 fprintf(stderr, "chardev: pipe: no filename given\n");
648 return NULL;
651 snprintf(filename_in, 256, "%s.in", filename);
652 snprintf(filename_out, 256, "%s.out", filename);
653 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
654 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
655 if (fd_in < 0 || fd_out < 0) {
656 if (fd_in >= 0)
657 close(fd_in);
658 if (fd_out >= 0)
659 close(fd_out);
660 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
661 if (fd_in < 0)
662 return NULL;
664 return qemu_chr_open_fd(fd_in, fd_out);
668 /* for STDIO, we handle the case where several clients use it
669 (nographic mode) */
671 #define TERM_FIFO_MAX_SIZE 1
673 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
674 static int term_fifo_size;
676 static int stdio_read_poll(void *opaque)
678 CharDriverState *chr = opaque;
680 /* try to flush the queue if needed */
681 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
682 qemu_chr_read(chr, term_fifo, 1);
683 term_fifo_size = 0;
685 /* see if we can absorb more chars */
686 if (term_fifo_size == 0)
687 return 1;
688 else
689 return 0;
692 static void stdio_read(void *opaque)
694 int size;
695 uint8_t buf[1];
696 CharDriverState *chr = opaque;
698 size = read(0, buf, 1);
699 if (size == 0) {
700 /* stdin has been closed. Remove it from the active list. */
701 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
702 qemu_chr_event(chr, CHR_EVENT_CLOSED);
703 return;
705 if (size > 0) {
706 if (qemu_chr_can_read(chr) > 0) {
707 qemu_chr_read(chr, buf, 1);
708 } else if (term_fifo_size == 0) {
709 term_fifo[term_fifo_size++] = buf[0];
714 /* init terminal so that we can grab keys */
715 static struct termios oldtty;
716 static int old_fd0_flags;
717 static int term_atexit_done;
719 static void term_exit(void)
721 tcsetattr (0, TCSANOW, &oldtty);
722 fcntl(0, F_SETFL, old_fd0_flags);
725 static void term_init(void)
727 struct termios tty;
729 tcgetattr (0, &tty);
730 oldtty = tty;
731 old_fd0_flags = fcntl(0, F_GETFL);
733 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
734 |INLCR|IGNCR|ICRNL|IXON);
735 tty.c_oflag |= OPOST;
736 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
737 /* if graphical mode, we allow Ctrl-C handling */
738 if (display_type == DT_NOGRAPHIC)
739 tty.c_lflag &= ~ISIG;
740 tty.c_cflag &= ~(CSIZE|PARENB);
741 tty.c_cflag |= CS8;
742 tty.c_cc[VMIN] = 1;
743 tty.c_cc[VTIME] = 0;
745 tcsetattr (0, TCSANOW, &tty);
747 if (!term_atexit_done++)
748 atexit(term_exit);
750 fcntl(0, F_SETFL, O_NONBLOCK);
753 static void qemu_chr_close_stdio(struct CharDriverState *chr)
755 term_exit();
756 stdio_nb_clients--;
757 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
758 fd_chr_close(chr);
761 static CharDriverState *qemu_chr_open_stdio(void)
763 CharDriverState *chr;
765 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
766 return NULL;
767 chr = qemu_chr_open_fd(0, 1);
768 chr->chr_close = qemu_chr_close_stdio;
769 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
770 stdio_nb_clients++;
771 term_init();
773 return chr;
776 #ifdef __sun__
777 /* Once Solaris has openpty(), this is going to be removed. */
778 static int openpty(int *amaster, int *aslave, char *name,
779 struct termios *termp, struct winsize *winp)
781 const char *slave;
782 int mfd = -1, sfd = -1;
784 *amaster = *aslave = -1;
786 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
787 if (mfd < 0)
788 goto err;
790 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
791 goto err;
793 if ((slave = ptsname(mfd)) == NULL)
794 goto err;
796 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
797 goto err;
799 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
800 (termp != NULL && tcgetattr(sfd, termp) < 0))
801 goto err;
803 if (amaster)
804 *amaster = mfd;
805 if (aslave)
806 *aslave = sfd;
807 if (winp)
808 ioctl(sfd, TIOCSWINSZ, winp);
810 return 0;
812 err:
813 if (sfd != -1)
814 close(sfd);
815 close(mfd);
816 return -1;
819 static void cfmakeraw (struct termios *termios_p)
821 termios_p->c_iflag &=
822 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
823 termios_p->c_oflag &= ~OPOST;
824 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
825 termios_p->c_cflag &= ~(CSIZE|PARENB);
826 termios_p->c_cflag |= CS8;
828 termios_p->c_cc[VMIN] = 0;
829 termios_p->c_cc[VTIME] = 0;
831 #endif
833 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
834 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
836 typedef struct {
837 int fd;
838 int connected;
839 int polling;
840 int read_bytes;
841 QEMUTimer *timer;
842 } PtyCharDriver;
844 static void pty_chr_update_read_handler(CharDriverState *chr);
845 static void pty_chr_state(CharDriverState *chr, int connected);
847 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
849 PtyCharDriver *s = chr->opaque;
851 if (!s->connected) {
852 /* guest sends data, check for (re-)connect */
853 pty_chr_update_read_handler(chr);
854 return 0;
856 return send_all(s->fd, buf, len);
859 static int pty_chr_read_poll(void *opaque)
861 CharDriverState *chr = opaque;
862 PtyCharDriver *s = chr->opaque;
864 s->read_bytes = qemu_chr_can_read(chr);
865 return s->read_bytes;
868 static void pty_chr_read(void *opaque)
870 CharDriverState *chr = opaque;
871 PtyCharDriver *s = chr->opaque;
872 int size, len;
873 uint8_t buf[1024];
875 len = sizeof(buf);
876 if (len > s->read_bytes)
877 len = s->read_bytes;
878 if (len == 0)
879 return;
880 size = read(s->fd, buf, len);
881 if ((size == -1 && errno == EIO) ||
882 (size == 0)) {
883 pty_chr_state(chr, 0);
884 return;
886 if (size > 0) {
887 pty_chr_state(chr, 1);
888 qemu_chr_read(chr, buf, size);
892 static void pty_chr_update_read_handler(CharDriverState *chr)
894 PtyCharDriver *s = chr->opaque;
896 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
897 pty_chr_read, NULL, chr);
898 s->polling = 1;
900 * Short timeout here: just need wait long enougth that qemu makes
901 * it through the poll loop once. When reconnected we want a
902 * short timeout so we notice it almost instantly. Otherwise
903 * read() gives us -EIO instantly, making pty_chr_state() reset the
904 * timeout to the normal (much longer) poll interval before the
905 * timer triggers.
907 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
910 static void pty_chr_state(CharDriverState *chr, int connected)
912 PtyCharDriver *s = chr->opaque;
914 if (!connected) {
915 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
916 s->connected = 0;
917 s->polling = 0;
918 /* (re-)connect poll interval for idle guests: once per second.
919 * We check more frequently in case the guests sends data to
920 * the virtual device linked to our pty. */
921 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
922 } else {
923 if (!s->connected)
924 qemu_chr_reset(chr);
925 s->connected = 1;
929 static void pty_chr_timer(void *opaque)
931 struct CharDriverState *chr = opaque;
932 PtyCharDriver *s = chr->opaque;
934 if (s->connected)
935 return;
936 if (s->polling) {
937 /* If we arrive here without polling being cleared due
938 * read returning -EIO, then we are (re-)connected */
939 pty_chr_state(chr, 1);
940 return;
943 /* Next poll ... */
944 pty_chr_update_read_handler(chr);
947 static void pty_chr_close(struct CharDriverState *chr)
949 PtyCharDriver *s = chr->opaque;
951 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
952 close(s->fd);
953 qemu_del_timer(s->timer);
954 qemu_free_timer(s->timer);
955 qemu_free(s);
956 qemu_chr_event(chr, CHR_EVENT_CLOSED);
959 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
961 CharDriverState *chr;
962 PtyCharDriver *s;
963 struct termios tty;
964 int slave_fd, len;
965 #if defined(__OpenBSD__) || defined(__DragonFly__)
966 char pty_name[PATH_MAX];
967 #define q_ptsname(x) pty_name
968 #else
969 char *pty_name = NULL;
970 #define q_ptsname(x) ptsname(x)
971 #endif
973 chr = qemu_mallocz(sizeof(CharDriverState));
974 s = qemu_mallocz(sizeof(PtyCharDriver));
976 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
977 return NULL;
980 /* Set raw attributes on the pty. */
981 tcgetattr(slave_fd, &tty);
982 cfmakeraw(&tty);
983 tcsetattr(slave_fd, TCSAFLUSH, &tty);
984 close(slave_fd);
986 len = strlen(q_ptsname(s->fd)) + 5;
987 chr->filename = qemu_malloc(len);
988 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
989 qemu_opt_set(opts, "path", q_ptsname(s->fd));
990 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
992 chr->opaque = s;
993 chr->chr_write = pty_chr_write;
994 chr->chr_update_read_handler = pty_chr_update_read_handler;
995 chr->chr_close = pty_chr_close;
997 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
999 return chr;
1002 static void tty_serial_init(int fd, int speed,
1003 int parity, int data_bits, int stop_bits)
1005 struct termios tty;
1006 speed_t spd;
1008 #if 0
1009 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1010 speed, parity, data_bits, stop_bits);
1011 #endif
1012 tcgetattr (fd, &tty);
1014 #define MARGIN 1.1
1015 if (speed <= 50 * MARGIN)
1016 spd = B50;
1017 else if (speed <= 75 * MARGIN)
1018 spd = B75;
1019 else if (speed <= 300 * MARGIN)
1020 spd = B300;
1021 else if (speed <= 600 * MARGIN)
1022 spd = B600;
1023 else if (speed <= 1200 * MARGIN)
1024 spd = B1200;
1025 else if (speed <= 2400 * MARGIN)
1026 spd = B2400;
1027 else if (speed <= 4800 * MARGIN)
1028 spd = B4800;
1029 else if (speed <= 9600 * MARGIN)
1030 spd = B9600;
1031 else if (speed <= 19200 * MARGIN)
1032 spd = B19200;
1033 else if (speed <= 38400 * MARGIN)
1034 spd = B38400;
1035 else if (speed <= 57600 * MARGIN)
1036 spd = B57600;
1037 else if (speed <= 115200 * MARGIN)
1038 spd = B115200;
1039 else
1040 spd = B115200;
1042 cfsetispeed(&tty, spd);
1043 cfsetospeed(&tty, spd);
1045 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1046 |INLCR|IGNCR|ICRNL|IXON);
1047 tty.c_oflag |= OPOST;
1048 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1049 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1050 switch(data_bits) {
1051 default:
1052 case 8:
1053 tty.c_cflag |= CS8;
1054 break;
1055 case 7:
1056 tty.c_cflag |= CS7;
1057 break;
1058 case 6:
1059 tty.c_cflag |= CS6;
1060 break;
1061 case 5:
1062 tty.c_cflag |= CS5;
1063 break;
1065 switch(parity) {
1066 default:
1067 case 'N':
1068 break;
1069 case 'E':
1070 tty.c_cflag |= PARENB;
1071 break;
1072 case 'O':
1073 tty.c_cflag |= PARENB | PARODD;
1074 break;
1076 if (stop_bits == 2)
1077 tty.c_cflag |= CSTOPB;
1079 tcsetattr (fd, TCSANOW, &tty);
1082 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1084 FDCharDriver *s = chr->opaque;
1086 switch(cmd) {
1087 case CHR_IOCTL_SERIAL_SET_PARAMS:
1089 QEMUSerialSetParams *ssp = arg;
1090 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1091 ssp->data_bits, ssp->stop_bits);
1093 break;
1094 case CHR_IOCTL_SERIAL_SET_BREAK:
1096 int enable = *(int *)arg;
1097 if (enable)
1098 tcsendbreak(s->fd_in, 1);
1100 break;
1101 case CHR_IOCTL_SERIAL_GET_TIOCM:
1103 int sarg = 0;
1104 int *targ = (int *)arg;
1105 ioctl(s->fd_in, TIOCMGET, &sarg);
1106 *targ = 0;
1107 if (sarg & TIOCM_CTS)
1108 *targ |= CHR_TIOCM_CTS;
1109 if (sarg & TIOCM_CAR)
1110 *targ |= CHR_TIOCM_CAR;
1111 if (sarg & TIOCM_DSR)
1112 *targ |= CHR_TIOCM_DSR;
1113 if (sarg & TIOCM_RI)
1114 *targ |= CHR_TIOCM_RI;
1115 if (sarg & TIOCM_DTR)
1116 *targ |= CHR_TIOCM_DTR;
1117 if (sarg & TIOCM_RTS)
1118 *targ |= CHR_TIOCM_RTS;
1120 break;
1121 case CHR_IOCTL_SERIAL_SET_TIOCM:
1123 int sarg = *(int *)arg;
1124 int targ = 0;
1125 ioctl(s->fd_in, TIOCMGET, &targ);
1126 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1127 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1128 if (sarg & CHR_TIOCM_CTS)
1129 targ |= TIOCM_CTS;
1130 if (sarg & CHR_TIOCM_CAR)
1131 targ |= TIOCM_CAR;
1132 if (sarg & CHR_TIOCM_DSR)
1133 targ |= TIOCM_DSR;
1134 if (sarg & CHR_TIOCM_RI)
1135 targ |= TIOCM_RI;
1136 if (sarg & CHR_TIOCM_DTR)
1137 targ |= TIOCM_DTR;
1138 if (sarg & CHR_TIOCM_RTS)
1139 targ |= TIOCM_RTS;
1140 ioctl(s->fd_in, TIOCMSET, &targ);
1142 break;
1143 default:
1144 return -ENOTSUP;
1146 return 0;
1149 static CharDriverState *qemu_chr_open_tty(const char *filename)
1151 CharDriverState *chr;
1152 int fd;
1154 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1155 tty_serial_init(fd, 115200, 'N', 8, 1);
1156 chr = qemu_chr_open_fd(fd, fd);
1157 if (!chr) {
1158 close(fd);
1159 return NULL;
1161 chr->chr_ioctl = tty_serial_ioctl;
1162 qemu_chr_reset(chr);
1163 return chr;
1165 #else /* ! __linux__ && ! __sun__ */
1166 static CharDriverState *qemu_chr_open_pty(void)
1168 return NULL;
1170 #endif /* __linux__ || __sun__ */
1172 #if defined(__linux__)
1173 typedef struct {
1174 int fd;
1175 int mode;
1176 } ParallelCharDriver;
1178 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1180 if (s->mode != mode) {
1181 int m = mode;
1182 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1183 return 0;
1184 s->mode = mode;
1186 return 1;
1189 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1191 ParallelCharDriver *drv = chr->opaque;
1192 int fd = drv->fd;
1193 uint8_t b;
1195 switch(cmd) {
1196 case CHR_IOCTL_PP_READ_DATA:
1197 if (ioctl(fd, PPRDATA, &b) < 0)
1198 return -ENOTSUP;
1199 *(uint8_t *)arg = b;
1200 break;
1201 case CHR_IOCTL_PP_WRITE_DATA:
1202 b = *(uint8_t *)arg;
1203 if (ioctl(fd, PPWDATA, &b) < 0)
1204 return -ENOTSUP;
1205 break;
1206 case CHR_IOCTL_PP_READ_CONTROL:
1207 if (ioctl(fd, PPRCONTROL, &b) < 0)
1208 return -ENOTSUP;
1209 /* Linux gives only the lowest bits, and no way to know data
1210 direction! For better compatibility set the fixed upper
1211 bits. */
1212 *(uint8_t *)arg = b | 0xc0;
1213 break;
1214 case CHR_IOCTL_PP_WRITE_CONTROL:
1215 b = *(uint8_t *)arg;
1216 if (ioctl(fd, PPWCONTROL, &b) < 0)
1217 return -ENOTSUP;
1218 break;
1219 case CHR_IOCTL_PP_READ_STATUS:
1220 if (ioctl(fd, PPRSTATUS, &b) < 0)
1221 return -ENOTSUP;
1222 *(uint8_t *)arg = b;
1223 break;
1224 case CHR_IOCTL_PP_DATA_DIR:
1225 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1226 return -ENOTSUP;
1227 break;
1228 case CHR_IOCTL_PP_EPP_READ_ADDR:
1229 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1230 struct ParallelIOArg *parg = arg;
1231 int n = read(fd, parg->buffer, parg->count);
1232 if (n != parg->count) {
1233 return -EIO;
1236 break;
1237 case CHR_IOCTL_PP_EPP_READ:
1238 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1239 struct ParallelIOArg *parg = arg;
1240 int n = read(fd, parg->buffer, parg->count);
1241 if (n != parg->count) {
1242 return -EIO;
1245 break;
1246 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1247 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1248 struct ParallelIOArg *parg = arg;
1249 int n = write(fd, parg->buffer, parg->count);
1250 if (n != parg->count) {
1251 return -EIO;
1254 break;
1255 case CHR_IOCTL_PP_EPP_WRITE:
1256 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1257 struct ParallelIOArg *parg = arg;
1258 int n = write(fd, parg->buffer, parg->count);
1259 if (n != parg->count) {
1260 return -EIO;
1263 break;
1264 default:
1265 return -ENOTSUP;
1267 return 0;
1270 static void pp_close(CharDriverState *chr)
1272 ParallelCharDriver *drv = chr->opaque;
1273 int fd = drv->fd;
1275 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1276 ioctl(fd, PPRELEASE);
1277 close(fd);
1278 qemu_free(drv);
1279 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1282 static CharDriverState *qemu_chr_open_pp(const char *filename)
1284 CharDriverState *chr;
1285 ParallelCharDriver *drv;
1286 int fd;
1288 TFR(fd = open(filename, O_RDWR));
1289 if (fd < 0)
1290 return NULL;
1292 if (ioctl(fd, PPCLAIM) < 0) {
1293 close(fd);
1294 return NULL;
1297 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1298 drv->fd = fd;
1299 drv->mode = IEEE1284_MODE_COMPAT;
1301 chr = qemu_mallocz(sizeof(CharDriverState));
1302 chr->chr_write = null_chr_write;
1303 chr->chr_ioctl = pp_ioctl;
1304 chr->chr_close = pp_close;
1305 chr->opaque = drv;
1307 qemu_chr_reset(chr);
1309 return chr;
1311 #endif /* __linux__ */
1313 #if defined(__FreeBSD__) || defined(__DragonFly__)
1314 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1316 int fd = (int)chr->opaque;
1317 uint8_t b;
1319 switch(cmd) {
1320 case CHR_IOCTL_PP_READ_DATA:
1321 if (ioctl(fd, PPIGDATA, &b) < 0)
1322 return -ENOTSUP;
1323 *(uint8_t *)arg = b;
1324 break;
1325 case CHR_IOCTL_PP_WRITE_DATA:
1326 b = *(uint8_t *)arg;
1327 if (ioctl(fd, PPISDATA, &b) < 0)
1328 return -ENOTSUP;
1329 break;
1330 case CHR_IOCTL_PP_READ_CONTROL:
1331 if (ioctl(fd, PPIGCTRL, &b) < 0)
1332 return -ENOTSUP;
1333 *(uint8_t *)arg = b;
1334 break;
1335 case CHR_IOCTL_PP_WRITE_CONTROL:
1336 b = *(uint8_t *)arg;
1337 if (ioctl(fd, PPISCTRL, &b) < 0)
1338 return -ENOTSUP;
1339 break;
1340 case CHR_IOCTL_PP_READ_STATUS:
1341 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1342 return -ENOTSUP;
1343 *(uint8_t *)arg = b;
1344 break;
1345 default:
1346 return -ENOTSUP;
1348 return 0;
1351 static CharDriverState *qemu_chr_open_pp(const char *filename)
1353 CharDriverState *chr;
1354 int fd;
1356 fd = open(filename, O_RDWR);
1357 if (fd < 0)
1358 return NULL;
1360 chr = qemu_mallocz(sizeof(CharDriverState));
1361 chr->opaque = (void *)fd;
1362 chr->chr_write = null_chr_write;
1363 chr->chr_ioctl = pp_ioctl;
1364 return chr;
1366 #endif
1368 #else /* _WIN32 */
1370 typedef struct {
1371 int max_size;
1372 HANDLE hcom, hrecv, hsend;
1373 OVERLAPPED orecv, osend;
1374 BOOL fpipe;
1375 DWORD len;
1376 } WinCharState;
1378 #define NSENDBUF 2048
1379 #define NRECVBUF 2048
1380 #define MAXCONNECT 1
1381 #define NTIMEOUT 5000
1383 static int win_chr_poll(void *opaque);
1384 static int win_chr_pipe_poll(void *opaque);
1386 static void win_chr_close(CharDriverState *chr)
1388 WinCharState *s = chr->opaque;
1390 if (s->hsend) {
1391 CloseHandle(s->hsend);
1392 s->hsend = NULL;
1394 if (s->hrecv) {
1395 CloseHandle(s->hrecv);
1396 s->hrecv = NULL;
1398 if (s->hcom) {
1399 CloseHandle(s->hcom);
1400 s->hcom = NULL;
1402 if (s->fpipe)
1403 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1404 else
1405 qemu_del_polling_cb(win_chr_poll, chr);
1407 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1410 static int win_chr_init(CharDriverState *chr, const char *filename)
1412 WinCharState *s = chr->opaque;
1413 COMMCONFIG comcfg;
1414 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1415 COMSTAT comstat;
1416 DWORD size;
1417 DWORD err;
1419 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1420 if (!s->hsend) {
1421 fprintf(stderr, "Failed CreateEvent\n");
1422 goto fail;
1424 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1425 if (!s->hrecv) {
1426 fprintf(stderr, "Failed CreateEvent\n");
1427 goto fail;
1430 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1431 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1432 if (s->hcom == INVALID_HANDLE_VALUE) {
1433 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1434 s->hcom = NULL;
1435 goto fail;
1438 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1439 fprintf(stderr, "Failed SetupComm\n");
1440 goto fail;
1443 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1444 size = sizeof(COMMCONFIG);
1445 GetDefaultCommConfig(filename, &comcfg, &size);
1446 comcfg.dcb.DCBlength = sizeof(DCB);
1447 CommConfigDialog(filename, NULL, &comcfg);
1449 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1450 fprintf(stderr, "Failed SetCommState\n");
1451 goto fail;
1454 if (!SetCommMask(s->hcom, EV_ERR)) {
1455 fprintf(stderr, "Failed SetCommMask\n");
1456 goto fail;
1459 cto.ReadIntervalTimeout = MAXDWORD;
1460 if (!SetCommTimeouts(s->hcom, &cto)) {
1461 fprintf(stderr, "Failed SetCommTimeouts\n");
1462 goto fail;
1465 if (!ClearCommError(s->hcom, &err, &comstat)) {
1466 fprintf(stderr, "Failed ClearCommError\n");
1467 goto fail;
1469 qemu_add_polling_cb(win_chr_poll, chr);
1470 return 0;
1472 fail:
1473 win_chr_close(chr);
1474 return -1;
1477 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1479 WinCharState *s = chr->opaque;
1480 DWORD len, ret, size, err;
1482 len = len1;
1483 ZeroMemory(&s->osend, sizeof(s->osend));
1484 s->osend.hEvent = s->hsend;
1485 while (len > 0) {
1486 if (s->hsend)
1487 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1488 else
1489 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1490 if (!ret) {
1491 err = GetLastError();
1492 if (err == ERROR_IO_PENDING) {
1493 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1494 if (ret) {
1495 buf += size;
1496 len -= size;
1497 } else {
1498 break;
1500 } else {
1501 break;
1503 } else {
1504 buf += size;
1505 len -= size;
1508 return len1 - len;
1511 static int win_chr_read_poll(CharDriverState *chr)
1513 WinCharState *s = chr->opaque;
1515 s->max_size = qemu_chr_can_read(chr);
1516 return s->max_size;
1519 static void win_chr_readfile(CharDriverState *chr)
1521 WinCharState *s = chr->opaque;
1522 int ret, err;
1523 uint8_t buf[1024];
1524 DWORD size;
1526 ZeroMemory(&s->orecv, sizeof(s->orecv));
1527 s->orecv.hEvent = s->hrecv;
1528 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1529 if (!ret) {
1530 err = GetLastError();
1531 if (err == ERROR_IO_PENDING) {
1532 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1536 if (size > 0) {
1537 qemu_chr_read(chr, buf, size);
1541 static void win_chr_read(CharDriverState *chr)
1543 WinCharState *s = chr->opaque;
1545 if (s->len > s->max_size)
1546 s->len = s->max_size;
1547 if (s->len == 0)
1548 return;
1550 win_chr_readfile(chr);
1553 static int win_chr_poll(void *opaque)
1555 CharDriverState *chr = opaque;
1556 WinCharState *s = chr->opaque;
1557 COMSTAT status;
1558 DWORD comerr;
1560 ClearCommError(s->hcom, &comerr, &status);
1561 if (status.cbInQue > 0) {
1562 s->len = status.cbInQue;
1563 win_chr_read_poll(chr);
1564 win_chr_read(chr);
1565 return 1;
1567 return 0;
1570 static CharDriverState *qemu_chr_open_win(const char *filename)
1572 CharDriverState *chr;
1573 WinCharState *s;
1575 chr = qemu_mallocz(sizeof(CharDriverState));
1576 s = qemu_mallocz(sizeof(WinCharState));
1577 chr->opaque = s;
1578 chr->chr_write = win_chr_write;
1579 chr->chr_close = win_chr_close;
1581 if (win_chr_init(chr, filename) < 0) {
1582 free(s);
1583 free(chr);
1584 return NULL;
1586 qemu_chr_reset(chr);
1587 return chr;
1590 static int win_chr_pipe_poll(void *opaque)
1592 CharDriverState *chr = opaque;
1593 WinCharState *s = chr->opaque;
1594 DWORD size;
1596 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1597 if (size > 0) {
1598 s->len = size;
1599 win_chr_read_poll(chr);
1600 win_chr_read(chr);
1601 return 1;
1603 return 0;
1606 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1608 WinCharState *s = chr->opaque;
1609 OVERLAPPED ov;
1610 int ret;
1611 DWORD size;
1612 char openname[256];
1614 s->fpipe = TRUE;
1616 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1617 if (!s->hsend) {
1618 fprintf(stderr, "Failed CreateEvent\n");
1619 goto fail;
1621 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1622 if (!s->hrecv) {
1623 fprintf(stderr, "Failed CreateEvent\n");
1624 goto fail;
1627 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1628 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1629 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1630 PIPE_WAIT,
1631 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1632 if (s->hcom == INVALID_HANDLE_VALUE) {
1633 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1634 s->hcom = NULL;
1635 goto fail;
1638 ZeroMemory(&ov, sizeof(ov));
1639 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1640 ret = ConnectNamedPipe(s->hcom, &ov);
1641 if (ret) {
1642 fprintf(stderr, "Failed ConnectNamedPipe\n");
1643 goto fail;
1646 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1647 if (!ret) {
1648 fprintf(stderr, "Failed GetOverlappedResult\n");
1649 if (ov.hEvent) {
1650 CloseHandle(ov.hEvent);
1651 ov.hEvent = NULL;
1653 goto fail;
1656 if (ov.hEvent) {
1657 CloseHandle(ov.hEvent);
1658 ov.hEvent = NULL;
1660 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1661 return 0;
1663 fail:
1664 win_chr_close(chr);
1665 return -1;
1669 static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
1671 const char *filename = qemu_opt_get(opts, "path");
1672 CharDriverState *chr;
1673 WinCharState *s;
1675 chr = qemu_mallocz(sizeof(CharDriverState));
1676 s = qemu_mallocz(sizeof(WinCharState));
1677 chr->opaque = s;
1678 chr->chr_write = win_chr_write;
1679 chr->chr_close = win_chr_close;
1681 if (win_chr_pipe_init(chr, filename) < 0) {
1682 free(s);
1683 free(chr);
1684 return NULL;
1686 qemu_chr_reset(chr);
1687 return chr;
1690 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1692 CharDriverState *chr;
1693 WinCharState *s;
1695 chr = qemu_mallocz(sizeof(CharDriverState));
1696 s = qemu_mallocz(sizeof(WinCharState));
1697 s->hcom = fd_out;
1698 chr->opaque = s;
1699 chr->chr_write = win_chr_write;
1700 qemu_chr_reset(chr);
1701 return chr;
1704 static CharDriverState *qemu_chr_open_win_con(const char *filename)
1706 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1709 static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
1711 const char *file_out = qemu_opt_get(opts, "path");
1712 HANDLE fd_out;
1714 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1715 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1716 if (fd_out == INVALID_HANDLE_VALUE)
1717 return NULL;
1719 return qemu_chr_open_win_file(fd_out);
1721 #endif /* !_WIN32 */
1723 /***********************************************************/
1724 /* UDP Net console */
1726 typedef struct {
1727 int fd;
1728 struct sockaddr_in daddr;
1729 uint8_t buf[1024];
1730 int bufcnt;
1731 int bufptr;
1732 int max_size;
1733 } NetCharDriver;
1735 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1737 NetCharDriver *s = chr->opaque;
1739 return sendto(s->fd, (const void *)buf, len, 0,
1740 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
1743 static int udp_chr_read_poll(void *opaque)
1745 CharDriverState *chr = opaque;
1746 NetCharDriver *s = chr->opaque;
1748 s->max_size = qemu_chr_can_read(chr);
1750 /* If there were any stray characters in the queue process them
1751 * first
1753 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1754 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1755 s->bufptr++;
1756 s->max_size = qemu_chr_can_read(chr);
1758 return s->max_size;
1761 static void udp_chr_read(void *opaque)
1763 CharDriverState *chr = opaque;
1764 NetCharDriver *s = chr->opaque;
1766 if (s->max_size == 0)
1767 return;
1768 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1769 s->bufptr = s->bufcnt;
1770 if (s->bufcnt <= 0)
1771 return;
1773 s->bufptr = 0;
1774 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1775 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1776 s->bufptr++;
1777 s->max_size = qemu_chr_can_read(chr);
1781 static void udp_chr_update_read_handler(CharDriverState *chr)
1783 NetCharDriver *s = chr->opaque;
1785 if (s->fd >= 0) {
1786 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1787 udp_chr_read, NULL, chr);
1791 static void udp_chr_close(CharDriverState *chr)
1793 NetCharDriver *s = chr->opaque;
1794 if (s->fd >= 0) {
1795 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1796 closesocket(s->fd);
1798 qemu_free(s);
1799 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1802 static CharDriverState *qemu_chr_open_udp(const char *def)
1804 CharDriverState *chr = NULL;
1805 NetCharDriver *s = NULL;
1806 int fd = -1;
1807 struct sockaddr_in saddr;
1809 chr = qemu_mallocz(sizeof(CharDriverState));
1810 s = qemu_mallocz(sizeof(NetCharDriver));
1812 fd = socket(PF_INET, SOCK_DGRAM, 0);
1813 if (fd < 0) {
1814 perror("socket(PF_INET, SOCK_DGRAM)");
1815 goto return_err;
1818 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
1819 printf("Could not parse: %s\n", def);
1820 goto return_err;
1823 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
1825 perror("bind");
1826 goto return_err;
1829 s->fd = fd;
1830 s->bufcnt = 0;
1831 s->bufptr = 0;
1832 chr->opaque = s;
1833 chr->chr_write = udp_chr_write;
1834 chr->chr_update_read_handler = udp_chr_update_read_handler;
1835 chr->chr_close = udp_chr_close;
1836 return chr;
1838 return_err:
1839 if (chr)
1840 free(chr);
1841 if (s)
1842 free(s);
1843 if (fd >= 0)
1844 closesocket(fd);
1845 return NULL;
1848 /***********************************************************/
1849 /* TCP Net console */
1851 typedef struct {
1852 int fd, listen_fd;
1853 int connected;
1854 int max_size;
1855 int do_telnetopt;
1856 int do_nodelay;
1857 int is_unix;
1858 int msgfd;
1859 } TCPCharDriver;
1861 static void tcp_chr_accept(void *opaque);
1863 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1865 TCPCharDriver *s = chr->opaque;
1866 if (s->connected) {
1867 return send_all(s->fd, buf, len);
1868 } else {
1869 /* XXX: indicate an error ? */
1870 return len;
1874 static int tcp_chr_read_poll(void *opaque)
1876 CharDriverState *chr = opaque;
1877 TCPCharDriver *s = chr->opaque;
1878 if (!s->connected)
1879 return 0;
1880 s->max_size = qemu_chr_can_read(chr);
1881 return s->max_size;
1884 #define IAC 255
1885 #define IAC_BREAK 243
1886 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1887 TCPCharDriver *s,
1888 uint8_t *buf, int *size)
1890 /* Handle any telnet client's basic IAC options to satisfy char by
1891 * char mode with no echo. All IAC options will be removed from
1892 * the buf and the do_telnetopt variable will be used to track the
1893 * state of the width of the IAC information.
1895 * IAC commands come in sets of 3 bytes with the exception of the
1896 * "IAC BREAK" command and the double IAC.
1899 int i;
1900 int j = 0;
1902 for (i = 0; i < *size; i++) {
1903 if (s->do_telnetopt > 1) {
1904 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1905 /* Double IAC means send an IAC */
1906 if (j != i)
1907 buf[j] = buf[i];
1908 j++;
1909 s->do_telnetopt = 1;
1910 } else {
1911 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1912 /* Handle IAC break commands by sending a serial break */
1913 qemu_chr_event(chr, CHR_EVENT_BREAK);
1914 s->do_telnetopt++;
1916 s->do_telnetopt++;
1918 if (s->do_telnetopt >= 4) {
1919 s->do_telnetopt = 1;
1921 } else {
1922 if ((unsigned char)buf[i] == IAC) {
1923 s->do_telnetopt = 2;
1924 } else {
1925 if (j != i)
1926 buf[j] = buf[i];
1927 j++;
1931 *size = j;
1934 static int tcp_get_msgfd(CharDriverState *chr)
1936 TCPCharDriver *s = chr->opaque;
1938 return s->msgfd;
1941 #ifndef _WIN32
1942 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1944 TCPCharDriver *s = chr->opaque;
1945 struct cmsghdr *cmsg;
1947 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1948 int fd;
1950 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1951 cmsg->cmsg_level != SOL_SOCKET ||
1952 cmsg->cmsg_type != SCM_RIGHTS)
1953 continue;
1955 fd = *((int *)CMSG_DATA(cmsg));
1956 if (fd < 0)
1957 continue;
1959 if (s->msgfd != -1)
1960 close(s->msgfd);
1961 s->msgfd = fd;
1965 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1967 TCPCharDriver *s = chr->opaque;
1968 struct msghdr msg = { NULL, };
1969 struct iovec iov[1];
1970 union {
1971 struct cmsghdr cmsg;
1972 char control[CMSG_SPACE(sizeof(int))];
1973 } msg_control;
1974 ssize_t ret;
1976 iov[0].iov_base = buf;
1977 iov[0].iov_len = len;
1979 msg.msg_iov = iov;
1980 msg.msg_iovlen = 1;
1981 msg.msg_control = &msg_control;
1982 msg.msg_controllen = sizeof(msg_control);
1984 ret = recvmsg(s->fd, &msg, 0);
1985 if (ret > 0 && s->is_unix)
1986 unix_process_msgfd(chr, &msg);
1988 return ret;
1990 #else
1991 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1993 TCPCharDriver *s = chr->opaque;
1994 return recv(s->fd, buf, len, 0);
1996 #endif
1998 static void tcp_chr_read(void *opaque)
2000 CharDriverState *chr = opaque;
2001 TCPCharDriver *s = chr->opaque;
2002 uint8_t buf[1024];
2003 int len, size;
2005 if (!s->connected || s->max_size <= 0)
2006 return;
2007 len = sizeof(buf);
2008 if (len > s->max_size)
2009 len = s->max_size;
2010 size = tcp_chr_recv(chr, (void *)buf, len);
2011 if (size == 0) {
2012 /* connection closed */
2013 s->connected = 0;
2014 if (s->listen_fd >= 0) {
2015 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2017 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2018 closesocket(s->fd);
2019 s->fd = -1;
2020 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2021 } else if (size > 0) {
2022 if (s->do_telnetopt)
2023 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2024 if (size > 0)
2025 qemu_chr_read(chr, buf, size);
2026 if (s->msgfd != -1) {
2027 close(s->msgfd);
2028 s->msgfd = -1;
2033 static void tcp_chr_connect(void *opaque)
2035 CharDriverState *chr = opaque;
2036 TCPCharDriver *s = chr->opaque;
2038 s->connected = 1;
2039 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2040 tcp_chr_read, NULL, chr);
2041 qemu_chr_reset(chr);
2044 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2045 static void tcp_chr_telnet_init(int fd)
2047 char buf[3];
2048 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2049 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2050 send(fd, (char *)buf, 3, 0);
2051 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2052 send(fd, (char *)buf, 3, 0);
2053 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2054 send(fd, (char *)buf, 3, 0);
2055 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2056 send(fd, (char *)buf, 3, 0);
2059 static void socket_set_nodelay(int fd)
2061 int val = 1;
2062 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2065 static void tcp_chr_accept(void *opaque)
2067 CharDriverState *chr = opaque;
2068 TCPCharDriver *s = chr->opaque;
2069 struct sockaddr_in saddr;
2070 #ifndef _WIN32
2071 struct sockaddr_un uaddr;
2072 #endif
2073 struct sockaddr *addr;
2074 socklen_t len;
2075 int fd;
2077 for(;;) {
2078 #ifndef _WIN32
2079 if (s->is_unix) {
2080 len = sizeof(uaddr);
2081 addr = (struct sockaddr *)&uaddr;
2082 } else
2083 #endif
2085 len = sizeof(saddr);
2086 addr = (struct sockaddr *)&saddr;
2088 fd = accept(s->listen_fd, addr, &len);
2089 if (fd < 0 && errno != EINTR) {
2090 return;
2091 } else if (fd >= 0) {
2092 if (s->do_telnetopt)
2093 tcp_chr_telnet_init(fd);
2094 break;
2097 socket_set_nonblock(fd);
2098 if (s->do_nodelay)
2099 socket_set_nodelay(fd);
2100 s->fd = fd;
2101 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2102 tcp_chr_connect(chr);
2105 static void tcp_chr_close(CharDriverState *chr)
2107 TCPCharDriver *s = chr->opaque;
2108 if (s->fd >= 0) {
2109 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2110 closesocket(s->fd);
2112 if (s->listen_fd >= 0) {
2113 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2114 closesocket(s->listen_fd);
2116 qemu_free(s);
2117 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2120 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2122 CharDriverState *chr = NULL;
2123 TCPCharDriver *s = NULL;
2124 int fd = -1;
2125 int is_listen;
2126 int is_waitconnect;
2127 int do_nodelay;
2128 int is_unix;
2129 int is_telnet;
2131 is_listen = qemu_opt_get_bool(opts, "server", 0);
2132 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2133 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2134 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2135 is_unix = qemu_opt_get(opts, "path") != NULL;
2136 if (!is_listen)
2137 is_waitconnect = 0;
2139 chr = qemu_mallocz(sizeof(CharDriverState));
2140 s = qemu_mallocz(sizeof(TCPCharDriver));
2142 if (is_unix) {
2143 if (is_listen) {
2144 fd = unix_listen_opts(opts);
2145 } else {
2146 fd = unix_connect_opts(opts);
2148 } else {
2149 if (is_listen) {
2150 fd = inet_listen_opts(opts, 0);
2151 } else {
2152 fd = inet_connect_opts(opts);
2155 if (fd < 0)
2156 goto fail;
2158 if (!is_waitconnect)
2159 socket_set_nonblock(fd);
2161 s->connected = 0;
2162 s->fd = -1;
2163 s->listen_fd = -1;
2164 s->msgfd = -1;
2165 s->is_unix = is_unix;
2166 s->do_nodelay = do_nodelay && !is_unix;
2168 chr->opaque = s;
2169 chr->chr_write = tcp_chr_write;
2170 chr->chr_close = tcp_chr_close;
2171 chr->get_msgfd = tcp_get_msgfd;
2173 if (is_listen) {
2174 s->listen_fd = fd;
2175 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2176 if (is_telnet)
2177 s->do_telnetopt = 1;
2179 } else {
2180 s->connected = 1;
2181 s->fd = fd;
2182 socket_set_nodelay(fd);
2183 tcp_chr_connect(chr);
2186 /* for "info chardev" monitor command */
2187 chr->filename = qemu_malloc(256);
2188 if (is_unix) {
2189 snprintf(chr->filename, 256, "unix:%s%s",
2190 qemu_opt_get(opts, "path"),
2191 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2192 } else if (is_telnet) {
2193 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2194 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2195 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2196 } else {
2197 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2198 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2199 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2202 if (is_listen && is_waitconnect) {
2203 printf("QEMU waiting for connection on: %s\n",
2204 chr->filename);
2205 tcp_chr_accept(chr);
2206 socket_set_nonblock(s->listen_fd);
2208 return chr;
2210 fail:
2211 if (fd >= 0)
2212 closesocket(fd);
2213 qemu_free(s);
2214 qemu_free(chr);
2215 return NULL;
2218 static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2220 char host[65], port[33];
2221 int pos;
2222 const char *p;
2223 QemuOpts *opts;
2225 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2226 if (NULL == opts)
2227 return NULL;
2229 if (strcmp(filename, "null") == 0 ||
2230 strcmp(filename, "pty") == 0) {
2231 qemu_opt_set(opts, "backend", filename);
2232 return opts;
2234 if (strstart(filename, "file:", &p)) {
2235 qemu_opt_set(opts, "backend", "file");
2236 qemu_opt_set(opts, "path", p);
2237 return opts;
2239 if (strstart(filename, "pipe:", &p)) {
2240 qemu_opt_set(opts, "backend", "pipe");
2241 qemu_opt_set(opts, "path", p);
2242 return opts;
2244 if (strstart(filename, "tcp:", &p) ||
2245 strstart(filename, "telnet:", &p)) {
2246 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2247 host[0] = 0;
2248 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2249 goto fail;
2251 qemu_opt_set(opts, "backend", "socket");
2252 qemu_opt_set(opts, "host", host);
2253 qemu_opt_set(opts, "port", port);
2254 if (p[pos] == ',') {
2255 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2256 goto fail;
2258 if (strstart(filename, "telnet:", &p))
2259 qemu_opt_set(opts, "telnet", "on");
2260 return opts;
2262 if (strstart(filename, "unix:", &p)) {
2263 qemu_opt_set(opts, "backend", "socket");
2264 if (qemu_opts_do_parse(opts, p, "path") != 0)
2265 goto fail;
2266 return opts;
2269 fail:
2270 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
2271 qemu_opts_del(opts);
2272 return NULL;
2275 static const struct {
2276 const char *name;
2277 CharDriverState *(*open)(QemuOpts *opts);
2278 } backend_table[] = {
2279 { .name = "null", .open = qemu_chr_open_null },
2280 { .name = "socket", .open = qemu_chr_open_socket },
2281 #ifdef _WIN32
2282 { .name = "file", .open = qemu_chr_open_win_file_out },
2283 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2284 #else
2285 { .name = "file", .open = qemu_chr_open_file_out },
2286 { .name = "pipe", .open = qemu_chr_open_pipe },
2287 { .name = "pty", .open = qemu_chr_open_pty },
2288 #endif
2291 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2292 void (*init)(struct CharDriverState *s))
2294 CharDriverState *chr;
2295 int i;
2297 if (qemu_opts_id(opts) == NULL) {
2298 fprintf(stderr, "chardev: no id specified\n");
2299 return NULL;
2302 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2303 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2304 break;
2306 if (i == ARRAY_SIZE(backend_table)) {
2307 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2308 qemu_opt_get(opts, "backend"));
2309 return NULL;
2312 chr = backend_table[i].open(opts);
2313 if (!chr) {
2314 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2315 qemu_opt_get(opts, "backend"));
2316 return NULL;
2319 if (!chr->filename)
2320 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2321 chr->init = init;
2322 chr->label = qemu_strdup(qemu_opts_id(opts));
2323 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2324 return chr;
2327 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2329 const char *p;
2330 CharDriverState *chr;
2331 QemuOpts *opts;
2333 opts = qemu_chr_parse_compat(label, filename);
2334 if (opts) {
2335 return qemu_chr_open_opts(opts, init);
2338 if (!strcmp(filename, "vc")) {
2339 chr = text_console_init(NULL);
2340 } else
2341 if (strstart(filename, "vc:", &p)) {
2342 chr = text_console_init(p);
2343 } else
2344 if (strstart(filename, "udp:", &p)) {
2345 chr = qemu_chr_open_udp(p);
2346 } else
2347 if (strstart(filename, "mon:", &p)) {
2348 chr = qemu_chr_open(label, p, NULL);
2349 if (chr) {
2350 chr = qemu_chr_open_mux(chr);
2351 monitor_init(chr, MONITOR_USE_READLINE);
2352 } else {
2353 printf("Unable to open driver: %s\n", p);
2355 } else if (!strcmp(filename, "msmouse")) {
2356 chr = qemu_chr_open_msmouse();
2357 } else
2358 #ifndef _WIN32
2359 if (!strcmp(filename, "stdio")) {
2360 chr = qemu_chr_open_stdio();
2361 } else
2362 #if defined(__linux__)
2363 if (strstart(filename, "/dev/parport", NULL)) {
2364 chr = qemu_chr_open_pp(filename);
2365 } else
2366 #elif defined(__FreeBSD__) || defined(__DragonFly__)
2367 if (strstart(filename, "/dev/ppi", NULL)) {
2368 chr = qemu_chr_open_pp(filename);
2369 } else
2370 #endif
2371 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2372 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2373 if (strstart(filename, "/dev/", NULL)) {
2374 chr = qemu_chr_open_tty(filename);
2375 } else
2376 #endif
2377 #else /* !_WIN32 */
2378 if (strstart(filename, "COM", NULL)) {
2379 chr = qemu_chr_open_win(filename);
2380 } else
2381 if (strstart(filename, "con:", NULL)) {
2382 chr = qemu_chr_open_win_con(filename);
2383 } else
2384 #endif
2385 #ifdef CONFIG_BRLAPI
2386 if (!strcmp(filename, "braille")) {
2387 chr = chr_baum_init();
2388 } else
2389 #endif
2391 chr = NULL;
2394 if (chr) {
2395 if (!chr->filename)
2396 chr->filename = qemu_strdup(filename);
2397 chr->init = init;
2398 chr->label = qemu_strdup(label);
2399 TAILQ_INSERT_TAIL(&chardevs, chr, next);
2401 return chr;
2404 void qemu_chr_close(CharDriverState *chr)
2406 TAILQ_REMOVE(&chardevs, chr, next);
2407 if (chr->chr_close)
2408 chr->chr_close(chr);
2409 qemu_free(chr->filename);
2410 qemu_free(chr->label);
2411 qemu_free(chr);
2414 void qemu_chr_info(Monitor *mon)
2416 CharDriverState *chr;
2418 TAILQ_FOREACH(chr, &chardevs, next) {
2419 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);