fix printf rename fallout in mips_malta.c
[qemu/aliguori.git] / qemu-char.c
blob6da548cbea3333e4181feca3d1318f4a6b3dbee7
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 "hw/usb.h"
32 #include "hw/baum.h"
33 #include "hw/msmouse.h"
34 #include "qemu-objects.h"
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <time.h>
39 #include <errno.h>
40 #include <sys/time.h>
41 #include <zlib.h>
43 #ifndef _WIN32
44 #include <sys/times.h>
45 #include <sys/wait.h>
46 #include <termios.h>
47 #include <sys/mman.h>
48 #include <sys/ioctl.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <net/if.h>
53 #include <arpa/inet.h>
54 #include <dirent.h>
55 #include <netdb.h>
56 #include <sys/select.h>
57 #ifdef CONFIG_BSD
58 #include <sys/stat.h>
59 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
60 #include <libutil.h>
61 #include <dev/ppbus/ppi.h>
62 #include <dev/ppbus/ppbconf.h>
63 #if defined(__GLIBC__)
64 #include <pty.h>
65 #endif
66 #elif defined(__DragonFly__)
67 #include <libutil.h>
68 #include <dev/misc/ppi/ppi.h>
69 #include <bus/ppbus/ppbconf.h>
70 #else
71 #include <util.h>
72 #endif
73 #else
74 #ifdef __linux__
75 #include <pty.h>
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
79 #endif
80 #ifdef __sun__
81 #include <sys/stat.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
91 #include <net/if.h>
92 #include <syslog.h>
93 #include <stropts.h>
94 #endif
95 #endif
96 #endif
98 #include "qemu_socket.h"
99 #include "ui/qemu-spice.h"
101 #define READ_BUF_LEN 4096
103 int term_escape_char = 0x01; /* ctrl-a is used for escape */
105 /***********************************************************/
106 /* character device */
108 static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
109 QTAILQ_HEAD_INITIALIZER(chardevs);
111 static void qemu_chr_event(CharDriverState *s, int event)
113 /* Keep track if the char device is open */
114 switch (event) {
115 case CHR_EVENT_OPENED:
116 s->opened = 1;
117 break;
118 case CHR_EVENT_CLOSED:
119 s->opened = 0;
120 break;
123 if (!s->chr_event)
124 return;
125 s->chr_event(s->handler_opaque, event, NULL);
128 static void qemu_chr_generic_open_bh(void *opaque)
130 CharDriverState *s = opaque;
131 qemu_chr_event(s, CHR_EVENT_OPENED);
132 qemu_bh_delete(s->bh);
133 s->bh = NULL;
136 void qemu_chr_generic_open(CharDriverState *s)
138 if (s->bh == NULL) {
139 s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s);
140 qemu_bh_schedule(s->bh);
144 static uint32_t char_queue_get_avail(CharQueue *q)
146 return sizeof(q->ring) - (q->prod - q->cons);
149 static bool char_queue_get_empty(CharQueue *q)
151 return (q->cons == q->prod);
154 static size_t char_queue_write(CharQueue *q, const void *data, size_t size)
156 const uint8_t *ptr = data;
157 size_t i;
159 for (i = 0; i < size; i++) {
160 if (char_queue_get_avail(q) == 0) {
161 break;
164 q->ring[q->prod % sizeof(q->ring)] = ptr[i];
165 q->prod++;
168 return i;
171 static size_t char_queue_read(CharQueue *q, void *data, size_t size)
173 uint8_t *ptr = data;
174 size_t i;
176 for (i = 0; i < size; i++) {
177 if (char_queue_get_empty(q)) {
178 break;
181 ptr[i] = q->ring[q->cons % sizeof(q->ring)];
182 q->cons++;
185 return i;
188 static void qemu_chr_flush_fe_tx(CharDriverState *s)
190 uint8_t buf[MAX_CHAR_QUEUE_RING];
191 int len;
193 /* Don't use polling queue if new style handlers are registered */
194 if (s->be_read || s->be_write) {
195 return;
198 /* Drain the queue into a flat buffer */
199 len = char_queue_read(&s->fe_tx, buf, sizeof(buf));
201 s->chr_write(s, buf, len);
203 /* We drop unwritten data until we have backend flow control */
206 int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
208 int ret;
209 bool is_empty;
211 assert(s->fe_opened > 0);
213 is_empty = char_queue_get_empty(&s->fe_tx);
215 ret = char_queue_write(&s->fe_tx, buf, len);
217 /* If the queue was empty, and now it's not, generate a read edge
218 * event for the backend. */
219 if (is_empty && !char_queue_get_empty(&s->fe_tx)) {
220 if (s->be_read) {
221 s->be_read(s->opaque);
225 qemu_chr_flush_fe_tx(s);
227 return ret;
230 int qemu_chr_fe_read(CharDriverState *s, uint8_t *buf, int len)
232 bool is_full;
233 int ret;
235 assert(s->fe_opened > 0);
237 is_full = (char_queue_get_avail(&s->be_tx) == 0);
239 ret = char_queue_read(&s->be_tx, buf, len);
241 /* If the queue was empty, and now its not, generate a write edge
242 * event for the backend. */
243 if (is_full && char_queue_get_avail(&s->be_tx)) {
244 if (s->be_write) {
245 s->be_write(s->opaque);
249 return ret;
252 void qemu_chr_fe_set_handlers(CharDriverState *s,
253 IOHandler *chr_read,
254 IOHandler *chr_write,
255 IOEventHandler *chr_event,
256 void *opaque)
258 assert(s->fe_opened > 0);
260 if (!opaque && !chr_read && !chr_write && !chr_event) {
261 /* chr driver being released. */
262 ++s->avail_connections;
264 s->fe_read = chr_read;
265 s->fe_write = chr_write;
266 s->chr_event = chr_event;
267 s->handler_opaque = opaque;
269 /* We're connecting to an already opened device, so let's make sure we
270 also get the open event */
271 if (s->opened) {
272 qemu_chr_generic_open(s);
276 int qemu_chr_be_can_write(CharDriverState *s)
278 /* Try to flush any queued data before returning how much data we can
279 * accept. */
280 return char_queue_get_avail(&s->be_tx);
283 int qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
285 int ret;
286 bool is_empty;
288 is_empty = char_queue_get_empty(&s->be_tx);
290 ret = char_queue_write(&s->be_tx, buf, len);
292 /* If the queue was empty, and now it's not, trigger a read edge
293 * event. */
294 if (is_empty && !char_queue_get_empty(&s->be_tx)) {
295 if (s->fe_read) {
296 s->fe_read(s->handler_opaque);
300 return ret;
303 int qemu_chr_be_read(CharDriverState *s, uint8_t *buf, int len)
305 bool is_full;
306 int ret;
308 is_full = (char_queue_get_avail(&s->fe_tx) == 0);
310 ret = char_queue_read(&s->fe_tx, buf, len);
312 /* If the queue was full, and now it's not, trigger an write edge
313 * event. */
314 if (is_full && char_queue_get_avail(&s->fe_tx)) {
315 if (s->fe_write) {
316 s->fe_write(s->handler_opaque);
320 return ret;
323 int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
325 if (!s->chr_ioctl)
326 return -ENOTSUP;
327 return s->chr_ioctl(s->opaque, cmd, arg);
330 void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
332 char buf[READ_BUF_LEN];
333 va_list ap;
334 va_start(ap, fmt);
335 vsnprintf(buf, sizeof(buf), fmt, ap);
336 qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
337 va_end(ap);
340 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
342 return len;
345 static int qemu_chr_open_null(QemuOpts *opts, CharDriverState **_chr)
347 CharDriverState *chr;
349 chr = qemu_mallocz(sizeof(CharDriverState));
350 chr->chr_write = null_chr_write;
352 *_chr= chr;
353 return 0;
356 #ifdef _WIN32
357 int send_all(int fd, const void *buf, int len1)
359 int ret, len;
361 len = len1;
362 while (len > 0) {
363 ret = send(fd, buf, len, 0);
364 if (ret < 0) {
365 errno = WSAGetLastError();
366 if (errno != WSAEWOULDBLOCK) {
367 return -1;
369 } else if (ret == 0) {
370 break;
371 } else {
372 buf += ret;
373 len -= ret;
376 return len1 - len;
379 #else
381 int send_all(int fd, const void *_buf, int len1)
383 int ret, len;
384 const uint8_t *buf = _buf;
386 len = len1;
387 while (len > 0) {
388 ret = write(fd, buf, len);
389 if (ret < 0) {
390 if (errno != EINTR && errno != EAGAIN)
391 return -1;
392 } else if (ret == 0) {
393 break;
394 } else {
395 buf += ret;
396 len -= ret;
399 return len1 - len;
401 #endif /* !_WIN32 */
403 #ifndef _WIN32
405 typedef struct {
406 int fd_in, fd_out;
407 int max_size;
408 } FDCharDriver;
410 #define STDIO_MAX_CLIENTS 1
411 static int stdio_nb_clients = 0;
413 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
415 FDCharDriver *s = chr->opaque;
416 return send_all(s->fd_out, buf, len);
419 static int fd_chr_read_poll(void *opaque)
421 CharDriverState *chr = opaque;
422 FDCharDriver *s = chr->opaque;
424 s->max_size = qemu_chr_be_can_write(chr);
425 return s->max_size;
428 static void fd_chr_read(void *opaque)
430 CharDriverState *chr = opaque;
431 FDCharDriver *s = chr->opaque;
432 int size, len;
433 uint8_t buf[READ_BUF_LEN];
435 len = sizeof(buf);
436 if (len > s->max_size)
437 len = s->max_size;
438 if (len == 0)
439 return;
440 size = read(s->fd_in, buf, len);
441 if (size == 0) {
442 /* FD has been closed. Remove it from the active list. */
443 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
444 qemu_chr_event(chr, CHR_EVENT_CLOSED);
445 return;
447 if (size > 0) {
448 qemu_chr_be_write(chr, buf, size);
452 static void fd_chr_close(struct CharDriverState *chr)
454 FDCharDriver *s = chr->opaque;
456 if (s->fd_in >= 0) {
457 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
458 } else {
459 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
463 qemu_free(s);
464 qemu_chr_event(chr, CHR_EVENT_CLOSED);
467 /* open a character device to a unix fd */
468 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
470 CharDriverState *chr;
471 FDCharDriver *s;
473 chr = qemu_mallocz(sizeof(CharDriverState));
474 s = qemu_mallocz(sizeof(FDCharDriver));
475 s->fd_in = fd_in;
476 s->fd_out = fd_out;
477 chr->opaque = s;
478 chr->chr_write = fd_chr_write;
479 chr->chr_close = fd_chr_close;
481 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
482 fd_chr_read, NULL, chr);
484 qemu_chr_generic_open(chr);
486 return chr;
489 static int qemu_chr_open_file_out(QemuOpts *opts, CharDriverState **_chr)
491 int fd_out;
493 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
494 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
495 if (fd_out < 0) {
496 return -errno;
499 *_chr = qemu_chr_open_fd(-1, fd_out);
500 return 0;
503 static int qemu_chr_open_pipe(QemuOpts *opts, CharDriverState **_chr)
505 int fd_in, fd_out;
506 char filename_in[256], filename_out[256];
507 const char *filename = qemu_opt_get(opts, "path");
509 if (filename == NULL) {
510 fprintf(stderr, "chardev: pipe: no filename given\n");
511 return -EINVAL;
514 snprintf(filename_in, 256, "%s.in", filename);
515 snprintf(filename_out, 256, "%s.out", filename);
516 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
517 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
518 if (fd_in < 0 || fd_out < 0) {
519 if (fd_in >= 0)
520 close(fd_in);
521 if (fd_out >= 0)
522 close(fd_out);
523 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
524 if (fd_in < 0) {
525 return -errno;
529 *_chr = qemu_chr_open_fd(fd_in, fd_out);
530 return 0;
534 /* for STDIO, we handle the case where several clients use it
535 (nographic mode) */
537 #define TERM_FIFO_MAX_SIZE 1
539 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
540 static int term_fifo_size;
542 static int stdio_read_poll(void *opaque)
544 CharDriverState *chr = opaque;
546 /* try to flush the queue if needed */
547 if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) {
548 qemu_chr_be_write(chr, term_fifo, 1);
549 term_fifo_size = 0;
551 /* see if we can absorb more chars */
552 if (term_fifo_size == 0)
553 return 1;
554 else
555 return 0;
558 static void stdio_read(void *opaque)
560 int size;
561 uint8_t buf[1];
562 CharDriverState *chr = opaque;
564 size = read(0, buf, 1);
565 if (size == 0) {
566 /* stdin has been closed. Remove it from the active list. */
567 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
568 qemu_chr_event(chr, CHR_EVENT_CLOSED);
569 return;
571 if (size > 0) {
572 if (qemu_chr_be_can_write(chr) > 0) {
573 qemu_chr_be_write(chr, buf, 1);
574 } else if (term_fifo_size == 0) {
575 term_fifo[term_fifo_size++] = buf[0];
580 /* init terminal so that we can grab keys */
581 static struct termios oldtty;
582 static int old_fd0_flags;
583 static bool stdio_allow_signal;
585 static void term_exit(void)
587 tcsetattr (0, TCSANOW, &oldtty);
588 fcntl(0, F_SETFL, old_fd0_flags);
591 static int qemu_chr_ioctl_stdio(void *opaque, int cmd, void *data)
593 struct termios tty;
594 bool *pecho = data;
596 if (cmd != CHR_SET_ECHO) {
597 return -ENOTSUP;
600 tty = oldtty;
601 if (!*pecho) {
602 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
603 |INLCR|IGNCR|ICRNL|IXON);
604 tty.c_oflag |= OPOST;
605 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
606 tty.c_cflag &= ~(CSIZE|PARENB);
607 tty.c_cflag |= CS8;
608 tty.c_cc[VMIN] = 1;
609 tty.c_cc[VTIME] = 0;
611 /* if graphical mode, we allow Ctrl-C handling */
612 if (!stdio_allow_signal)
613 tty.c_lflag &= ~ISIG;
615 tcsetattr (0, TCSANOW, &tty);
617 return 0;
620 static void qemu_chr_close_stdio(struct CharDriverState *chr)
622 term_exit();
623 stdio_nb_clients--;
624 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
625 fd_chr_close(chr);
628 static int qemu_chr_open_stdio(QemuOpts *opts, CharDriverState **_chr)
630 CharDriverState *chr;
632 if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
633 return -EBUSY;
636 if (stdio_nb_clients == 0) {
637 old_fd0_flags = fcntl(0, F_GETFL);
638 tcgetattr (0, &oldtty);
639 fcntl(0, F_SETFL, O_NONBLOCK);
640 atexit(term_exit);
643 chr = qemu_chr_open_fd(0, 1);
644 chr->chr_close = qemu_chr_close_stdio;
645 chr->chr_ioctl = qemu_chr_ioctl_stdio;
646 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
647 stdio_nb_clients++;
648 stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
649 display_type != DT_NOGRAPHIC);
650 qemu_chr_fe_set_echo(chr, false);
652 *_chr = chr;
653 return 0;
656 #ifdef __sun__
657 /* Once Solaris has openpty(), this is going to be removed. */
658 static int openpty(int *amaster, int *aslave, char *name,
659 struct termios *termp, struct winsize *winp)
661 const char *slave;
662 int mfd = -1, sfd = -1;
664 *amaster = *aslave = -1;
666 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
667 if (mfd < 0)
668 goto err;
670 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
671 goto err;
673 if ((slave = ptsname(mfd)) == NULL)
674 goto err;
676 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
677 goto err;
679 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
680 (termp != NULL && tcgetattr(sfd, termp) < 0))
681 goto err;
683 if (amaster)
684 *amaster = mfd;
685 if (aslave)
686 *aslave = sfd;
687 if (winp)
688 ioctl(sfd, TIOCSWINSZ, winp);
690 return 0;
692 err:
693 if (sfd != -1)
694 close(sfd);
695 close(mfd);
696 return -1;
699 static void cfmakeraw (struct termios *termios_p)
701 termios_p->c_iflag &=
702 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
703 termios_p->c_oflag &= ~OPOST;
704 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
705 termios_p->c_cflag &= ~(CSIZE|PARENB);
706 termios_p->c_cflag |= CS8;
708 termios_p->c_cc[VMIN] = 0;
709 termios_p->c_cc[VTIME] = 0;
711 #endif
713 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
714 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
715 || defined(__GLIBC__)
717 typedef struct {
718 int fd;
719 int connected;
720 int polling;
721 int read_bytes;
722 QEMUTimer *timer;
723 } PtyCharDriver;
725 static void pty_chr_update_read_handler(CharDriverState *chr);
726 static void pty_chr_state(CharDriverState *chr, int connected);
728 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
730 PtyCharDriver *s = chr->opaque;
732 if (!s->connected) {
733 /* guest sends data, check for (re-)connect */
734 pty_chr_update_read_handler(chr);
735 return 0;
737 return send_all(s->fd, buf, len);
740 static int pty_chr_read_poll(void *opaque)
742 CharDriverState *chr = opaque;
743 PtyCharDriver *s = chr->opaque;
745 s->read_bytes = qemu_chr_be_can_write(chr);
746 return s->read_bytes;
749 static void pty_chr_read(void *opaque)
751 CharDriverState *chr = opaque;
752 PtyCharDriver *s = chr->opaque;
753 int size, len;
754 uint8_t buf[READ_BUF_LEN];
756 len = sizeof(buf);
757 if (len > s->read_bytes)
758 len = s->read_bytes;
759 if (len == 0)
760 return;
761 size = read(s->fd, buf, len);
762 if ((size == -1 && errno == EIO) ||
763 (size == 0)) {
764 pty_chr_state(chr, 0);
765 return;
767 if (size > 0) {
768 pty_chr_state(chr, 1);
769 qemu_chr_be_write(chr, buf, size);
773 static void pty_chr_update_read_handler(CharDriverState *chr)
775 PtyCharDriver *s = chr->opaque;
777 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
778 pty_chr_read, NULL, chr);
779 s->polling = 1;
781 * Short timeout here: just need wait long enougth that qemu makes
782 * it through the poll loop once. When reconnected we want a
783 * short timeout so we notice it almost instantly. Otherwise
784 * read() gives us -EIO instantly, making pty_chr_state() reset the
785 * timeout to the normal (much longer) poll interval before the
786 * timer triggers.
788 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
791 static void pty_chr_state(CharDriverState *chr, int connected)
793 PtyCharDriver *s = chr->opaque;
795 if (!connected) {
796 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
797 s->connected = 0;
798 s->polling = 0;
799 /* (re-)connect poll interval for idle guests: once per second.
800 * We check more frequently in case the guests sends data to
801 * the virtual device linked to our pty. */
802 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
803 } else {
804 if (!s->connected)
805 qemu_chr_generic_open(chr);
806 s->connected = 1;
810 static void pty_chr_timer(void *opaque)
812 struct CharDriverState *chr = opaque;
813 PtyCharDriver *s = chr->opaque;
815 if (s->connected)
816 return;
817 if (s->polling) {
818 /* If we arrive here without polling being cleared due
819 * read returning -EIO, then we are (re-)connected */
820 pty_chr_state(chr, 1);
821 return;
824 /* Next poll ... */
825 pty_chr_update_read_handler(chr);
828 static void pty_chr_close(struct CharDriverState *chr)
830 PtyCharDriver *s = chr->opaque;
832 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
833 close(s->fd);
834 qemu_del_timer(s->timer);
835 qemu_free_timer(s->timer);
836 qemu_free(s);
837 qemu_chr_event(chr, CHR_EVENT_CLOSED);
840 static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
842 CharDriverState *chr;
843 PtyCharDriver *s;
844 struct termios tty;
845 int slave_fd, len;
846 #if defined(__OpenBSD__) || defined(__DragonFly__)
847 char pty_name[PATH_MAX];
848 #define q_ptsname(x) pty_name
849 #else
850 char *pty_name = NULL;
851 #define q_ptsname(x) ptsname(x)
852 #endif
854 chr = qemu_mallocz(sizeof(CharDriverState));
855 s = qemu_mallocz(sizeof(PtyCharDriver));
857 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
858 return -errno;
861 /* Set raw attributes on the pty. */
862 tcgetattr(slave_fd, &tty);
863 cfmakeraw(&tty);
864 tcsetattr(slave_fd, TCSAFLUSH, &tty);
865 close(slave_fd);
867 len = strlen(q_ptsname(s->fd)) + 5;
868 chr->filename = qemu_malloc(len);
869 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
870 qemu_opt_set(opts, "path", q_ptsname(s->fd));
871 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
873 chr->opaque = s;
874 chr->chr_write = pty_chr_write;
875 chr->chr_close = pty_chr_close;
877 s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
879 *_chr = chr;
880 return 0;
883 static void tty_serial_init(int fd, int speed,
884 int parity, int data_bits, int stop_bits)
886 struct termios tty;
887 speed_t spd;
889 #if 0
890 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
891 speed, parity, data_bits, stop_bits);
892 #endif
893 tcgetattr (fd, &tty);
895 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
896 speed = speed * 10 / 11;
897 do {
898 check_speed(50);
899 check_speed(75);
900 check_speed(110);
901 check_speed(134);
902 check_speed(150);
903 check_speed(200);
904 check_speed(300);
905 check_speed(600);
906 check_speed(1200);
907 check_speed(1800);
908 check_speed(2400);
909 check_speed(4800);
910 check_speed(9600);
911 check_speed(19200);
912 check_speed(38400);
913 /* Non-Posix values follow. They may be unsupported on some systems. */
914 check_speed(57600);
915 check_speed(115200);
916 #ifdef B230400
917 check_speed(230400);
918 #endif
919 #ifdef B460800
920 check_speed(460800);
921 #endif
922 #ifdef B500000
923 check_speed(500000);
924 #endif
925 #ifdef B576000
926 check_speed(576000);
927 #endif
928 #ifdef B921600
929 check_speed(921600);
930 #endif
931 #ifdef B1000000
932 check_speed(1000000);
933 #endif
934 #ifdef B1152000
935 check_speed(1152000);
936 #endif
937 #ifdef B1500000
938 check_speed(1500000);
939 #endif
940 #ifdef B2000000
941 check_speed(2000000);
942 #endif
943 #ifdef B2500000
944 check_speed(2500000);
945 #endif
946 #ifdef B3000000
947 check_speed(3000000);
948 #endif
949 #ifdef B3500000
950 check_speed(3500000);
951 #endif
952 #ifdef B4000000
953 check_speed(4000000);
954 #endif
955 spd = B115200;
956 } while (0);
958 cfsetispeed(&tty, spd);
959 cfsetospeed(&tty, spd);
961 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
962 |INLCR|IGNCR|ICRNL|IXON);
963 tty.c_oflag |= OPOST;
964 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
965 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
966 switch(data_bits) {
967 default:
968 case 8:
969 tty.c_cflag |= CS8;
970 break;
971 case 7:
972 tty.c_cflag |= CS7;
973 break;
974 case 6:
975 tty.c_cflag |= CS6;
976 break;
977 case 5:
978 tty.c_cflag |= CS5;
979 break;
981 switch(parity) {
982 default:
983 case 'N':
984 break;
985 case 'E':
986 tty.c_cflag |= PARENB;
987 break;
988 case 'O':
989 tty.c_cflag |= PARENB | PARODD;
990 break;
992 if (stop_bits == 2)
993 tty.c_cflag |= CSTOPB;
995 tcsetattr (fd, TCSANOW, &tty);
998 static int tty_serial_ioctl(void *opaque, int cmd, void *arg)
1000 FDCharDriver *s = opaque;
1002 switch(cmd) {
1003 case CHR_IOCTL_SERIAL_SET_PARAMS:
1005 QEMUSerialSetParams *ssp = arg;
1006 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1007 ssp->data_bits, ssp->stop_bits);
1009 break;
1010 case CHR_IOCTL_SERIAL_SET_BREAK:
1012 int enable = *(int *)arg;
1013 if (enable)
1014 tcsendbreak(s->fd_in, 1);
1016 break;
1017 case CHR_IOCTL_SERIAL_GET_TIOCM:
1019 int sarg = 0;
1020 int *targ = (int *)arg;
1021 ioctl(s->fd_in, TIOCMGET, &sarg);
1022 *targ = 0;
1023 if (sarg & TIOCM_CTS)
1024 *targ |= CHR_TIOCM_CTS;
1025 if (sarg & TIOCM_CAR)
1026 *targ |= CHR_TIOCM_CAR;
1027 if (sarg & TIOCM_DSR)
1028 *targ |= CHR_TIOCM_DSR;
1029 if (sarg & TIOCM_RI)
1030 *targ |= CHR_TIOCM_RI;
1031 if (sarg & TIOCM_DTR)
1032 *targ |= CHR_TIOCM_DTR;
1033 if (sarg & TIOCM_RTS)
1034 *targ |= CHR_TIOCM_RTS;
1036 break;
1037 case CHR_IOCTL_SERIAL_SET_TIOCM:
1039 int sarg = *(int *)arg;
1040 int targ = 0;
1041 ioctl(s->fd_in, TIOCMGET, &targ);
1042 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1043 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1044 if (sarg & CHR_TIOCM_CTS)
1045 targ |= TIOCM_CTS;
1046 if (sarg & CHR_TIOCM_CAR)
1047 targ |= TIOCM_CAR;
1048 if (sarg & CHR_TIOCM_DSR)
1049 targ |= TIOCM_DSR;
1050 if (sarg & CHR_TIOCM_RI)
1051 targ |= TIOCM_RI;
1052 if (sarg & CHR_TIOCM_DTR)
1053 targ |= TIOCM_DTR;
1054 if (sarg & CHR_TIOCM_RTS)
1055 targ |= TIOCM_RTS;
1056 ioctl(s->fd_in, TIOCMSET, &targ);
1058 break;
1059 default:
1060 return -ENOTSUP;
1062 return 0;
1065 static void qemu_chr_close_tty(CharDriverState *chr)
1067 FDCharDriver *s = chr->opaque;
1068 int fd = -1;
1070 if (s) {
1071 fd = s->fd_in;
1074 fd_chr_close(chr);
1076 if (fd >= 0) {
1077 close(fd);
1081 static int qemu_chr_open_tty(QemuOpts *opts, CharDriverState **_chr)
1083 const char *filename = qemu_opt_get(opts, "path");
1084 CharDriverState *chr;
1085 int fd;
1087 TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
1088 if (fd < 0) {
1089 return -errno;
1091 tty_serial_init(fd, 115200, 'N', 8, 1);
1092 chr = qemu_chr_open_fd(fd, fd);
1093 chr->chr_ioctl = tty_serial_ioctl;
1094 chr->chr_close = qemu_chr_close_tty;
1096 *_chr = chr;
1097 return 0;
1099 #else /* ! __linux__ && ! __sun__ */
1100 static int qemu_chr_open_pty(QemuOpts *opts, CharDriverState **_chr)
1102 return -ENOTSUP;
1104 #endif /* __linux__ || __sun__ */
1106 #if defined(__linux__)
1107 typedef struct {
1108 int fd;
1109 int mode;
1110 } ParallelCharDriver;
1112 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1114 if (s->mode != mode) {
1115 int m = mode;
1116 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1117 return 0;
1118 s->mode = mode;
1120 return 1;
1123 static int pp_ioctl(void *opaque, int cmd, void *arg)
1125 ParallelCharDriver *drv = opaque;
1126 int fd = drv->fd;
1127 uint8_t b;
1129 switch(cmd) {
1130 case CHR_IOCTL_PP_READ_DATA:
1131 if (ioctl(fd, PPRDATA, &b) < 0)
1132 return -ENOTSUP;
1133 *(uint8_t *)arg = b;
1134 break;
1135 case CHR_IOCTL_PP_WRITE_DATA:
1136 b = *(uint8_t *)arg;
1137 if (ioctl(fd, PPWDATA, &b) < 0)
1138 return -ENOTSUP;
1139 break;
1140 case CHR_IOCTL_PP_READ_CONTROL:
1141 if (ioctl(fd, PPRCONTROL, &b) < 0)
1142 return -ENOTSUP;
1143 /* Linux gives only the lowest bits, and no way to know data
1144 direction! For better compatibility set the fixed upper
1145 bits. */
1146 *(uint8_t *)arg = b | 0xc0;
1147 break;
1148 case CHR_IOCTL_PP_WRITE_CONTROL:
1149 b = *(uint8_t *)arg;
1150 if (ioctl(fd, PPWCONTROL, &b) < 0)
1151 return -ENOTSUP;
1152 break;
1153 case CHR_IOCTL_PP_READ_STATUS:
1154 if (ioctl(fd, PPRSTATUS, &b) < 0)
1155 return -ENOTSUP;
1156 *(uint8_t *)arg = b;
1157 break;
1158 case CHR_IOCTL_PP_DATA_DIR:
1159 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1160 return -ENOTSUP;
1161 break;
1162 case CHR_IOCTL_PP_EPP_READ_ADDR:
1163 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1164 struct ParallelIOArg *parg = arg;
1165 int n = read(fd, parg->buffer, parg->count);
1166 if (n != parg->count) {
1167 return -EIO;
1170 break;
1171 case CHR_IOCTL_PP_EPP_READ:
1172 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1173 struct ParallelIOArg *parg = arg;
1174 int n = read(fd, parg->buffer, parg->count);
1175 if (n != parg->count) {
1176 return -EIO;
1179 break;
1180 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1181 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1182 struct ParallelIOArg *parg = arg;
1183 int n = write(fd, parg->buffer, parg->count);
1184 if (n != parg->count) {
1185 return -EIO;
1188 break;
1189 case CHR_IOCTL_PP_EPP_WRITE:
1190 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1191 struct ParallelIOArg *parg = arg;
1192 int n = write(fd, parg->buffer, parg->count);
1193 if (n != parg->count) {
1194 return -EIO;
1197 break;
1198 default:
1199 return -ENOTSUP;
1201 return 0;
1204 static void pp_close(CharDriverState *chr)
1206 ParallelCharDriver *drv = chr->opaque;
1207 int fd = drv->fd;
1209 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1210 ioctl(fd, PPRELEASE);
1211 close(fd);
1212 qemu_free(drv);
1213 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1216 static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
1218 const char *filename = qemu_opt_get(opts, "path");
1219 CharDriverState *chr;
1220 ParallelCharDriver *drv;
1221 int fd;
1223 TFR(fd = open(filename, O_RDWR));
1224 if (fd < 0) {
1225 return -errno;
1228 if (ioctl(fd, PPCLAIM) < 0) {
1229 close(fd);
1230 return -errno;
1233 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1234 drv->fd = fd;
1235 drv->mode = IEEE1284_MODE_COMPAT;
1237 chr = qemu_mallocz(sizeof(CharDriverState));
1238 chr->chr_write = null_chr_write;
1239 chr->chr_ioctl = pp_ioctl;
1240 chr->chr_close = pp_close;
1241 chr->opaque = drv;
1243 qemu_chr_generic_open(chr);
1245 *_chr = chr;
1246 return 0;
1248 #endif /* __linux__ */
1250 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1251 static int pp_ioctl(void *opaque, int cmd, void *arg)
1253 int fd = (int)(intptr_t)opaque;
1254 uint8_t b;
1256 switch(cmd) {
1257 case CHR_IOCTL_PP_READ_DATA:
1258 if (ioctl(fd, PPIGDATA, &b) < 0)
1259 return -ENOTSUP;
1260 *(uint8_t *)arg = b;
1261 break;
1262 case CHR_IOCTL_PP_WRITE_DATA:
1263 b = *(uint8_t *)arg;
1264 if (ioctl(fd, PPISDATA, &b) < 0)
1265 return -ENOTSUP;
1266 break;
1267 case CHR_IOCTL_PP_READ_CONTROL:
1268 if (ioctl(fd, PPIGCTRL, &b) < 0)
1269 return -ENOTSUP;
1270 *(uint8_t *)arg = b;
1271 break;
1272 case CHR_IOCTL_PP_WRITE_CONTROL:
1273 b = *(uint8_t *)arg;
1274 if (ioctl(fd, PPISCTRL, &b) < 0)
1275 return -ENOTSUP;
1276 break;
1277 case CHR_IOCTL_PP_READ_STATUS:
1278 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1279 return -ENOTSUP;
1280 *(uint8_t *)arg = b;
1281 break;
1282 default:
1283 return -ENOTSUP;
1285 return 0;
1288 static int qemu_chr_open_pp(QemuOpts *opts, CharDriverState **_chr)
1290 const char *filename = qemu_opt_get(opts, "path");
1291 CharDriverState *chr;
1292 int fd;
1294 fd = qemu_open(filename, O_RDWR);
1295 if (fd < 0) {
1296 return -errno;
1299 chr = qemu_mallocz(sizeof(CharDriverState));
1300 chr->opaque = (void *)(intptr_t)fd;
1301 chr->chr_write = null_chr_write;
1302 chr->chr_ioctl = pp_ioctl;
1304 *_chr = chr;
1305 return 0;
1307 #endif
1309 #else /* _WIN32 */
1311 typedef struct {
1312 int max_size;
1313 HANDLE hcom, hrecv, hsend;
1314 OVERLAPPED orecv, osend;
1315 BOOL fpipe;
1316 DWORD len;
1317 } WinCharState;
1319 #define NSENDBUF 2048
1320 #define NRECVBUF 2048
1321 #define MAXCONNECT 1
1322 #define NTIMEOUT 5000
1324 static int win_chr_poll(void *opaque);
1325 static int win_chr_pipe_poll(void *opaque);
1327 static void win_chr_close(CharDriverState *chr)
1329 WinCharState *s = chr->opaque;
1331 if (s->hsend) {
1332 CloseHandle(s->hsend);
1333 s->hsend = NULL;
1335 if (s->hrecv) {
1336 CloseHandle(s->hrecv);
1337 s->hrecv = NULL;
1339 if (s->hcom) {
1340 CloseHandle(s->hcom);
1341 s->hcom = NULL;
1343 if (s->fpipe)
1344 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1345 else
1346 qemu_del_polling_cb(win_chr_poll, chr);
1348 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1351 static int win_chr_init(CharDriverState *chr, const char *filename)
1353 WinCharState *s = chr->opaque;
1354 COMMCONFIG comcfg;
1355 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1356 COMSTAT comstat;
1357 DWORD size;
1358 DWORD err;
1360 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1361 if (!s->hsend) {
1362 fprintf(stderr, "Failed CreateEvent\n");
1363 goto fail;
1365 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1366 if (!s->hrecv) {
1367 fprintf(stderr, "Failed CreateEvent\n");
1368 goto fail;
1371 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1372 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1373 if (s->hcom == INVALID_HANDLE_VALUE) {
1374 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1375 s->hcom = NULL;
1376 goto fail;
1379 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1380 fprintf(stderr, "Failed SetupComm\n");
1381 goto fail;
1384 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1385 size = sizeof(COMMCONFIG);
1386 GetDefaultCommConfig(filename, &comcfg, &size);
1387 comcfg.dcb.DCBlength = sizeof(DCB);
1388 CommConfigDialog(filename, NULL, &comcfg);
1390 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1391 fprintf(stderr, "Failed SetCommState\n");
1392 goto fail;
1395 if (!SetCommMask(s->hcom, EV_ERR)) {
1396 fprintf(stderr, "Failed SetCommMask\n");
1397 goto fail;
1400 cto.ReadIntervalTimeout = MAXDWORD;
1401 if (!SetCommTimeouts(s->hcom, &cto)) {
1402 fprintf(stderr, "Failed SetCommTimeouts\n");
1403 goto fail;
1406 if (!ClearCommError(s->hcom, &err, &comstat)) {
1407 fprintf(stderr, "Failed ClearCommError\n");
1408 goto fail;
1410 qemu_add_polling_cb(win_chr_poll, chr);
1411 return 0;
1413 fail:
1414 win_chr_close(chr);
1415 return -1;
1418 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1420 WinCharState *s = chr->opaque;
1421 DWORD len, ret, size, err;
1423 len = len1;
1424 ZeroMemory(&s->osend, sizeof(s->osend));
1425 s->osend.hEvent = s->hsend;
1426 while (len > 0) {
1427 if (s->hsend)
1428 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1429 else
1430 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1431 if (!ret) {
1432 err = GetLastError();
1433 if (err == ERROR_IO_PENDING) {
1434 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1435 if (ret) {
1436 buf += size;
1437 len -= size;
1438 } else {
1439 break;
1441 } else {
1442 break;
1444 } else {
1445 buf += size;
1446 len -= size;
1449 return len1 - len;
1452 static int win_chr_read_poll(CharDriverState *chr)
1454 WinCharState *s = chr->opaque;
1456 s->max_size = qemu_chr_be_can_write(chr);
1457 return s->max_size;
1460 static void win_chr_readfile(CharDriverState *chr)
1462 WinCharState *s = chr->opaque;
1463 int ret, err;
1464 uint8_t buf[READ_BUF_LEN];
1465 DWORD size;
1467 ZeroMemory(&s->orecv, sizeof(s->orecv));
1468 s->orecv.hEvent = s->hrecv;
1469 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1470 if (!ret) {
1471 err = GetLastError();
1472 if (err == ERROR_IO_PENDING) {
1473 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1477 if (size > 0) {
1478 qemu_chr_be_write(chr, buf, size);
1482 static void win_chr_read(CharDriverState *chr)
1484 WinCharState *s = chr->opaque;
1486 if (s->len > s->max_size)
1487 s->len = s->max_size;
1488 if (s->len == 0)
1489 return;
1491 win_chr_readfile(chr);
1494 static int win_chr_poll(void *opaque)
1496 CharDriverState *chr = opaque;
1497 WinCharState *s = chr->opaque;
1498 COMSTAT status;
1499 DWORD comerr;
1501 ClearCommError(s->hcom, &comerr, &status);
1502 if (status.cbInQue > 0) {
1503 s->len = status.cbInQue;
1504 win_chr_read_poll(chr);
1505 win_chr_read(chr);
1506 return 1;
1508 return 0;
1511 static int qemu_chr_open_win(QemuOpts *opts, CharDriverState **_chr)
1513 const char *filename = qemu_opt_get(opts, "path");
1514 CharDriverState *chr;
1515 WinCharState *s;
1517 chr = qemu_mallocz(sizeof(CharDriverState));
1518 s = qemu_mallocz(sizeof(WinCharState));
1519 chr->opaque = s;
1520 chr->chr_write = win_chr_write;
1521 chr->chr_close = win_chr_close;
1523 if (win_chr_init(chr, filename) < 0) {
1524 free(s);
1525 free(chr);
1526 return -EIO;
1528 qemu_chr_generic_open(chr);
1530 *_chr = chr;
1531 return 0;
1534 static int win_chr_pipe_poll(void *opaque)
1536 CharDriverState *chr = opaque;
1537 WinCharState *s = chr->opaque;
1538 DWORD size;
1540 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1541 if (size > 0) {
1542 s->len = size;
1543 win_chr_read_poll(chr);
1544 win_chr_read(chr);
1545 return 1;
1547 return 0;
1550 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1552 WinCharState *s = chr->opaque;
1553 OVERLAPPED ov;
1554 int ret;
1555 DWORD size;
1556 char openname[256];
1558 s->fpipe = TRUE;
1560 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1561 if (!s->hsend) {
1562 fprintf(stderr, "Failed CreateEvent\n");
1563 goto fail;
1565 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1566 if (!s->hrecv) {
1567 fprintf(stderr, "Failed CreateEvent\n");
1568 goto fail;
1571 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1572 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1573 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1574 PIPE_WAIT,
1575 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1576 if (s->hcom == INVALID_HANDLE_VALUE) {
1577 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1578 s->hcom = NULL;
1579 goto fail;
1582 ZeroMemory(&ov, sizeof(ov));
1583 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1584 ret = ConnectNamedPipe(s->hcom, &ov);
1585 if (ret) {
1586 fprintf(stderr, "Failed ConnectNamedPipe\n");
1587 goto fail;
1590 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1591 if (!ret) {
1592 fprintf(stderr, "Failed GetOverlappedResult\n");
1593 if (ov.hEvent) {
1594 CloseHandle(ov.hEvent);
1595 ov.hEvent = NULL;
1597 goto fail;
1600 if (ov.hEvent) {
1601 CloseHandle(ov.hEvent);
1602 ov.hEvent = NULL;
1604 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1605 return 0;
1607 fail:
1608 win_chr_close(chr);
1609 return -1;
1613 static int qemu_chr_open_win_pipe(QemuOpts *opts, CharDriverState **_chr)
1615 const char *filename = qemu_opt_get(opts, "path");
1616 CharDriverState *chr;
1617 WinCharState *s;
1619 chr = qemu_mallocz(sizeof(CharDriverState));
1620 s = qemu_mallocz(sizeof(WinCharState));
1621 chr->opaque = s;
1622 chr->chr_write = win_chr_write;
1623 chr->chr_close = win_chr_close;
1625 if (win_chr_pipe_init(chr, filename) < 0) {
1626 free(s);
1627 free(chr);
1628 return -EIO;
1630 qemu_chr_generic_open(chr);
1632 *_chr = chr;
1633 return 0;
1636 static int qemu_chr_open_win_file(HANDLE fd_out, CharDriverState **pchr)
1638 CharDriverState *chr;
1639 WinCharState *s;
1641 chr = qemu_mallocz(sizeof(CharDriverState));
1642 s = qemu_mallocz(sizeof(WinCharState));
1643 s->hcom = fd_out;
1644 chr->opaque = s;
1645 chr->chr_write = win_chr_write;
1646 qemu_chr_generic_open(chr);
1647 *pchr = chr;
1648 return 0;
1651 static int qemu_chr_open_win_con(QemuOpts *opts, CharDriverState **chr)
1653 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE), chr);
1656 static int qemu_chr_open_win_file_out(QemuOpts *opts, CharDriverState **_chr)
1658 const char *file_out = qemu_opt_get(opts, "path");
1659 HANDLE fd_out;
1661 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1662 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1663 if (fd_out == INVALID_HANDLE_VALUE) {
1664 return -EIO;
1667 return qemu_chr_open_win_file(fd_out, _chr);
1669 #endif /* !_WIN32 */
1671 /***********************************************************/
1672 /* UDP Net console */
1674 typedef struct {
1675 int fd;
1676 uint8_t buf[READ_BUF_LEN];
1677 int bufcnt;
1678 int bufptr;
1679 int max_size;
1680 } NetCharDriver;
1682 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1684 NetCharDriver *s = chr->opaque;
1686 return send(s->fd, (const void *)buf, len, 0);
1689 static int udp_chr_read_poll(void *opaque)
1691 CharDriverState *chr = opaque;
1692 NetCharDriver *s = chr->opaque;
1694 s->max_size = qemu_chr_be_can_write(chr);
1696 /* If there were any stray characters in the queue process them
1697 * first
1699 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1700 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
1701 s->bufptr++;
1702 s->max_size = qemu_chr_be_can_write(chr);
1704 return s->max_size;
1707 static void udp_chr_read(void *opaque)
1709 CharDriverState *chr = opaque;
1710 NetCharDriver *s = chr->opaque;
1712 if (s->max_size == 0)
1713 return;
1714 s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
1715 s->bufptr = s->bufcnt;
1716 if (s->bufcnt <= 0)
1717 return;
1719 s->bufptr = 0;
1720 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1721 qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
1722 s->bufptr++;
1723 s->max_size = qemu_chr_be_can_write(chr);
1727 static void udp_chr_close(CharDriverState *chr)
1729 NetCharDriver *s = chr->opaque;
1730 if (s->fd >= 0) {
1731 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1732 closesocket(s->fd);
1734 qemu_free(s);
1735 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1738 static int qemu_chr_open_udp(QemuOpts *opts, CharDriverState **_chr)
1740 CharDriverState *chr = NULL;
1741 NetCharDriver *s = NULL;
1742 int fd = -1;
1743 int ret;
1745 chr = qemu_mallocz(sizeof(CharDriverState));
1746 s = qemu_mallocz(sizeof(NetCharDriver));
1748 fd = inet_dgram_opts(opts);
1749 if (fd < 0) {
1750 fprintf(stderr, "inet_dgram_opts failed\n");
1751 ret = -errno;
1752 goto return_err;
1755 s->fd = fd;
1756 s->bufcnt = 0;
1757 s->bufptr = 0;
1758 chr->opaque = s;
1759 chr->chr_write = udp_chr_write;
1760 chr->chr_close = udp_chr_close;
1762 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1763 udp_chr_read, NULL, chr);
1765 *_chr = chr;
1766 return 0;
1768 return_err:
1769 qemu_free(chr);
1770 qemu_free(s);
1771 if (fd >= 0) {
1772 closesocket(fd);
1774 return ret;
1777 /***********************************************************/
1778 /* TCP Net console */
1780 typedef struct {
1781 int fd, listen_fd;
1782 int connected;
1783 int max_size;
1784 int do_telnetopt;
1785 int do_nodelay;
1786 int is_unix;
1787 int msgfd;
1788 } TCPCharDriver;
1790 static void tcp_chr_accept(void *opaque);
1792 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1794 TCPCharDriver *s = chr->opaque;
1795 if (s->connected) {
1796 return send_all(s->fd, buf, len);
1797 } else {
1798 /* XXX: indicate an error ? */
1799 return len;
1803 static int tcp_chr_read_poll(void *opaque)
1805 CharDriverState *chr = opaque;
1806 TCPCharDriver *s = chr->opaque;
1807 if (!s->connected)
1808 return 0;
1809 s->max_size = qemu_chr_be_can_write(chr);
1810 return s->max_size;
1813 #define IAC 255
1814 #define IAC_BREAK 243
1815 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1816 TCPCharDriver *s,
1817 uint8_t *buf, int *size)
1819 /* Handle any telnet client's basic IAC options to satisfy char by
1820 * char mode with no echo. All IAC options will be removed from
1821 * the buf and the do_telnetopt variable will be used to track the
1822 * state of the width of the IAC information.
1824 * IAC commands come in sets of 3 bytes with the exception of the
1825 * "IAC BREAK" command and the double IAC.
1828 int i;
1829 int j = 0;
1831 for (i = 0; i < *size; i++) {
1832 if (s->do_telnetopt > 1) {
1833 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1834 /* Double IAC means send an IAC */
1835 if (j != i)
1836 buf[j] = buf[i];
1837 j++;
1838 s->do_telnetopt = 1;
1839 } else {
1840 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1841 /* Handle IAC break commands by sending a serial break */
1842 qemu_chr_event(chr, CHR_EVENT_BREAK);
1843 s->do_telnetopt++;
1845 s->do_telnetopt++;
1847 if (s->do_telnetopt >= 4) {
1848 s->do_telnetopt = 1;
1850 } else {
1851 if ((unsigned char)buf[i] == IAC) {
1852 s->do_telnetopt = 2;
1853 } else {
1854 if (j != i)
1855 buf[j] = buf[i];
1856 j++;
1860 *size = j;
1863 static int tcp_chr_ioctl(void *opaque, int cmd, void *data)
1865 TCPCharDriver *s = opaque;
1866 int ret;
1868 switch (cmd) {
1869 case CHR_GET_MSGFD: {
1870 int *pfd = data;
1871 *pfd = s->msgfd;
1872 s->msgfd = -1;
1873 ret = 0;
1874 break;
1876 default:
1877 ret = -ENOTSUP;
1878 break;
1881 return ret;
1884 #ifndef _WIN32
1885 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1887 TCPCharDriver *s = chr->opaque;
1888 struct cmsghdr *cmsg;
1890 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1891 int fd;
1893 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1894 cmsg->cmsg_level != SOL_SOCKET ||
1895 cmsg->cmsg_type != SCM_RIGHTS)
1896 continue;
1898 fd = *((int *)CMSG_DATA(cmsg));
1899 if (fd < 0)
1900 continue;
1902 if (s->msgfd != -1)
1903 close(s->msgfd);
1904 s->msgfd = fd;
1908 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1910 TCPCharDriver *s = chr->opaque;
1911 struct msghdr msg = { NULL, };
1912 struct iovec iov[1];
1913 union {
1914 struct cmsghdr cmsg;
1915 char control[CMSG_SPACE(sizeof(int))];
1916 } msg_control;
1917 ssize_t ret;
1919 iov[0].iov_base = buf;
1920 iov[0].iov_len = len;
1922 msg.msg_iov = iov;
1923 msg.msg_iovlen = 1;
1924 msg.msg_control = &msg_control;
1925 msg.msg_controllen = sizeof(msg_control);
1927 ret = recvmsg(s->fd, &msg, 0);
1928 if (ret > 0 && s->is_unix)
1929 unix_process_msgfd(chr, &msg);
1931 return ret;
1933 #else
1934 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1936 TCPCharDriver *s = chr->opaque;
1937 return qemu_recv(s->fd, buf, len, 0);
1939 #endif
1941 static void tcp_chr_read(void *opaque)
1943 CharDriverState *chr = opaque;
1944 TCPCharDriver *s = chr->opaque;
1945 uint8_t buf[READ_BUF_LEN];
1946 int len, size;
1948 if (!s->connected || s->max_size <= 0)
1949 return;
1950 len = sizeof(buf);
1951 if (len > s->max_size)
1952 len = s->max_size;
1953 size = tcp_chr_recv(chr, (void *)buf, len);
1954 if (size == 0) {
1955 /* connection closed */
1956 s->connected = 0;
1957 if (s->listen_fd >= 0) {
1958 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
1960 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1961 closesocket(s->fd);
1962 s->fd = -1;
1963 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1964 } else if (size > 0) {
1965 if (s->do_telnetopt)
1966 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
1967 if (size > 0)
1968 qemu_chr_be_write(chr, buf, size);
1972 #ifndef _WIN32
1973 CharDriverState *qemu_chr_open_eventfd(int eventfd)
1975 return qemu_chr_open_fd(eventfd, eventfd);
1977 #endif
1979 static void tcp_chr_connect(void *opaque)
1981 CharDriverState *chr = opaque;
1982 TCPCharDriver *s = chr->opaque;
1984 s->connected = 1;
1985 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
1986 tcp_chr_read, NULL, chr);
1987 qemu_chr_generic_open(chr);
1990 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
1991 static void tcp_chr_telnet_init(int fd)
1993 char buf[3];
1994 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
1995 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
1996 send(fd, (char *)buf, 3, 0);
1997 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
1998 send(fd, (char *)buf, 3, 0);
1999 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2000 send(fd, (char *)buf, 3, 0);
2001 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2002 send(fd, (char *)buf, 3, 0);
2005 static void socket_set_nodelay(int fd)
2007 int val = 1;
2008 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2011 static int tcp_chr_add_client(CharDriverState *chr, int fd)
2013 TCPCharDriver *s = chr->opaque;
2014 if (s->fd != -1)
2015 return -1;
2017 socket_set_nonblock(fd);
2018 if (s->do_nodelay)
2019 socket_set_nodelay(fd);
2020 s->fd = fd;
2021 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2022 tcp_chr_connect(chr);
2024 return 0;
2027 static void tcp_chr_accept(void *opaque)
2029 CharDriverState *chr = opaque;
2030 TCPCharDriver *s = chr->opaque;
2031 struct sockaddr_in saddr;
2032 #ifndef _WIN32
2033 struct sockaddr_un uaddr;
2034 #endif
2035 struct sockaddr *addr;
2036 socklen_t len;
2037 int fd;
2039 for(;;) {
2040 #ifndef _WIN32
2041 if (s->is_unix) {
2042 len = sizeof(uaddr);
2043 addr = (struct sockaddr *)&uaddr;
2044 } else
2045 #endif
2047 len = sizeof(saddr);
2048 addr = (struct sockaddr *)&saddr;
2050 fd = qemu_accept(s->listen_fd, addr, &len);
2051 if (fd < 0 && errno != EINTR) {
2052 return;
2053 } else if (fd >= 0) {
2054 if (s->do_telnetopt)
2055 tcp_chr_telnet_init(fd);
2056 break;
2059 if (tcp_chr_add_client(chr, fd) < 0)
2060 close(fd);
2063 static void tcp_chr_close(CharDriverState *chr)
2065 TCPCharDriver *s = chr->opaque;
2066 if (s->fd >= 0) {
2067 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2068 closesocket(s->fd);
2070 if (s->listen_fd >= 0) {
2071 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2072 closesocket(s->listen_fd);
2074 qemu_free(s);
2075 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2078 static int qemu_chr_open_socket(QemuOpts *opts, CharDriverState **_chr)
2080 CharDriverState *chr = NULL;
2081 TCPCharDriver *s = NULL;
2082 int fd = -1;
2083 int is_listen;
2084 int is_waitconnect;
2085 int do_nodelay;
2086 int is_unix;
2087 int is_telnet;
2088 int ret;
2090 is_listen = qemu_opt_get_bool(opts, "server", 0);
2091 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2092 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2093 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2094 is_unix = qemu_opt_get(opts, "path") != NULL;
2095 if (!is_listen)
2096 is_waitconnect = 0;
2098 chr = qemu_mallocz(sizeof(CharDriverState));
2099 s = qemu_mallocz(sizeof(TCPCharDriver));
2101 if (is_unix) {
2102 if (is_listen) {
2103 fd = unix_listen_opts(opts);
2104 } else {
2105 fd = unix_connect_opts(opts);
2107 } else {
2108 if (is_listen) {
2109 fd = inet_listen_opts(opts, 0);
2110 } else {
2111 fd = inet_connect_opts(opts);
2114 if (fd < 0) {
2115 ret = -errno;
2116 goto fail;
2119 if (!is_waitconnect)
2120 socket_set_nonblock(fd);
2122 s->connected = 0;
2123 s->fd = -1;
2124 s->listen_fd = -1;
2125 s->msgfd = -1;
2126 s->is_unix = is_unix;
2127 s->do_nodelay = do_nodelay && !is_unix;
2129 chr->opaque = s;
2130 chr->chr_write = tcp_chr_write;
2131 chr->chr_close = tcp_chr_close;
2132 chr->chr_ioctl = tcp_chr_ioctl;
2134 if (is_listen) {
2135 s->listen_fd = fd;
2136 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2137 if (is_telnet)
2138 s->do_telnetopt = 1;
2140 } else {
2141 s->connected = 1;
2142 s->fd = fd;
2143 socket_set_nodelay(fd);
2144 tcp_chr_connect(chr);
2147 /* for "info chardev" monitor command */
2148 chr->filename = qemu_malloc(256);
2149 if (is_unix) {
2150 snprintf(chr->filename, 256, "unix:%s%s",
2151 qemu_opt_get(opts, "path"),
2152 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2153 } else if (is_telnet) {
2154 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2155 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2156 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2157 } else {
2158 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2159 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2160 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2163 if (is_listen && is_waitconnect) {
2164 printf("QEMU waiting for connection on: %s\n",
2165 chr->filename);
2166 tcp_chr_accept(chr);
2167 socket_set_nonblock(s->listen_fd);
2170 *_chr = chr;
2171 return 0;
2173 fail:
2174 if (fd >= 0)
2175 closesocket(fd);
2176 qemu_free(s);
2177 qemu_free(chr);
2178 return ret;
2181 /***********************************************************/
2182 /* Memory chardev */
2183 typedef struct {
2184 size_t outbuf_size;
2185 size_t outbuf_capacity;
2186 uint8_t *outbuf;
2187 } MemoryDriver;
2189 static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2191 MemoryDriver *d = chr->opaque;
2193 /* TODO: the QString implementation has the same code, we should
2194 * introduce a generic way to do this in cutils.c */
2195 if (d->outbuf_capacity < d->outbuf_size + len) {
2196 /* grow outbuf */
2197 d->outbuf_capacity += len;
2198 d->outbuf_capacity *= 2;
2199 d->outbuf = qemu_realloc(d->outbuf, d->outbuf_capacity);
2202 memcpy(d->outbuf + d->outbuf_size, buf, len);
2203 d->outbuf_size += len;
2205 return len;
2208 void qemu_chr_init_mem(CharDriverState *chr)
2210 MemoryDriver *d;
2212 d = qemu_malloc(sizeof(*d));
2213 d->outbuf_size = 0;
2214 d->outbuf_capacity = 4096;
2215 d->outbuf = qemu_mallocz(d->outbuf_capacity);
2217 memset(chr, 0, sizeof(*chr));
2218 chr->opaque = d;
2219 chr->chr_write = mem_chr_write;
2222 QString *qemu_chr_mem_to_qs(CharDriverState *chr)
2224 MemoryDriver *d = chr->opaque;
2225 return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
2228 /* NOTE: this driver can not be closed with qemu_chr_fe_delete()! */
2229 void qemu_chr_close_mem(CharDriverState *chr)
2231 MemoryDriver *d = chr->opaque;
2233 qemu_free(d->outbuf);
2234 qemu_free(chr->opaque);
2235 chr->opaque = NULL;
2236 chr->chr_write = NULL;
2239 size_t qemu_chr_mem_osize(const CharDriverState *chr)
2241 const MemoryDriver *d = chr->opaque;
2242 return d->outbuf_size;
2245 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2247 char host[65], port[33], width[8], height[8];
2248 int pos;
2249 const char *p;
2250 QemuOpts *opts;
2252 opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1);
2253 if (NULL == opts)
2254 return NULL;
2256 if (strcmp(filename, "null") == 0 ||
2257 strcmp(filename, "pty") == 0 ||
2258 strcmp(filename, "msmouse") == 0 ||
2259 strcmp(filename, "braille") == 0 ||
2260 strcmp(filename, "stdio") == 0) {
2261 qemu_opt_set(opts, "backend", filename);
2262 return opts;
2264 if (strstart(filename, "vc", &p)) {
2265 qemu_opt_set(opts, "backend", "vc");
2266 if (*p == ':') {
2267 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2268 /* pixels */
2269 qemu_opt_set(opts, "width", width);
2270 qemu_opt_set(opts, "height", height);
2271 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2272 /* chars */
2273 qemu_opt_set(opts, "cols", width);
2274 qemu_opt_set(opts, "rows", height);
2275 } else {
2276 goto fail;
2279 return opts;
2281 if (strcmp(filename, "con:") == 0) {
2282 qemu_opt_set(opts, "backend", "console");
2283 return opts;
2285 if (strstart(filename, "COM", NULL)) {
2286 qemu_opt_set(opts, "backend", "serial");
2287 qemu_opt_set(opts, "path", filename);
2288 return opts;
2290 if (strstart(filename, "file:", &p)) {
2291 qemu_opt_set(opts, "backend", "file");
2292 qemu_opt_set(opts, "path", p);
2293 return opts;
2295 if (strstart(filename, "pipe:", &p)) {
2296 qemu_opt_set(opts, "backend", "pipe");
2297 qemu_opt_set(opts, "path", p);
2298 return opts;
2300 if (strstart(filename, "tcp:", &p) ||
2301 strstart(filename, "telnet:", &p)) {
2302 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2303 host[0] = 0;
2304 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2305 goto fail;
2307 qemu_opt_set(opts, "backend", "socket");
2308 qemu_opt_set(opts, "host", host);
2309 qemu_opt_set(opts, "port", port);
2310 if (p[pos] == ',') {
2311 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2312 goto fail;
2314 if (strstart(filename, "telnet:", &p))
2315 qemu_opt_set(opts, "telnet", "on");
2316 return opts;
2318 if (strstart(filename, "udp:", &p)) {
2319 qemu_opt_set(opts, "backend", "udp");
2320 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2321 host[0] = 0;
2322 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
2323 goto fail;
2326 qemu_opt_set(opts, "host", host);
2327 qemu_opt_set(opts, "port", port);
2328 if (p[pos] == '@') {
2329 p += pos + 1;
2330 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2331 host[0] = 0;
2332 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2333 goto fail;
2336 qemu_opt_set(opts, "localaddr", host);
2337 qemu_opt_set(opts, "localport", port);
2339 return opts;
2341 if (strstart(filename, "unix:", &p)) {
2342 qemu_opt_set(opts, "backend", "socket");
2343 if (qemu_opts_do_parse(opts, p, "path") != 0)
2344 goto fail;
2345 return opts;
2347 if (strstart(filename, "/dev/parport", NULL) ||
2348 strstart(filename, "/dev/ppi", NULL)) {
2349 qemu_opt_set(opts, "backend", "parport");
2350 qemu_opt_set(opts, "path", filename);
2351 return opts;
2353 if (strstart(filename, "/dev/", NULL)) {
2354 qemu_opt_set(opts, "backend", "tty");
2355 qemu_opt_set(opts, "path", filename);
2356 return opts;
2359 fail:
2360 qemu_opts_del(opts);
2361 return NULL;
2364 static const struct {
2365 const char *name;
2366 int (*open)(QemuOpts *opts, CharDriverState **chr);
2367 } backend_table[] = {
2368 { .name = "null", .open = qemu_chr_open_null },
2369 { .name = "socket", .open = qemu_chr_open_socket },
2370 { .name = "udp", .open = qemu_chr_open_udp },
2371 { .name = "msmouse", .open = qemu_chr_open_msmouse },
2372 { .name = "vc", .open = text_console_init },
2373 #ifdef _WIN32
2374 { .name = "file", .open = qemu_chr_open_win_file_out },
2375 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2376 { .name = "console", .open = qemu_chr_open_win_con },
2377 { .name = "serial", .open = qemu_chr_open_win },
2378 #else
2379 { .name = "file", .open = qemu_chr_open_file_out },
2380 { .name = "pipe", .open = qemu_chr_open_pipe },
2381 { .name = "pty", .open = qemu_chr_open_pty },
2382 { .name = "stdio", .open = qemu_chr_open_stdio },
2383 #endif
2384 #ifdef CONFIG_BRLAPI
2385 { .name = "braille", .open = chr_baum_init },
2386 #endif
2387 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2388 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2389 || defined(__FreeBSD_kernel__)
2390 { .name = "tty", .open = qemu_chr_open_tty },
2391 #endif
2392 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2393 || defined(__FreeBSD_kernel__)
2394 { .name = "parport", .open = qemu_chr_open_pp },
2395 #endif
2396 #ifdef CONFIG_SPICE
2397 { .name = "spicevmc", .open = qemu_chr_open_spice },
2398 #endif
2401 CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
2402 void (*init)(struct CharDriverState *s))
2404 CharDriverState *chr;
2405 int i;
2406 int ret;
2408 if (qemu_opts_id(opts) == NULL) {
2409 fprintf(stderr, "chardev: no id specified\n");
2410 return NULL;
2413 if (qemu_opt_get(opts, "backend") == NULL) {
2414 fprintf(stderr, "chardev: \"%s\" missing backend\n",
2415 qemu_opts_id(opts));
2416 return NULL;
2418 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2419 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2420 break;
2422 if (i == ARRAY_SIZE(backend_table)) {
2423 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2424 qemu_opt_get(opts, "backend"));
2425 return NULL;
2428 ret = backend_table[i].open(opts, &chr);
2429 if (ret < 0) {
2430 fprintf(stderr, "chardev: opening backend \"%s\" failed: %s\n",
2431 qemu_opt_get(opts, "backend"), strerror(-ret));
2432 return NULL;
2435 if (!chr->filename)
2436 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2437 chr->init = init;
2438 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2440 chr->avail_connections = 1;
2441 chr->label = qemu_strdup(qemu_opts_id(opts));
2442 return chr;
2445 CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2447 const char *p;
2448 CharDriverState *chr;
2449 QemuOpts *opts;
2451 if (strstart(filename, "chardev:", &p)) {
2452 return qemu_chr_find(p);
2455 opts = qemu_chr_parse_compat(label, filename);
2456 if (!opts)
2457 return NULL;
2459 chr = qemu_chr_new_from_opts(opts, init);
2460 qemu_opts_del(opts);
2461 return chr;
2464 void qemu_chr_fe_open(struct CharDriverState *chr)
2466 chr->fe_opened++;
2468 if (chr->chr_guest_open) {
2469 chr->chr_guest_open(chr);
2473 void qemu_chr_fe_close(struct CharDriverState *chr)
2475 if (chr->chr_guest_close) {
2476 chr->chr_guest_close(chr);
2479 chr->fe_opened--;
2482 void qemu_chr_fe_delete(CharDriverState *chr)
2484 QTAILQ_REMOVE(&chardevs, chr, next);
2485 if (chr->chr_close)
2486 chr->chr_close(chr);
2487 qemu_free(chr->filename);
2488 qemu_free(chr->label);
2489 qemu_free(chr);
2492 static void qemu_chr_qlist_iter(QObject *obj, void *opaque)
2494 QDict *chr_dict;
2495 Monitor *mon = opaque;
2497 chr_dict = qobject_to_qdict(obj);
2498 monitor_printf(mon, "%s: filename=%s\n", qdict_get_str(chr_dict, "label"),
2499 qdict_get_str(chr_dict, "filename"));
2502 void qemu_chr_info_print(Monitor *mon, const QObject *ret_data)
2504 qlist_iter(qobject_to_qlist(ret_data), qemu_chr_qlist_iter, mon);
2507 void qemu_chr_info(Monitor *mon, QObject **ret_data)
2509 QList *chr_list;
2510 CharDriverState *chr;
2512 chr_list = qlist_new();
2514 QTAILQ_FOREACH(chr, &chardevs, next) {
2515 QObject *obj = qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2516 chr->label, chr->filename);
2517 qlist_append_obj(chr_list, obj);
2520 *ret_data = QOBJECT(chr_list);
2523 CharDriverState *qemu_chr_find(const char *name)
2525 CharDriverState *chr;
2527 QTAILQ_FOREACH(chr, &chardevs, next) {
2528 if (strcmp(chr->label, name) != 0)
2529 continue;
2530 return chr;
2532 return NULL;