fw_cfg: change cur_offset to 32 bits
[qemu.git] / qemu-char.c
blob5a81e8f5f357b130f3232c7b32226017adf47fdc
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 #include <arpa/inet.h>
55 #include <dirent.h>
56 #include <netdb.h>
57 #include <sys/select.h>
58 #ifdef CONFIG_BSD
59 #include <sys/stat.h>
60 #ifdef __FreeBSD__
61 #include <libutil.h>
62 #include <dev/ppbus/ppi.h>
63 #include <dev/ppbus/ppbconf.h>
64 #elif defined(__DragonFly__)
65 #include <libutil.h>
66 #include <dev/misc/ppi/ppi.h>
67 #include <bus/ppbus/ppbconf.h>
68 #else
69 #include <util.h>
70 #endif
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
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"
100 #define READ_BUF_LEN 4096
102 /***********************************************************/
103 /* character device */
105 static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
106 QTAILQ_HEAD_INITIALIZER(chardevs);
108 static void qemu_chr_event(CharDriverState *s, int event)
110 if (!s->chr_event)
111 return;
112 s->chr_event(s->handler_opaque, event);
115 static void qemu_chr_generic_open_bh(void *opaque)
117 CharDriverState *s = opaque;
118 qemu_chr_event(s, CHR_EVENT_OPENED);
119 qemu_bh_delete(s->bh);
120 s->bh = NULL;
123 void qemu_chr_generic_open(CharDriverState *s)
125 if (s->bh == NULL) {
126 s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s);
127 qemu_bh_schedule(s->bh);
131 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
133 return s->chr_write(s, buf, len);
136 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
138 if (!s->chr_ioctl)
139 return -ENOTSUP;
140 return s->chr_ioctl(s, cmd, arg);
143 int qemu_chr_can_read(CharDriverState *s)
145 if (!s->chr_can_read)
146 return 0;
147 return s->chr_can_read(s->handler_opaque);
150 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
152 s->chr_read(s->handler_opaque, buf, len);
155 int qemu_chr_get_msgfd(CharDriverState *s)
157 return s->get_msgfd ? s->get_msgfd(s) : -1;
160 void qemu_chr_accept_input(CharDriverState *s)
162 if (s->chr_accept_input)
163 s->chr_accept_input(s);
166 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
168 char buf[READ_BUF_LEN];
169 va_list ap;
170 va_start(ap, fmt);
171 vsnprintf(buf, sizeof(buf), fmt, ap);
172 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
173 va_end(ap);
176 void qemu_chr_send_event(CharDriverState *s, int event)
178 if (s->chr_send_event)
179 s->chr_send_event(s, event);
182 void qemu_chr_add_handlers(CharDriverState *s,
183 IOCanRWHandler *fd_can_read,
184 IOReadHandler *fd_read,
185 IOEventHandler *fd_event,
186 void *opaque)
188 s->chr_can_read = fd_can_read;
189 s->chr_read = fd_read;
190 s->chr_event = fd_event;
191 s->handler_opaque = opaque;
192 if (s->chr_update_read_handler)
193 s->chr_update_read_handler(s);
196 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
198 return len;
201 static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
203 CharDriverState *chr;
205 chr = qemu_mallocz(sizeof(CharDriverState));
206 chr->chr_write = null_chr_write;
207 return chr;
210 /* MUX driver for serial I/O splitting */
211 #define MAX_MUX 4
212 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
213 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
214 typedef struct {
215 IOCanRWHandler *chr_can_read[MAX_MUX];
216 IOReadHandler *chr_read[MAX_MUX];
217 IOEventHandler *chr_event[MAX_MUX];
218 void *ext_opaque[MAX_MUX];
219 CharDriverState *drv;
220 int focus;
221 int mux_cnt;
222 int term_got_escape;
223 int max_size;
224 /* Intermediate input buffer allows to catch escape sequences even if the
225 currently active device is not accepting any input - but only until it
226 is full as well. */
227 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
228 int prod[MAX_MUX];
229 int cons[MAX_MUX];
230 int timestamps;
231 int linestart;
232 int64_t timestamps_start;
233 } MuxDriver;
236 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
238 MuxDriver *d = chr->opaque;
239 int ret;
240 if (!d->timestamps) {
241 ret = d->drv->chr_write(d->drv, buf, len);
242 } else {
243 int i;
245 ret = 0;
246 for (i = 0; i < len; i++) {
247 if (d->linestart) {
248 char buf1[64];
249 int64_t ti;
250 int secs;
252 ti = qemu_get_clock(rt_clock);
253 if (d->timestamps_start == -1)
254 d->timestamps_start = ti;
255 ti -= d->timestamps_start;
256 secs = ti / 1000;
257 snprintf(buf1, sizeof(buf1),
258 "[%02d:%02d:%02d.%03d] ",
259 secs / 3600,
260 (secs / 60) % 60,
261 secs % 60,
262 (int)(ti % 1000));
263 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
264 d->linestart = 0;
266 ret += d->drv->chr_write(d->drv, buf+i, 1);
267 if (buf[i] == '\n') {
268 d->linestart = 1;
272 return ret;
275 static const char * const mux_help[] = {
276 "% h print this help\n\r",
277 "% x exit emulator\n\r",
278 "% s save disk data back to file (if -snapshot)\n\r",
279 "% t toggle console timestamps\n\r"
280 "% b send break (magic sysrq)\n\r",
281 "% c switch between console and monitor\n\r",
282 "% % sends %\n\r",
283 NULL
286 int term_escape_char = 0x01; /* ctrl-a is used for escape */
287 static void mux_print_help(CharDriverState *chr)
289 int i, j;
290 char ebuf[15] = "Escape-Char";
291 char cbuf[50] = "\n\r";
293 if (term_escape_char > 0 && term_escape_char < 26) {
294 snprintf(cbuf, sizeof(cbuf), "\n\r");
295 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
296 } else {
297 snprintf(cbuf, sizeof(cbuf),
298 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
299 term_escape_char);
301 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
302 for (i = 0; mux_help[i] != NULL; i++) {
303 for (j=0; mux_help[i][j] != '\0'; j++) {
304 if (mux_help[i][j] == '%')
305 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
306 else
307 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
312 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
314 if (d->chr_event[mux_nr])
315 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
318 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
320 if (d->term_got_escape) {
321 d->term_got_escape = 0;
322 if (ch == term_escape_char)
323 goto send_char;
324 switch(ch) {
325 case '?':
326 case 'h':
327 mux_print_help(chr);
328 break;
329 case 'x':
331 const char *term = "QEMU: Terminated\n\r";
332 chr->chr_write(chr,(uint8_t *)term,strlen(term));
333 exit(0);
334 break;
336 case 's':
338 DriveInfo *dinfo;
339 QTAILQ_FOREACH(dinfo, &drives, next) {
340 bdrv_commit(dinfo->bdrv);
343 break;
344 case 'b':
345 qemu_chr_event(chr, CHR_EVENT_BREAK);
346 break;
347 case 'c':
348 /* Switch to the next registered device */
349 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
350 d->focus++;
351 if (d->focus >= d->mux_cnt)
352 d->focus = 0;
353 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
354 break;
355 case 't':
356 d->timestamps = !d->timestamps;
357 d->timestamps_start = -1;
358 d->linestart = 0;
359 break;
361 } else if (ch == term_escape_char) {
362 d->term_got_escape = 1;
363 } else {
364 send_char:
365 return 1;
367 return 0;
370 static void mux_chr_accept_input(CharDriverState *chr)
372 MuxDriver *d = chr->opaque;
373 int m = d->focus;
375 while (d->prod[m] != d->cons[m] &&
376 d->chr_can_read[m] &&
377 d->chr_can_read[m](d->ext_opaque[m])) {
378 d->chr_read[m](d->ext_opaque[m],
379 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
383 static int mux_chr_can_read(void *opaque)
385 CharDriverState *chr = opaque;
386 MuxDriver *d = chr->opaque;
387 int m = d->focus;
389 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
390 return 1;
391 if (d->chr_can_read[m])
392 return d->chr_can_read[m](d->ext_opaque[m]);
393 return 0;
396 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
398 CharDriverState *chr = opaque;
399 MuxDriver *d = chr->opaque;
400 int m = d->focus;
401 int i;
403 mux_chr_accept_input (opaque);
405 for(i = 0; i < size; i++)
406 if (mux_proc_byte(chr, d, buf[i])) {
407 if (d->prod[m] == d->cons[m] &&
408 d->chr_can_read[m] &&
409 d->chr_can_read[m](d->ext_opaque[m]))
410 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
411 else
412 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
416 static void mux_chr_event(void *opaque, int event)
418 CharDriverState *chr = opaque;
419 MuxDriver *d = chr->opaque;
420 int i;
422 /* Send the event to all registered listeners */
423 for (i = 0; i < d->mux_cnt; i++)
424 mux_chr_send_event(d, i, event);
427 static void mux_chr_update_read_handler(CharDriverState *chr)
429 MuxDriver *d = chr->opaque;
431 if (d->mux_cnt >= MAX_MUX) {
432 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
433 return;
435 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
436 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
437 d->chr_read[d->mux_cnt] = chr->chr_read;
438 d->chr_event[d->mux_cnt] = chr->chr_event;
439 /* Fix up the real driver with mux routines */
440 if (d->mux_cnt == 0) {
441 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
442 mux_chr_event, chr);
444 if (d->focus != -1) {
445 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
447 d->focus = d->mux_cnt;
448 d->mux_cnt++;
449 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
452 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
454 CharDriverState *chr;
455 MuxDriver *d;
457 chr = qemu_mallocz(sizeof(CharDriverState));
458 d = qemu_mallocz(sizeof(MuxDriver));
460 chr->opaque = d;
461 d->drv = drv;
462 d->focus = -1;
463 chr->chr_write = mux_chr_write;
464 chr->chr_update_read_handler = mux_chr_update_read_handler;
465 chr->chr_accept_input = mux_chr_accept_input;
466 return chr;
470 #ifdef _WIN32
471 int send_all(int fd, const void *buf, int len1)
473 int ret, len;
475 len = len1;
476 while (len > 0) {
477 ret = send(fd, buf, len, 0);
478 if (ret < 0) {
479 errno = WSAGetLastError();
480 if (errno != WSAEWOULDBLOCK) {
481 return -1;
483 } else if (ret == 0) {
484 break;
485 } else {
486 buf += ret;
487 len -= ret;
490 return len1 - len;
493 #else
495 static int unix_write(int fd, const uint8_t *buf, int len1)
497 int ret, len;
499 len = len1;
500 while (len > 0) {
501 ret = write(fd, buf, len);
502 if (ret < 0) {
503 if (errno != EINTR && errno != EAGAIN)
504 return -1;
505 } else if (ret == 0) {
506 break;
507 } else {
508 buf += ret;
509 len -= ret;
512 return len1 - len;
515 int send_all(int fd, const void *buf, int len1)
517 return unix_write(fd, buf, len1);
519 #endif /* !_WIN32 */
521 #ifndef _WIN32
523 typedef struct {
524 int fd_in, fd_out;
525 int max_size;
526 } FDCharDriver;
528 #define STDIO_MAX_CLIENTS 1
529 static int stdio_nb_clients = 0;
531 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
533 FDCharDriver *s = chr->opaque;
534 return send_all(s->fd_out, buf, len);
537 static int fd_chr_read_poll(void *opaque)
539 CharDriverState *chr = opaque;
540 FDCharDriver *s = chr->opaque;
542 s->max_size = qemu_chr_can_read(chr);
543 return s->max_size;
546 static void fd_chr_read(void *opaque)
548 CharDriverState *chr = opaque;
549 FDCharDriver *s = chr->opaque;
550 int size, len;
551 uint8_t buf[READ_BUF_LEN];
553 len = sizeof(buf);
554 if (len > s->max_size)
555 len = s->max_size;
556 if (len == 0)
557 return;
558 size = read(s->fd_in, buf, len);
559 if (size == 0) {
560 /* FD has been closed. Remove it from the active list. */
561 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
562 qemu_chr_event(chr, CHR_EVENT_CLOSED);
563 return;
565 if (size > 0) {
566 qemu_chr_read(chr, buf, size);
570 static void fd_chr_update_read_handler(CharDriverState *chr)
572 FDCharDriver *s = chr->opaque;
574 if (s->fd_in >= 0) {
575 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
576 } else {
577 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
578 fd_chr_read, NULL, chr);
583 static void fd_chr_close(struct CharDriverState *chr)
585 FDCharDriver *s = chr->opaque;
587 if (s->fd_in >= 0) {
588 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
589 } else {
590 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
594 qemu_free(s);
595 qemu_chr_event(chr, CHR_EVENT_CLOSED);
598 /* open a character device to a unix fd */
599 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
601 CharDriverState *chr;
602 FDCharDriver *s;
604 chr = qemu_mallocz(sizeof(CharDriverState));
605 s = qemu_mallocz(sizeof(FDCharDriver));
606 s->fd_in = fd_in;
607 s->fd_out = fd_out;
608 chr->opaque = s;
609 chr->chr_write = fd_chr_write;
610 chr->chr_update_read_handler = fd_chr_update_read_handler;
611 chr->chr_close = fd_chr_close;
613 qemu_chr_generic_open(chr);
615 return chr;
618 static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
620 int fd_out;
622 TFR(fd_out = open(qemu_opt_get(opts, "path"),
623 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
624 if (fd_out < 0)
625 return NULL;
626 return qemu_chr_open_fd(-1, fd_out);
629 static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
631 int fd_in, fd_out;
632 char filename_in[256], filename_out[256];
633 const char *filename = qemu_opt_get(opts, "path");
635 if (filename == NULL) {
636 fprintf(stderr, "chardev: pipe: no filename given\n");
637 return NULL;
640 snprintf(filename_in, 256, "%s.in", filename);
641 snprintf(filename_out, 256, "%s.out", filename);
642 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
643 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
644 if (fd_in < 0 || fd_out < 0) {
645 if (fd_in >= 0)
646 close(fd_in);
647 if (fd_out >= 0)
648 close(fd_out);
649 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
650 if (fd_in < 0)
651 return NULL;
653 return qemu_chr_open_fd(fd_in, fd_out);
657 /* for STDIO, we handle the case where several clients use it
658 (nographic mode) */
660 #define TERM_FIFO_MAX_SIZE 1
662 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
663 static int term_fifo_size;
665 static int stdio_read_poll(void *opaque)
667 CharDriverState *chr = opaque;
669 /* try to flush the queue if needed */
670 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
671 qemu_chr_read(chr, term_fifo, 1);
672 term_fifo_size = 0;
674 /* see if we can absorb more chars */
675 if (term_fifo_size == 0)
676 return 1;
677 else
678 return 0;
681 static void stdio_read(void *opaque)
683 int size;
684 uint8_t buf[1];
685 CharDriverState *chr = opaque;
687 size = read(0, buf, 1);
688 if (size == 0) {
689 /* stdin has been closed. Remove it from the active list. */
690 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
691 qemu_chr_event(chr, CHR_EVENT_CLOSED);
692 return;
694 if (size > 0) {
695 if (qemu_chr_can_read(chr) > 0) {
696 qemu_chr_read(chr, buf, 1);
697 } else if (term_fifo_size == 0) {
698 term_fifo[term_fifo_size++] = buf[0];
703 /* init terminal so that we can grab keys */
704 static struct termios oldtty;
705 static int old_fd0_flags;
706 static int term_atexit_done;
708 static void term_exit(void)
710 tcsetattr (0, TCSANOW, &oldtty);
711 fcntl(0, F_SETFL, old_fd0_flags);
714 static void term_init(QemuOpts *opts)
716 struct termios tty;
718 tcgetattr (0, &tty);
719 oldtty = tty;
720 old_fd0_flags = fcntl(0, F_GETFL);
722 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
723 |INLCR|IGNCR|ICRNL|IXON);
724 tty.c_oflag |= OPOST;
725 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
726 /* if graphical mode, we allow Ctrl-C handling */
727 if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC))
728 tty.c_lflag &= ~ISIG;
729 tty.c_cflag &= ~(CSIZE|PARENB);
730 tty.c_cflag |= CS8;
731 tty.c_cc[VMIN] = 1;
732 tty.c_cc[VTIME] = 0;
734 tcsetattr (0, TCSANOW, &tty);
736 if (!term_atexit_done++)
737 atexit(term_exit);
739 fcntl(0, F_SETFL, O_NONBLOCK);
742 static void qemu_chr_close_stdio(struct CharDriverState *chr)
744 term_exit();
745 stdio_nb_clients--;
746 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
747 fd_chr_close(chr);
750 static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
752 CharDriverState *chr;
754 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
755 return NULL;
756 chr = qemu_chr_open_fd(0, 1);
757 chr->chr_close = qemu_chr_close_stdio;
758 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
759 stdio_nb_clients++;
760 term_init(opts);
762 return chr;
765 #ifdef __sun__
766 /* Once Solaris has openpty(), this is going to be removed. */
767 static int openpty(int *amaster, int *aslave, char *name,
768 struct termios *termp, struct winsize *winp)
770 const char *slave;
771 int mfd = -1, sfd = -1;
773 *amaster = *aslave = -1;
775 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
776 if (mfd < 0)
777 goto err;
779 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
780 goto err;
782 if ((slave = ptsname(mfd)) == NULL)
783 goto err;
785 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
786 goto err;
788 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
789 (termp != NULL && tcgetattr(sfd, termp) < 0))
790 goto err;
792 if (amaster)
793 *amaster = mfd;
794 if (aslave)
795 *aslave = sfd;
796 if (winp)
797 ioctl(sfd, TIOCSWINSZ, winp);
799 return 0;
801 err:
802 if (sfd != -1)
803 close(sfd);
804 close(mfd);
805 return -1;
808 static void cfmakeraw (struct termios *termios_p)
810 termios_p->c_iflag &=
811 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
812 termios_p->c_oflag &= ~OPOST;
813 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
814 termios_p->c_cflag &= ~(CSIZE|PARENB);
815 termios_p->c_cflag |= CS8;
817 termios_p->c_cc[VMIN] = 0;
818 termios_p->c_cc[VTIME] = 0;
820 #endif
822 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
823 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
825 typedef struct {
826 int fd;
827 int connected;
828 int polling;
829 int read_bytes;
830 QEMUTimer *timer;
831 } PtyCharDriver;
833 static void pty_chr_update_read_handler(CharDriverState *chr);
834 static void pty_chr_state(CharDriverState *chr, int connected);
836 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
838 PtyCharDriver *s = chr->opaque;
840 if (!s->connected) {
841 /* guest sends data, check for (re-)connect */
842 pty_chr_update_read_handler(chr);
843 return 0;
845 return send_all(s->fd, buf, len);
848 static int pty_chr_read_poll(void *opaque)
850 CharDriverState *chr = opaque;
851 PtyCharDriver *s = chr->opaque;
853 s->read_bytes = qemu_chr_can_read(chr);
854 return s->read_bytes;
857 static void pty_chr_read(void *opaque)
859 CharDriverState *chr = opaque;
860 PtyCharDriver *s = chr->opaque;
861 int size, len;
862 uint8_t buf[READ_BUF_LEN];
864 len = sizeof(buf);
865 if (len > s->read_bytes)
866 len = s->read_bytes;
867 if (len == 0)
868 return;
869 size = read(s->fd, buf, len);
870 if ((size == -1 && errno == EIO) ||
871 (size == 0)) {
872 pty_chr_state(chr, 0);
873 return;
875 if (size > 0) {
876 pty_chr_state(chr, 1);
877 qemu_chr_read(chr, buf, size);
881 static void pty_chr_update_read_handler(CharDriverState *chr)
883 PtyCharDriver *s = chr->opaque;
885 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
886 pty_chr_read, NULL, chr);
887 s->polling = 1;
889 * Short timeout here: just need wait long enougth that qemu makes
890 * it through the poll loop once. When reconnected we want a
891 * short timeout so we notice it almost instantly. Otherwise
892 * read() gives us -EIO instantly, making pty_chr_state() reset the
893 * timeout to the normal (much longer) poll interval before the
894 * timer triggers.
896 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
899 static void pty_chr_state(CharDriverState *chr, int connected)
901 PtyCharDriver *s = chr->opaque;
903 if (!connected) {
904 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
905 s->connected = 0;
906 s->polling = 0;
907 /* (re-)connect poll interval for idle guests: once per second.
908 * We check more frequently in case the guests sends data to
909 * the virtual device linked to our pty. */
910 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
911 } else {
912 if (!s->connected)
913 qemu_chr_generic_open(chr);
914 s->connected = 1;
918 static void pty_chr_timer(void *opaque)
920 struct CharDriverState *chr = opaque;
921 PtyCharDriver *s = chr->opaque;
923 if (s->connected)
924 return;
925 if (s->polling) {
926 /* If we arrive here without polling being cleared due
927 * read returning -EIO, then we are (re-)connected */
928 pty_chr_state(chr, 1);
929 return;
932 /* Next poll ... */
933 pty_chr_update_read_handler(chr);
936 static void pty_chr_close(struct CharDriverState *chr)
938 PtyCharDriver *s = chr->opaque;
940 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
941 close(s->fd);
942 qemu_del_timer(s->timer);
943 qemu_free_timer(s->timer);
944 qemu_free(s);
945 qemu_chr_event(chr, CHR_EVENT_CLOSED);
948 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
950 CharDriverState *chr;
951 PtyCharDriver *s;
952 struct termios tty;
953 int slave_fd, len;
954 #if defined(__OpenBSD__) || defined(__DragonFly__)
955 char pty_name[PATH_MAX];
956 #define q_ptsname(x) pty_name
957 #else
958 char *pty_name = NULL;
959 #define q_ptsname(x) ptsname(x)
960 #endif
962 chr = qemu_mallocz(sizeof(CharDriverState));
963 s = qemu_mallocz(sizeof(PtyCharDriver));
965 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
966 return NULL;
969 /* Set raw attributes on the pty. */
970 tcgetattr(slave_fd, &tty);
971 cfmakeraw(&tty);
972 tcsetattr(slave_fd, TCSAFLUSH, &tty);
973 close(slave_fd);
975 len = strlen(q_ptsname(s->fd)) + 5;
976 chr->filename = qemu_malloc(len);
977 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
978 qemu_opt_set(opts, "path", q_ptsname(s->fd));
979 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
981 chr->opaque = s;
982 chr->chr_write = pty_chr_write;
983 chr->chr_update_read_handler = pty_chr_update_read_handler;
984 chr->chr_close = pty_chr_close;
986 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
988 return chr;
991 static void tty_serial_init(int fd, int speed,
992 int parity, int data_bits, int stop_bits)
994 struct termios tty;
995 speed_t spd;
997 #if 0
998 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
999 speed, parity, data_bits, stop_bits);
1000 #endif
1001 tcgetattr (fd, &tty);
1003 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1004 speed = speed * 10 / 11;
1005 do {
1006 check_speed(50);
1007 check_speed(75);
1008 check_speed(110);
1009 check_speed(134);
1010 check_speed(150);
1011 check_speed(200);
1012 check_speed(300);
1013 check_speed(600);
1014 check_speed(1200);
1015 check_speed(1800);
1016 check_speed(2400);
1017 check_speed(4800);
1018 check_speed(9600);
1019 check_speed(19200);
1020 check_speed(38400);
1021 /* Non-Posix values follow. They may be unsupported on some systems. */
1022 check_speed(57600);
1023 check_speed(115200);
1024 #ifdef B230400
1025 check_speed(230400);
1026 #endif
1027 #ifdef B460800
1028 check_speed(460800);
1029 #endif
1030 #ifdef B500000
1031 check_speed(500000);
1032 #endif
1033 #ifdef B576000
1034 check_speed(576000);
1035 #endif
1036 #ifdef B921600
1037 check_speed(921600);
1038 #endif
1039 #ifdef B1000000
1040 check_speed(1000000);
1041 #endif
1042 #ifdef B1152000
1043 check_speed(1152000);
1044 #endif
1045 #ifdef B1500000
1046 check_speed(1500000);
1047 #endif
1048 #ifdef B2000000
1049 check_speed(2000000);
1050 #endif
1051 #ifdef B2500000
1052 check_speed(2500000);
1053 #endif
1054 #ifdef B3000000
1055 check_speed(3000000);
1056 #endif
1057 #ifdef B3500000
1058 check_speed(3500000);
1059 #endif
1060 #ifdef B4000000
1061 check_speed(4000000);
1062 #endif
1063 spd = B115200;
1064 } while (0);
1066 cfsetispeed(&tty, spd);
1067 cfsetospeed(&tty, spd);
1069 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1070 |INLCR|IGNCR|ICRNL|IXON);
1071 tty.c_oflag |= OPOST;
1072 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1073 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1074 switch(data_bits) {
1075 default:
1076 case 8:
1077 tty.c_cflag |= CS8;
1078 break;
1079 case 7:
1080 tty.c_cflag |= CS7;
1081 break;
1082 case 6:
1083 tty.c_cflag |= CS6;
1084 break;
1085 case 5:
1086 tty.c_cflag |= CS5;
1087 break;
1089 switch(parity) {
1090 default:
1091 case 'N':
1092 break;
1093 case 'E':
1094 tty.c_cflag |= PARENB;
1095 break;
1096 case 'O':
1097 tty.c_cflag |= PARENB | PARODD;
1098 break;
1100 if (stop_bits == 2)
1101 tty.c_cflag |= CSTOPB;
1103 tcsetattr (fd, TCSANOW, &tty);
1106 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1108 FDCharDriver *s = chr->opaque;
1110 switch(cmd) {
1111 case CHR_IOCTL_SERIAL_SET_PARAMS:
1113 QEMUSerialSetParams *ssp = arg;
1114 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1115 ssp->data_bits, ssp->stop_bits);
1117 break;
1118 case CHR_IOCTL_SERIAL_SET_BREAK:
1120 int enable = *(int *)arg;
1121 if (enable)
1122 tcsendbreak(s->fd_in, 1);
1124 break;
1125 case CHR_IOCTL_SERIAL_GET_TIOCM:
1127 int sarg = 0;
1128 int *targ = (int *)arg;
1129 ioctl(s->fd_in, TIOCMGET, &sarg);
1130 *targ = 0;
1131 if (sarg & TIOCM_CTS)
1132 *targ |= CHR_TIOCM_CTS;
1133 if (sarg & TIOCM_CAR)
1134 *targ |= CHR_TIOCM_CAR;
1135 if (sarg & TIOCM_DSR)
1136 *targ |= CHR_TIOCM_DSR;
1137 if (sarg & TIOCM_RI)
1138 *targ |= CHR_TIOCM_RI;
1139 if (sarg & TIOCM_DTR)
1140 *targ |= CHR_TIOCM_DTR;
1141 if (sarg & TIOCM_RTS)
1142 *targ |= CHR_TIOCM_RTS;
1144 break;
1145 case CHR_IOCTL_SERIAL_SET_TIOCM:
1147 int sarg = *(int *)arg;
1148 int targ = 0;
1149 ioctl(s->fd_in, TIOCMGET, &targ);
1150 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1151 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1152 if (sarg & CHR_TIOCM_CTS)
1153 targ |= TIOCM_CTS;
1154 if (sarg & CHR_TIOCM_CAR)
1155 targ |= TIOCM_CAR;
1156 if (sarg & CHR_TIOCM_DSR)
1157 targ |= TIOCM_DSR;
1158 if (sarg & CHR_TIOCM_RI)
1159 targ |= TIOCM_RI;
1160 if (sarg & CHR_TIOCM_DTR)
1161 targ |= TIOCM_DTR;
1162 if (sarg & CHR_TIOCM_RTS)
1163 targ |= TIOCM_RTS;
1164 ioctl(s->fd_in, TIOCMSET, &targ);
1166 break;
1167 default:
1168 return -ENOTSUP;
1170 return 0;
1173 static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
1175 const char *filename = qemu_opt_get(opts, "path");
1176 CharDriverState *chr;
1177 int fd;
1179 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1180 tty_serial_init(fd, 115200, 'N', 8, 1);
1181 chr = qemu_chr_open_fd(fd, fd);
1182 if (!chr) {
1183 close(fd);
1184 return NULL;
1186 chr->chr_ioctl = tty_serial_ioctl;
1187 qemu_chr_generic_open(chr);
1188 return chr;
1190 #else /* ! __linux__ && ! __sun__ */
1191 static CharDriverState *qemu_chr_open_pty(void)
1193 return NULL;
1195 #endif /* __linux__ || __sun__ */
1197 #if defined(__linux__)
1198 typedef struct {
1199 int fd;
1200 int mode;
1201 } ParallelCharDriver;
1203 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1205 if (s->mode != mode) {
1206 int m = mode;
1207 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1208 return 0;
1209 s->mode = mode;
1211 return 1;
1214 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1216 ParallelCharDriver *drv = chr->opaque;
1217 int fd = drv->fd;
1218 uint8_t b;
1220 switch(cmd) {
1221 case CHR_IOCTL_PP_READ_DATA:
1222 if (ioctl(fd, PPRDATA, &b) < 0)
1223 return -ENOTSUP;
1224 *(uint8_t *)arg = b;
1225 break;
1226 case CHR_IOCTL_PP_WRITE_DATA:
1227 b = *(uint8_t *)arg;
1228 if (ioctl(fd, PPWDATA, &b) < 0)
1229 return -ENOTSUP;
1230 break;
1231 case CHR_IOCTL_PP_READ_CONTROL:
1232 if (ioctl(fd, PPRCONTROL, &b) < 0)
1233 return -ENOTSUP;
1234 /* Linux gives only the lowest bits, and no way to know data
1235 direction! For better compatibility set the fixed upper
1236 bits. */
1237 *(uint8_t *)arg = b | 0xc0;
1238 break;
1239 case CHR_IOCTL_PP_WRITE_CONTROL:
1240 b = *(uint8_t *)arg;
1241 if (ioctl(fd, PPWCONTROL, &b) < 0)
1242 return -ENOTSUP;
1243 break;
1244 case CHR_IOCTL_PP_READ_STATUS:
1245 if (ioctl(fd, PPRSTATUS, &b) < 0)
1246 return -ENOTSUP;
1247 *(uint8_t *)arg = b;
1248 break;
1249 case CHR_IOCTL_PP_DATA_DIR:
1250 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1251 return -ENOTSUP;
1252 break;
1253 case CHR_IOCTL_PP_EPP_READ_ADDR:
1254 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1255 struct ParallelIOArg *parg = arg;
1256 int n = read(fd, parg->buffer, parg->count);
1257 if (n != parg->count) {
1258 return -EIO;
1261 break;
1262 case CHR_IOCTL_PP_EPP_READ:
1263 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1264 struct ParallelIOArg *parg = arg;
1265 int n = read(fd, parg->buffer, parg->count);
1266 if (n != parg->count) {
1267 return -EIO;
1270 break;
1271 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1272 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1273 struct ParallelIOArg *parg = arg;
1274 int n = write(fd, parg->buffer, parg->count);
1275 if (n != parg->count) {
1276 return -EIO;
1279 break;
1280 case CHR_IOCTL_PP_EPP_WRITE:
1281 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1282 struct ParallelIOArg *parg = arg;
1283 int n = write(fd, parg->buffer, parg->count);
1284 if (n != parg->count) {
1285 return -EIO;
1288 break;
1289 default:
1290 return -ENOTSUP;
1292 return 0;
1295 static void pp_close(CharDriverState *chr)
1297 ParallelCharDriver *drv = chr->opaque;
1298 int fd = drv->fd;
1300 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1301 ioctl(fd, PPRELEASE);
1302 close(fd);
1303 qemu_free(drv);
1304 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1307 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1309 const char *filename = qemu_opt_get(opts, "path");
1310 CharDriverState *chr;
1311 ParallelCharDriver *drv;
1312 int fd;
1314 TFR(fd = open(filename, O_RDWR));
1315 if (fd < 0)
1316 return NULL;
1318 if (ioctl(fd, PPCLAIM) < 0) {
1319 close(fd);
1320 return NULL;
1323 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1324 drv->fd = fd;
1325 drv->mode = IEEE1284_MODE_COMPAT;
1327 chr = qemu_mallocz(sizeof(CharDriverState));
1328 chr->chr_write = null_chr_write;
1329 chr->chr_ioctl = pp_ioctl;
1330 chr->chr_close = pp_close;
1331 chr->opaque = drv;
1333 qemu_chr_generic_open(chr);
1335 return chr;
1337 #endif /* __linux__ */
1339 #if defined(__FreeBSD__) || defined(__DragonFly__)
1340 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1342 int fd = (int)chr->opaque;
1343 uint8_t b;
1345 switch(cmd) {
1346 case CHR_IOCTL_PP_READ_DATA:
1347 if (ioctl(fd, PPIGDATA, &b) < 0)
1348 return -ENOTSUP;
1349 *(uint8_t *)arg = b;
1350 break;
1351 case CHR_IOCTL_PP_WRITE_DATA:
1352 b = *(uint8_t *)arg;
1353 if (ioctl(fd, PPISDATA, &b) < 0)
1354 return -ENOTSUP;
1355 break;
1356 case CHR_IOCTL_PP_READ_CONTROL:
1357 if (ioctl(fd, PPIGCTRL, &b) < 0)
1358 return -ENOTSUP;
1359 *(uint8_t *)arg = b;
1360 break;
1361 case CHR_IOCTL_PP_WRITE_CONTROL:
1362 b = *(uint8_t *)arg;
1363 if (ioctl(fd, PPISCTRL, &b) < 0)
1364 return -ENOTSUP;
1365 break;
1366 case CHR_IOCTL_PP_READ_STATUS:
1367 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1368 return -ENOTSUP;
1369 *(uint8_t *)arg = b;
1370 break;
1371 default:
1372 return -ENOTSUP;
1374 return 0;
1377 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1379 const char *filename = qemu_opt_get(opts, "path");
1380 CharDriverState *chr;
1381 int fd;
1383 fd = open(filename, O_RDWR);
1384 if (fd < 0)
1385 return NULL;
1387 chr = qemu_mallocz(sizeof(CharDriverState));
1388 chr->opaque = (void *)fd;
1389 chr->chr_write = null_chr_write;
1390 chr->chr_ioctl = pp_ioctl;
1391 return chr;
1393 #endif
1395 #else /* _WIN32 */
1397 typedef struct {
1398 int max_size;
1399 HANDLE hcom, hrecv, hsend;
1400 OVERLAPPED orecv, osend;
1401 BOOL fpipe;
1402 DWORD len;
1403 } WinCharState;
1405 #define NSENDBUF 2048
1406 #define NRECVBUF 2048
1407 #define MAXCONNECT 1
1408 #define NTIMEOUT 5000
1410 static int win_chr_poll(void *opaque);
1411 static int win_chr_pipe_poll(void *opaque);
1413 static void win_chr_close(CharDriverState *chr)
1415 WinCharState *s = chr->opaque;
1417 if (s->hsend) {
1418 CloseHandle(s->hsend);
1419 s->hsend = NULL;
1421 if (s->hrecv) {
1422 CloseHandle(s->hrecv);
1423 s->hrecv = NULL;
1425 if (s->hcom) {
1426 CloseHandle(s->hcom);
1427 s->hcom = NULL;
1429 if (s->fpipe)
1430 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1431 else
1432 qemu_del_polling_cb(win_chr_poll, chr);
1434 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1437 static int win_chr_init(CharDriverState *chr, const char *filename)
1439 WinCharState *s = chr->opaque;
1440 COMMCONFIG comcfg;
1441 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1442 COMSTAT comstat;
1443 DWORD size;
1444 DWORD err;
1446 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1447 if (!s->hsend) {
1448 fprintf(stderr, "Failed CreateEvent\n");
1449 goto fail;
1451 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1452 if (!s->hrecv) {
1453 fprintf(stderr, "Failed CreateEvent\n");
1454 goto fail;
1457 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1458 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1459 if (s->hcom == INVALID_HANDLE_VALUE) {
1460 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1461 s->hcom = NULL;
1462 goto fail;
1465 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1466 fprintf(stderr, "Failed SetupComm\n");
1467 goto fail;
1470 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1471 size = sizeof(COMMCONFIG);
1472 GetDefaultCommConfig(filename, &comcfg, &size);
1473 comcfg.dcb.DCBlength = sizeof(DCB);
1474 CommConfigDialog(filename, NULL, &comcfg);
1476 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1477 fprintf(stderr, "Failed SetCommState\n");
1478 goto fail;
1481 if (!SetCommMask(s->hcom, EV_ERR)) {
1482 fprintf(stderr, "Failed SetCommMask\n");
1483 goto fail;
1486 cto.ReadIntervalTimeout = MAXDWORD;
1487 if (!SetCommTimeouts(s->hcom, &cto)) {
1488 fprintf(stderr, "Failed SetCommTimeouts\n");
1489 goto fail;
1492 if (!ClearCommError(s->hcom, &err, &comstat)) {
1493 fprintf(stderr, "Failed ClearCommError\n");
1494 goto fail;
1496 qemu_add_polling_cb(win_chr_poll, chr);
1497 return 0;
1499 fail:
1500 win_chr_close(chr);
1501 return -1;
1504 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1506 WinCharState *s = chr->opaque;
1507 DWORD len, ret, size, err;
1509 len = len1;
1510 ZeroMemory(&s->osend, sizeof(s->osend));
1511 s->osend.hEvent = s->hsend;
1512 while (len > 0) {
1513 if (s->hsend)
1514 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1515 else
1516 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1517 if (!ret) {
1518 err = GetLastError();
1519 if (err == ERROR_IO_PENDING) {
1520 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1521 if (ret) {
1522 buf += size;
1523 len -= size;
1524 } else {
1525 break;
1527 } else {
1528 break;
1530 } else {
1531 buf += size;
1532 len -= size;
1535 return len1 - len;
1538 static int win_chr_read_poll(CharDriverState *chr)
1540 WinCharState *s = chr->opaque;
1542 s->max_size = qemu_chr_can_read(chr);
1543 return s->max_size;
1546 static void win_chr_readfile(CharDriverState *chr)
1548 WinCharState *s = chr->opaque;
1549 int ret, err;
1550 uint8_t buf[READ_BUF_LEN];
1551 DWORD size;
1553 ZeroMemory(&s->orecv, sizeof(s->orecv));
1554 s->orecv.hEvent = s->hrecv;
1555 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1556 if (!ret) {
1557 err = GetLastError();
1558 if (err == ERROR_IO_PENDING) {
1559 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1563 if (size > 0) {
1564 qemu_chr_read(chr, buf, size);
1568 static void win_chr_read(CharDriverState *chr)
1570 WinCharState *s = chr->opaque;
1572 if (s->len > s->max_size)
1573 s->len = s->max_size;
1574 if (s->len == 0)
1575 return;
1577 win_chr_readfile(chr);
1580 static int win_chr_poll(void *opaque)
1582 CharDriverState *chr = opaque;
1583 WinCharState *s = chr->opaque;
1584 COMSTAT status;
1585 DWORD comerr;
1587 ClearCommError(s->hcom, &comerr, &status);
1588 if (status.cbInQue > 0) {
1589 s->len = status.cbInQue;
1590 win_chr_read_poll(chr);
1591 win_chr_read(chr);
1592 return 1;
1594 return 0;
1597 static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1599 const char *filename = qemu_opt_get(opts, "path");
1600 CharDriverState *chr;
1601 WinCharState *s;
1603 chr = qemu_mallocz(sizeof(CharDriverState));
1604 s = qemu_mallocz(sizeof(WinCharState));
1605 chr->opaque = s;
1606 chr->chr_write = win_chr_write;
1607 chr->chr_close = win_chr_close;
1609 if (win_chr_init(chr, filename) < 0) {
1610 free(s);
1611 free(chr);
1612 return NULL;
1614 qemu_chr_generic_open(chr);
1615 return chr;
1618 static int win_chr_pipe_poll(void *opaque)
1620 CharDriverState *chr = opaque;
1621 WinCharState *s = chr->opaque;
1622 DWORD size;
1624 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1625 if (size > 0) {
1626 s->len = size;
1627 win_chr_read_poll(chr);
1628 win_chr_read(chr);
1629 return 1;
1631 return 0;
1634 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1636 WinCharState *s = chr->opaque;
1637 OVERLAPPED ov;
1638 int ret;
1639 DWORD size;
1640 char openname[256];
1642 s->fpipe = TRUE;
1644 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1645 if (!s->hsend) {
1646 fprintf(stderr, "Failed CreateEvent\n");
1647 goto fail;
1649 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1650 if (!s->hrecv) {
1651 fprintf(stderr, "Failed CreateEvent\n");
1652 goto fail;
1655 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1656 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1657 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1658 PIPE_WAIT,
1659 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1660 if (s->hcom == INVALID_HANDLE_VALUE) {
1661 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1662 s->hcom = NULL;
1663 goto fail;
1666 ZeroMemory(&ov, sizeof(ov));
1667 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1668 ret = ConnectNamedPipe(s->hcom, &ov);
1669 if (ret) {
1670 fprintf(stderr, "Failed ConnectNamedPipe\n");
1671 goto fail;
1674 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1675 if (!ret) {
1676 fprintf(stderr, "Failed GetOverlappedResult\n");
1677 if (ov.hEvent) {
1678 CloseHandle(ov.hEvent);
1679 ov.hEvent = NULL;
1681 goto fail;
1684 if (ov.hEvent) {
1685 CloseHandle(ov.hEvent);
1686 ov.hEvent = NULL;
1688 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1689 return 0;
1691 fail:
1692 win_chr_close(chr);
1693 return -1;
1697 static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
1699 const char *filename = qemu_opt_get(opts, "path");
1700 CharDriverState *chr;
1701 WinCharState *s;
1703 chr = qemu_mallocz(sizeof(CharDriverState));
1704 s = qemu_mallocz(sizeof(WinCharState));
1705 chr->opaque = s;
1706 chr->chr_write = win_chr_write;
1707 chr->chr_close = win_chr_close;
1709 if (win_chr_pipe_init(chr, filename) < 0) {
1710 free(s);
1711 free(chr);
1712 return NULL;
1714 qemu_chr_generic_open(chr);
1715 return chr;
1718 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1720 CharDriverState *chr;
1721 WinCharState *s;
1723 chr = qemu_mallocz(sizeof(CharDriverState));
1724 s = qemu_mallocz(sizeof(WinCharState));
1725 s->hcom = fd_out;
1726 chr->opaque = s;
1727 chr->chr_write = win_chr_write;
1728 qemu_chr_generic_open(chr);
1729 return chr;
1732 static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
1734 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1737 static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
1739 const char *file_out = qemu_opt_get(opts, "path");
1740 HANDLE fd_out;
1742 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1743 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1744 if (fd_out == INVALID_HANDLE_VALUE)
1745 return NULL;
1747 return qemu_chr_open_win_file(fd_out);
1749 #endif /* !_WIN32 */
1751 /***********************************************************/
1752 /* UDP Net console */
1754 typedef struct {
1755 int fd;
1756 uint8_t buf[READ_BUF_LEN];
1757 int bufcnt;
1758 int bufptr;
1759 int max_size;
1760 } NetCharDriver;
1762 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1764 NetCharDriver *s = chr->opaque;
1766 return send(s->fd, (const void *)buf, len, 0);
1769 static int udp_chr_read_poll(void *opaque)
1771 CharDriverState *chr = opaque;
1772 NetCharDriver *s = chr->opaque;
1774 s->max_size = qemu_chr_can_read(chr);
1776 /* If there were any stray characters in the queue process them
1777 * first
1779 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1780 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1781 s->bufptr++;
1782 s->max_size = qemu_chr_can_read(chr);
1784 return s->max_size;
1787 static void udp_chr_read(void *opaque)
1789 CharDriverState *chr = opaque;
1790 NetCharDriver *s = chr->opaque;
1792 if (s->max_size == 0)
1793 return;
1794 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1795 s->bufptr = s->bufcnt;
1796 if (s->bufcnt <= 0)
1797 return;
1799 s->bufptr = 0;
1800 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1801 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1802 s->bufptr++;
1803 s->max_size = qemu_chr_can_read(chr);
1807 static void udp_chr_update_read_handler(CharDriverState *chr)
1809 NetCharDriver *s = chr->opaque;
1811 if (s->fd >= 0) {
1812 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1813 udp_chr_read, NULL, chr);
1817 static void udp_chr_close(CharDriverState *chr)
1819 NetCharDriver *s = chr->opaque;
1820 if (s->fd >= 0) {
1821 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1822 closesocket(s->fd);
1824 qemu_free(s);
1825 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1828 static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
1830 CharDriverState *chr = NULL;
1831 NetCharDriver *s = NULL;
1832 int fd = -1;
1834 chr = qemu_mallocz(sizeof(CharDriverState));
1835 s = qemu_mallocz(sizeof(NetCharDriver));
1837 fd = inet_dgram_opts(opts);
1838 if (fd < 0) {
1839 fprintf(stderr, "inet_dgram_opts failed\n");
1840 goto return_err;
1843 s->fd = fd;
1844 s->bufcnt = 0;
1845 s->bufptr = 0;
1846 chr->opaque = s;
1847 chr->chr_write = udp_chr_write;
1848 chr->chr_update_read_handler = udp_chr_update_read_handler;
1849 chr->chr_close = udp_chr_close;
1850 return chr;
1852 return_err:
1853 if (chr)
1854 free(chr);
1855 if (s)
1856 free(s);
1857 if (fd >= 0)
1858 closesocket(fd);
1859 return NULL;
1862 /***********************************************************/
1863 /* TCP Net console */
1865 typedef struct {
1866 int fd, listen_fd;
1867 int connected;
1868 int max_size;
1869 int do_telnetopt;
1870 int do_nodelay;
1871 int is_unix;
1872 int msgfd;
1873 } TCPCharDriver;
1875 static void tcp_chr_accept(void *opaque);
1877 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1879 TCPCharDriver *s = chr->opaque;
1880 if (s->connected) {
1881 return send_all(s->fd, buf, len);
1882 } else {
1883 /* XXX: indicate an error ? */
1884 return len;
1888 static int tcp_chr_read_poll(void *opaque)
1890 CharDriverState *chr = opaque;
1891 TCPCharDriver *s = chr->opaque;
1892 if (!s->connected)
1893 return 0;
1894 s->max_size = qemu_chr_can_read(chr);
1895 return s->max_size;
1898 #define IAC 255
1899 #define IAC_BREAK 243
1900 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1901 TCPCharDriver *s,
1902 uint8_t *buf, int *size)
1904 /* Handle any telnet client's basic IAC options to satisfy char by
1905 * char mode with no echo. All IAC options will be removed from
1906 * the buf and the do_telnetopt variable will be used to track the
1907 * state of the width of the IAC information.
1909 * IAC commands come in sets of 3 bytes with the exception of the
1910 * "IAC BREAK" command and the double IAC.
1913 int i;
1914 int j = 0;
1916 for (i = 0; i < *size; i++) {
1917 if (s->do_telnetopt > 1) {
1918 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1919 /* Double IAC means send an IAC */
1920 if (j != i)
1921 buf[j] = buf[i];
1922 j++;
1923 s->do_telnetopt = 1;
1924 } else {
1925 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1926 /* Handle IAC break commands by sending a serial break */
1927 qemu_chr_event(chr, CHR_EVENT_BREAK);
1928 s->do_telnetopt++;
1930 s->do_telnetopt++;
1932 if (s->do_telnetopt >= 4) {
1933 s->do_telnetopt = 1;
1935 } else {
1936 if ((unsigned char)buf[i] == IAC) {
1937 s->do_telnetopt = 2;
1938 } else {
1939 if (j != i)
1940 buf[j] = buf[i];
1941 j++;
1945 *size = j;
1948 static int tcp_get_msgfd(CharDriverState *chr)
1950 TCPCharDriver *s = chr->opaque;
1952 return s->msgfd;
1955 #ifndef _WIN32
1956 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
1958 TCPCharDriver *s = chr->opaque;
1959 struct cmsghdr *cmsg;
1961 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
1962 int fd;
1964 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
1965 cmsg->cmsg_level != SOL_SOCKET ||
1966 cmsg->cmsg_type != SCM_RIGHTS)
1967 continue;
1969 fd = *((int *)CMSG_DATA(cmsg));
1970 if (fd < 0)
1971 continue;
1973 if (s->msgfd != -1)
1974 close(s->msgfd);
1975 s->msgfd = fd;
1979 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
1981 TCPCharDriver *s = chr->opaque;
1982 struct msghdr msg = { NULL, };
1983 struct iovec iov[1];
1984 union {
1985 struct cmsghdr cmsg;
1986 char control[CMSG_SPACE(sizeof(int))];
1987 } msg_control;
1988 ssize_t ret;
1990 iov[0].iov_base = buf;
1991 iov[0].iov_len = len;
1993 msg.msg_iov = iov;
1994 msg.msg_iovlen = 1;
1995 msg.msg_control = &msg_control;
1996 msg.msg_controllen = sizeof(msg_control);
1998 ret = recvmsg(s->fd, &msg, 0);
1999 if (ret > 0 && s->is_unix)
2000 unix_process_msgfd(chr, &msg);
2002 return ret;
2004 #else
2005 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2007 TCPCharDriver *s = chr->opaque;
2008 return recv(s->fd, buf, len, 0);
2010 #endif
2012 static void tcp_chr_read(void *opaque)
2014 CharDriverState *chr = opaque;
2015 TCPCharDriver *s = chr->opaque;
2016 uint8_t buf[READ_BUF_LEN];
2017 int len, size;
2019 if (!s->connected || s->max_size <= 0)
2020 return;
2021 len = sizeof(buf);
2022 if (len > s->max_size)
2023 len = s->max_size;
2024 size = tcp_chr_recv(chr, (void *)buf, len);
2025 if (size == 0) {
2026 /* connection closed */
2027 s->connected = 0;
2028 if (s->listen_fd >= 0) {
2029 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2031 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2032 closesocket(s->fd);
2033 s->fd = -1;
2034 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2035 } else if (size > 0) {
2036 if (s->do_telnetopt)
2037 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2038 if (size > 0)
2039 qemu_chr_read(chr, buf, size);
2040 if (s->msgfd != -1) {
2041 close(s->msgfd);
2042 s->msgfd = -1;
2047 static void tcp_chr_connect(void *opaque)
2049 CharDriverState *chr = opaque;
2050 TCPCharDriver *s = chr->opaque;
2052 s->connected = 1;
2053 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2054 tcp_chr_read, NULL, chr);
2055 qemu_chr_generic_open(chr);
2058 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2059 static void tcp_chr_telnet_init(int fd)
2061 char buf[3];
2062 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2063 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2064 send(fd, (char *)buf, 3, 0);
2065 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2066 send(fd, (char *)buf, 3, 0);
2067 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2068 send(fd, (char *)buf, 3, 0);
2069 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2070 send(fd, (char *)buf, 3, 0);
2073 static void socket_set_nodelay(int fd)
2075 int val = 1;
2076 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2079 static void tcp_chr_accept(void *opaque)
2081 CharDriverState *chr = opaque;
2082 TCPCharDriver *s = chr->opaque;
2083 struct sockaddr_in saddr;
2084 #ifndef _WIN32
2085 struct sockaddr_un uaddr;
2086 #endif
2087 struct sockaddr *addr;
2088 socklen_t len;
2089 int fd;
2091 for(;;) {
2092 #ifndef _WIN32
2093 if (s->is_unix) {
2094 len = sizeof(uaddr);
2095 addr = (struct sockaddr *)&uaddr;
2096 } else
2097 #endif
2099 len = sizeof(saddr);
2100 addr = (struct sockaddr *)&saddr;
2102 fd = accept(s->listen_fd, addr, &len);
2103 if (fd < 0 && errno != EINTR) {
2104 return;
2105 } else if (fd >= 0) {
2106 if (s->do_telnetopt)
2107 tcp_chr_telnet_init(fd);
2108 break;
2111 socket_set_nonblock(fd);
2112 if (s->do_nodelay)
2113 socket_set_nodelay(fd);
2114 s->fd = fd;
2115 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2116 tcp_chr_connect(chr);
2119 static void tcp_chr_close(CharDriverState *chr)
2121 TCPCharDriver *s = chr->opaque;
2122 if (s->fd >= 0) {
2123 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2124 closesocket(s->fd);
2126 if (s->listen_fd >= 0) {
2127 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2128 closesocket(s->listen_fd);
2130 qemu_free(s);
2131 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2134 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2136 CharDriverState *chr = NULL;
2137 TCPCharDriver *s = NULL;
2138 int fd = -1;
2139 int is_listen;
2140 int is_waitconnect;
2141 int do_nodelay;
2142 int is_unix;
2143 int is_telnet;
2145 is_listen = qemu_opt_get_bool(opts, "server", 0);
2146 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2147 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2148 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2149 is_unix = qemu_opt_get(opts, "path") != NULL;
2150 if (!is_listen)
2151 is_waitconnect = 0;
2153 chr = qemu_mallocz(sizeof(CharDriverState));
2154 s = qemu_mallocz(sizeof(TCPCharDriver));
2156 if (is_unix) {
2157 if (is_listen) {
2158 fd = unix_listen_opts(opts);
2159 } else {
2160 fd = unix_connect_opts(opts);
2162 } else {
2163 if (is_listen) {
2164 fd = inet_listen_opts(opts, 0);
2165 } else {
2166 fd = inet_connect_opts(opts);
2169 if (fd < 0)
2170 goto fail;
2172 if (!is_waitconnect)
2173 socket_set_nonblock(fd);
2175 s->connected = 0;
2176 s->fd = -1;
2177 s->listen_fd = -1;
2178 s->msgfd = -1;
2179 s->is_unix = is_unix;
2180 s->do_nodelay = do_nodelay && !is_unix;
2182 chr->opaque = s;
2183 chr->chr_write = tcp_chr_write;
2184 chr->chr_close = tcp_chr_close;
2185 chr->get_msgfd = tcp_get_msgfd;
2187 if (is_listen) {
2188 s->listen_fd = fd;
2189 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2190 if (is_telnet)
2191 s->do_telnetopt = 1;
2193 } else {
2194 s->connected = 1;
2195 s->fd = fd;
2196 socket_set_nodelay(fd);
2197 tcp_chr_connect(chr);
2200 /* for "info chardev" monitor command */
2201 chr->filename = qemu_malloc(256);
2202 if (is_unix) {
2203 snprintf(chr->filename, 256, "unix:%s%s",
2204 qemu_opt_get(opts, "path"),
2205 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2206 } else if (is_telnet) {
2207 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2208 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2209 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2210 } else {
2211 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2212 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2213 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2216 if (is_listen && is_waitconnect) {
2217 printf("QEMU waiting for connection on: %s\n",
2218 chr->filename);
2219 tcp_chr_accept(chr);
2220 socket_set_nonblock(s->listen_fd);
2222 return chr;
2224 fail:
2225 if (fd >= 0)
2226 closesocket(fd);
2227 qemu_free(s);
2228 qemu_free(chr);
2229 return NULL;
2232 static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2234 char host[65], port[33], width[8], height[8];
2235 int pos;
2236 const char *p;
2237 QemuOpts *opts;
2239 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2240 if (NULL == opts)
2241 return NULL;
2243 if (strstart(filename, "mon:", &p)) {
2244 filename = p;
2245 qemu_opt_set(opts, "mux", "on");
2248 if (strcmp(filename, "null") == 0 ||
2249 strcmp(filename, "pty") == 0 ||
2250 strcmp(filename, "msmouse") == 0 ||
2251 strcmp(filename, "braille") == 0 ||
2252 strcmp(filename, "stdio") == 0) {
2253 qemu_opt_set(opts, "backend", filename);
2254 return opts;
2256 if (strstart(filename, "vc", &p)) {
2257 qemu_opt_set(opts, "backend", "vc");
2258 if (*p == ':') {
2259 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2260 /* pixels */
2261 qemu_opt_set(opts, "width", width);
2262 qemu_opt_set(opts, "height", height);
2263 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2264 /* chars */
2265 qemu_opt_set(opts, "cols", width);
2266 qemu_opt_set(opts, "rows", height);
2267 } else {
2268 goto fail;
2271 return opts;
2273 if (strcmp(filename, "con:") == 0) {
2274 qemu_opt_set(opts, "backend", "console");
2275 return opts;
2277 if (strstart(filename, "COM", NULL)) {
2278 qemu_opt_set(opts, "backend", "serial");
2279 qemu_opt_set(opts, "path", filename);
2280 return opts;
2282 if (strstart(filename, "file:", &p)) {
2283 qemu_opt_set(opts, "backend", "file");
2284 qemu_opt_set(opts, "path", p);
2285 return opts;
2287 if (strstart(filename, "pipe:", &p)) {
2288 qemu_opt_set(opts, "backend", "pipe");
2289 qemu_opt_set(opts, "path", p);
2290 return opts;
2292 if (strstart(filename, "tcp:", &p) ||
2293 strstart(filename, "telnet:", &p)) {
2294 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2295 host[0] = 0;
2296 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2297 goto fail;
2299 qemu_opt_set(opts, "backend", "socket");
2300 qemu_opt_set(opts, "host", host);
2301 qemu_opt_set(opts, "port", port);
2302 if (p[pos] == ',') {
2303 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2304 goto fail;
2306 if (strstart(filename, "telnet:", &p))
2307 qemu_opt_set(opts, "telnet", "on");
2308 return opts;
2310 if (strstart(filename, "udp:", &p)) {
2311 qemu_opt_set(opts, "backend", "udp");
2312 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2313 host[0] = 0;
2314 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2315 fprintf(stderr, "udp #1\n");
2316 goto fail;
2319 qemu_opt_set(opts, "host", host);
2320 qemu_opt_set(opts, "port", port);
2321 if (p[pos] == '@') {
2322 p += pos + 1;
2323 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2324 host[0] = 0;
2325 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2326 fprintf(stderr, "udp #2\n");
2327 goto fail;
2330 qemu_opt_set(opts, "localaddr", host);
2331 qemu_opt_set(opts, "localport", port);
2333 return opts;
2335 if (strstart(filename, "unix:", &p)) {
2336 qemu_opt_set(opts, "backend", "socket");
2337 if (qemu_opts_do_parse(opts, p, "path") != 0)
2338 goto fail;
2339 return opts;
2341 if (strstart(filename, "/dev/parport", NULL) ||
2342 strstart(filename, "/dev/ppi", NULL)) {
2343 qemu_opt_set(opts, "backend", "parport");
2344 qemu_opt_set(opts, "path", filename);
2345 return opts;
2347 if (strstart(filename, "/dev/", NULL)) {
2348 qemu_opt_set(opts, "backend", "tty");
2349 qemu_opt_set(opts, "path", filename);
2350 return opts;
2353 fail:
2354 fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
2355 qemu_opts_del(opts);
2356 return NULL;
2359 static const struct {
2360 const char *name;
2361 CharDriverState *(*open)(QemuOpts *opts);
2362 } backend_table[] = {
2363 { .name = "null", .open = qemu_chr_open_null },
2364 { .name = "socket", .open = qemu_chr_open_socket },
2365 { .name = "udp", .open = qemu_chr_open_udp },
2366 { .name = "msmouse", .open = qemu_chr_open_msmouse },
2367 { .name = "vc", .open = text_console_init },
2368 #ifdef _WIN32
2369 { .name = "file", .open = qemu_chr_open_win_file_out },
2370 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2371 { .name = "console", .open = qemu_chr_open_win_con },
2372 { .name = "serial", .open = qemu_chr_open_win },
2373 #else
2374 { .name = "file", .open = qemu_chr_open_file_out },
2375 { .name = "pipe", .open = qemu_chr_open_pipe },
2376 { .name = "pty", .open = qemu_chr_open_pty },
2377 { .name = "stdio", .open = qemu_chr_open_stdio },
2378 #endif
2379 #ifdef CONFIG_BRLAPI
2380 { .name = "braille", .open = chr_baum_init },
2381 #endif
2382 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2383 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
2384 { .name = "tty", .open = qemu_chr_open_tty },
2385 #endif
2386 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
2387 { .name = "parport", .open = qemu_chr_open_pp },
2388 #endif
2391 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2392 void (*init)(struct CharDriverState *s))
2394 CharDriverState *chr;
2395 int i;
2397 if (qemu_opts_id(opts) == NULL) {
2398 fprintf(stderr, "chardev: no id specified\n");
2399 return NULL;
2402 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2403 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2404 break;
2406 if (i == ARRAY_SIZE(backend_table)) {
2407 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2408 qemu_opt_get(opts, "backend"));
2409 return NULL;
2412 chr = backend_table[i].open(opts);
2413 if (!chr) {
2414 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2415 qemu_opt_get(opts, "backend"));
2416 return NULL;
2419 if (!chr->filename)
2420 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2421 chr->init = init;
2422 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2424 if (qemu_opt_get_bool(opts, "mux", 0)) {
2425 CharDriverState *base = chr;
2426 int len = strlen(qemu_opts_id(opts)) + 6;
2427 base->label = qemu_malloc(len);
2428 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2429 chr = qemu_chr_open_mux(base);
2430 chr->filename = base->filename;
2431 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2433 chr->label = qemu_strdup(qemu_opts_id(opts));
2434 return chr;
2437 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2439 const char *p;
2440 CharDriverState *chr;
2441 QemuOpts *opts;
2443 if (strstart(filename, "chardev:", &p)) {
2444 return qemu_chr_find(p);
2447 opts = qemu_chr_parse_compat(label, filename);
2448 if (!opts)
2449 return NULL;
2451 chr = qemu_chr_open_opts(opts, init);
2452 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2453 monitor_init(chr, MONITOR_USE_READLINE);
2455 return chr;
2458 void qemu_chr_close(CharDriverState *chr)
2460 QTAILQ_REMOVE(&chardevs, chr, next);
2461 if (chr->chr_close)
2462 chr->chr_close(chr);
2463 qemu_free(chr->filename);
2464 qemu_free(chr->label);
2465 qemu_free(chr);
2468 void qemu_chr_info(Monitor *mon)
2470 CharDriverState *chr;
2472 QTAILQ_FOREACH(chr, &chardevs, next) {
2473 monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
2477 CharDriverState *qemu_chr_find(const char *name)
2479 CharDriverState *chr;
2481 QTAILQ_FOREACH(chr, &chardevs, next) {
2482 if (strcmp(chr->label, name) != 0)
2483 continue;
2484 return chr;
2486 return NULL;