Commit 6f97dba008ec434b742764596d9e28df673cf6c2

Authored by aliguori
1 parent 0e82f34d

Move CharDriverState code out of vl.c

The motivating goal behind this is to allow other tools to use the CharDriver
code.  This patch is pure code motion except for the Makefile changes and the
copyright/header in qemu-char.c.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>



git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5580 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 3 changed files with 2206 additions and 2116 deletions
Makefile.target
... ... @@ -579,7 +579,7 @@ endif #CONFIG_BSD_USER
579 579 ifndef CONFIG_USER_ONLY
580 580  
581 581 OBJS=vl.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o net-checksum.o
582   -OBJS+=fw_cfg.o aio.o buffered_file.o migration.o migration-tcp.o
  582 +OBJS+=fw_cfg.o aio.o buffered_file.o migration.o migration-tcp.o qemu-char.o
583 583 ifdef CONFIG_WIN32
584 584 OBJS+=block-raw-win32.o
585 585 else
... ...
qemu-char.c 0 → 100644
  1 +/*
  2 + * QEMU System Emulator
  3 + *
  4 + * Copyright (c) 2003-2008 Fabrice Bellard
  5 + *
  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:
  12 + *
  13 + * The above copyright notice and this permission notice shall be included in
  14 + * all copies or substantial portions of the Software.
  15 + *
  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.
  23 + */
  24 +#include "qemu-common.h"
  25 +#include "net.h"
  26 +#include "console.h"
  27 +#include "sysemu.h"
  28 +#include "qemu-timer.h"
  29 +#include "qemu-char.h"
  30 +#include "block.h"
  31 +
  32 +#include <unistd.h>
  33 +#include <fcntl.h>
  34 +#include <signal.h>
  35 +#include <time.h>
  36 +#include <errno.h>
  37 +#include <sys/time.h>
  38 +#include <zlib.h>
  39 +
  40 +#ifndef _WIN32
  41 +#include <sys/times.h>
  42 +#include <sys/wait.h>
  43 +#include <termios.h>
  44 +#include <sys/mman.h>
  45 +#include <sys/ioctl.h>
  46 +#include <sys/socket.h>
  47 +#include <netinet/in.h>
  48 +#include <dirent.h>
  49 +#include <netdb.h>
  50 +#include <sys/select.h>
  51 +#include <arpa/inet.h>
  52 +#ifdef _BSD
  53 +#include <sys/stat.h>
  54 +#if !defined(__APPLE__) && !defined(__OpenBSD__)
  55 +#include <libutil.h>
  56 +#endif
  57 +#ifdef __OpenBSD__
  58 +#include <net/if.h>
  59 +#endif
  60 +#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
  61 +#include <freebsd/stdlib.h>
  62 +#else
  63 +#ifdef __linux__
  64 +#include <linux/if.h>
  65 +#include <pty.h>
  66 +
  67 +#include <linux/ppdev.h>
  68 +#include <linux/parport.h>
  69 +#endif
  70 +#ifdef __sun__
  71 +#include <sys/stat.h>
  72 +#include <sys/ethernet.h>
  73 +#include <sys/sockio.h>
  74 +#include <netinet/arp.h>
  75 +#include <netinet/in.h>
  76 +#include <netinet/in_systm.h>
  77 +#include <netinet/ip.h>
  78 +#include <netinet/ip_icmp.h> // must come after ip.h
  79 +#include <netinet/udp.h>
  80 +#include <netinet/tcp.h>
  81 +#include <net/if.h>
  82 +#include <syslog.h>
  83 +#include <stropts.h>
  84 +#endif
  85 +#endif
  86 +#endif
  87 +
  88 +#include "qemu_socket.h"
  89 +
  90 +/***********************************************************/
  91 +/* character device */
  92 +
  93 +static void qemu_chr_event(CharDriverState *s, int event)
  94 +{
  95 + if (!s->chr_event)
  96 + return;
  97 + s->chr_event(s->handler_opaque, event);
  98 +}
  99 +
  100 +static void qemu_chr_reset_bh(void *opaque)
  101 +{
  102 + CharDriverState *s = opaque;
  103 + qemu_chr_event(s, CHR_EVENT_RESET);
  104 + qemu_bh_delete(s->bh);
  105 + s->bh = NULL;
  106 +}
  107 +
  108 +void qemu_chr_reset(CharDriverState *s)
  109 +{
  110 + if (s->bh == NULL) {
  111 + s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
  112 + qemu_bh_schedule(s->bh);
  113 + }
  114 +}
  115 +
  116 +int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
  117 +{
  118 + return s->chr_write(s, buf, len);
  119 +}
  120 +
  121 +int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
  122 +{
  123 + if (!s->chr_ioctl)
  124 + return -ENOTSUP;
  125 + return s->chr_ioctl(s, cmd, arg);
  126 +}
  127 +
  128 +int qemu_chr_can_read(CharDriverState *s)
  129 +{
  130 + if (!s->chr_can_read)
  131 + return 0;
  132 + return s->chr_can_read(s->handler_opaque);
  133 +}
  134 +
  135 +void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
  136 +{
  137 + s->chr_read(s->handler_opaque, buf, len);
  138 +}
  139 +
  140 +void qemu_chr_accept_input(CharDriverState *s)
  141 +{
  142 + if (s->chr_accept_input)
  143 + s->chr_accept_input(s);
  144 +}
  145 +
  146 +void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
  147 +{
  148 + char buf[4096];
  149 + va_list ap;
  150 + va_start(ap, fmt);
  151 + vsnprintf(buf, sizeof(buf), fmt, ap);
  152 + qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
  153 + va_end(ap);
  154 +}
  155 +
  156 +void qemu_chr_send_event(CharDriverState *s, int event)
  157 +{
  158 + if (s->chr_send_event)
  159 + s->chr_send_event(s, event);
  160 +}
  161 +
  162 +void qemu_chr_add_handlers(CharDriverState *s,
  163 + IOCanRWHandler *fd_can_read,
  164 + IOReadHandler *fd_read,
  165 + IOEventHandler *fd_event,
  166 + void *opaque)
  167 +{
  168 + s->chr_can_read = fd_can_read;
  169 + s->chr_read = fd_read;
  170 + s->chr_event = fd_event;
  171 + s->handler_opaque = opaque;
  172 + if (s->chr_update_read_handler)
  173 + s->chr_update_read_handler(s);
  174 +}
  175 +
  176 +static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  177 +{
  178 + return len;
  179 +}
  180 +
  181 +static CharDriverState *qemu_chr_open_null(void)
  182 +{
  183 + CharDriverState *chr;
  184 +
  185 + chr = qemu_mallocz(sizeof(CharDriverState));
  186 + if (!chr)
  187 + return NULL;
  188 + chr->chr_write = null_chr_write;
  189 + return chr;
  190 +}
  191 +
  192 +/* MUX driver for serial I/O splitting */
  193 +static int term_timestamps;
  194 +static int64_t term_timestamps_start;
  195 +#define MAX_MUX 4
  196 +#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
  197 +#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
  198 +typedef struct {
  199 + IOCanRWHandler *chr_can_read[MAX_MUX];
  200 + IOReadHandler *chr_read[MAX_MUX];
  201 + IOEventHandler *chr_event[MAX_MUX];
  202 + void *ext_opaque[MAX_MUX];
  203 + CharDriverState *drv;
  204 + unsigned char buffer[MUX_BUFFER_SIZE];
  205 + int prod;
  206 + int cons;
  207 + int mux_cnt;
  208 + int term_got_escape;
  209 + int max_size;
  210 +} MuxDriver;
  211 +
  212 +
  213 +static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  214 +{
  215 + MuxDriver *d = chr->opaque;
  216 + int ret;
  217 + if (!term_timestamps) {
  218 + ret = d->drv->chr_write(d->drv, buf, len);
  219 + } else {
  220 + int i;
  221 +
  222 + ret = 0;
  223 + for(i = 0; i < len; i++) {
  224 + ret += d->drv->chr_write(d->drv, buf+i, 1);
  225 + if (buf[i] == '\n') {
  226 + char buf1[64];
  227 + int64_t ti;
  228 + int secs;
  229 +
  230 + ti = qemu_get_clock(rt_clock);
  231 + if (term_timestamps_start == -1)
  232 + term_timestamps_start = ti;
  233 + ti -= term_timestamps_start;
  234 + secs = ti / 1000000000;
  235 + snprintf(buf1, sizeof(buf1),
  236 + "[%02d:%02d:%02d.%03d] ",
  237 + secs / 3600,
  238 + (secs / 60) % 60,
  239 + secs % 60,
  240 + (int)((ti / 1000000) % 1000));
  241 + d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
  242 + }
  243 + }
  244 + }
  245 + return ret;
  246 +}
  247 +
  248 +static const char * const mux_help[] = {
  249 + "% h print this help\n\r",
  250 + "% x exit emulator\n\r",
  251 + "% s save disk data back to file (if -snapshot)\n\r",
  252 + "% t toggle console timestamps\n\r"
  253 + "% b send break (magic sysrq)\n\r",
  254 + "% c switch between console and monitor\n\r",
  255 + "% % sends %\n\r",
  256 + NULL
  257 +};
  258 +
  259 +int term_escape_char = 0x01; /* ctrl-a is used for escape */
  260 +static void mux_print_help(CharDriverState *chr)
  261 +{
  262 + int i, j;
  263 + char ebuf[15] = "Escape-Char";
  264 + char cbuf[50] = "\n\r";
  265 +
  266 + if (term_escape_char > 0 && term_escape_char < 26) {
  267 + snprintf(cbuf, sizeof(cbuf), "\n\r");
  268 + snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
  269 + } else {
  270 + snprintf(cbuf, sizeof(cbuf),
  271 + "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
  272 + term_escape_char);
  273 + }
  274 + chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
  275 + for (i = 0; mux_help[i] != NULL; i++) {
  276 + for (j=0; mux_help[i][j] != '\0'; j++) {
  277 + if (mux_help[i][j] == '%')
  278 + chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
  279 + else
  280 + chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
  281 + }
  282 + }
  283 +}
  284 +
  285 +static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
  286 +{
  287 + if (d->term_got_escape) {
  288 + d->term_got_escape = 0;
  289 + if (ch == term_escape_char)
  290 + goto send_char;
  291 + switch(ch) {
  292 + case '?':
  293 + case 'h':
  294 + mux_print_help(chr);
  295 + break;
  296 + case 'x':
  297 + {
  298 + const char *term = "QEMU: Terminated\n\r";
  299 + chr->chr_write(chr,(uint8_t *)term,strlen(term));
  300 + exit(0);
  301 + break;
  302 + }
  303 + case 's':
  304 + {
  305 + int i;
  306 + for (i = 0; i < nb_drives; i++) {
  307 + bdrv_commit(drives_table[i].bdrv);
  308 + }
  309 + }
  310 + break;
  311 + case 'b':
  312 + qemu_chr_event(chr, CHR_EVENT_BREAK);
  313 + break;
  314 + case 'c':
  315 + /* Switch to the next registered device */
  316 + chr->focus++;
  317 + if (chr->focus >= d->mux_cnt)
  318 + chr->focus = 0;
  319 + break;
  320 + case 't':
  321 + term_timestamps = !term_timestamps;
  322 + term_timestamps_start = -1;
  323 + break;
  324 + }
  325 + } else if (ch == term_escape_char) {
  326 + d->term_got_escape = 1;
  327 + } else {
  328 + send_char:
  329 + return 1;
  330 + }
  331 + return 0;
  332 +}
  333 +
  334 +static void mux_chr_accept_input(CharDriverState *chr)
  335 +{
  336 + int m = chr->focus;
  337 + MuxDriver *d = chr->opaque;
  338 +
  339 + while (d->prod != d->cons &&
  340 + d->chr_can_read[m] &&
  341 + d->chr_can_read[m](d->ext_opaque[m])) {
  342 + d->chr_read[m](d->ext_opaque[m],
  343 + &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
  344 + }
  345 +}
  346 +
  347 +static int mux_chr_can_read(void *opaque)
  348 +{
  349 + CharDriverState *chr = opaque;
  350 + MuxDriver *d = chr->opaque;
  351 +
  352 + if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
  353 + return 1;
  354 + if (d->chr_can_read[chr->focus])
  355 + return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
  356 + return 0;
  357 +}
  358 +
  359 +static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
  360 +{
  361 + CharDriverState *chr = opaque;
  362 + MuxDriver *d = chr->opaque;
  363 + int m = chr->focus;
  364 + int i;
  365 +
  366 + mux_chr_accept_input (opaque);
  367 +
  368 + for(i = 0; i < size; i++)
  369 + if (mux_proc_byte(chr, d, buf[i])) {
  370 + if (d->prod == d->cons &&
  371 + d->chr_can_read[m] &&
  372 + d->chr_can_read[m](d->ext_opaque[m]))
  373 + d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
  374 + else
  375 + d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
  376 + }
  377 +}
  378 +
  379 +static void mux_chr_event(void *opaque, int event)
  380 +{
  381 + CharDriverState *chr = opaque;
  382 + MuxDriver *d = chr->opaque;
  383 + int i;
  384 +
  385 + /* Send the event to all registered listeners */
  386 + for (i = 0; i < d->mux_cnt; i++)
  387 + if (d->chr_event[i])
  388 + d->chr_event[i](d->ext_opaque[i], event);
  389 +}
  390 +
  391 +static void mux_chr_update_read_handler(CharDriverState *chr)
  392 +{
  393 + MuxDriver *d = chr->opaque;
  394 +
  395 + if (d->mux_cnt >= MAX_MUX) {
  396 + fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
  397 + return;
  398 + }
  399 + d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
  400 + d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
  401 + d->chr_read[d->mux_cnt] = chr->chr_read;
  402 + d->chr_event[d->mux_cnt] = chr->chr_event;
  403 + /* Fix up the real driver with mux routines */
  404 + if (d->mux_cnt == 0) {
  405 + qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
  406 + mux_chr_event, chr);
  407 + }
  408 + chr->focus = d->mux_cnt;
  409 + d->mux_cnt++;
  410 +}
  411 +
  412 +static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
  413 +{
  414 + CharDriverState *chr;
  415 + MuxDriver *d;
  416 +
  417 + chr = qemu_mallocz(sizeof(CharDriverState));
  418 + if (!chr)
  419 + return NULL;
  420 + d = qemu_mallocz(sizeof(MuxDriver));
  421 + if (!d) {
  422 + free(chr);
  423 + return NULL;
  424 + }
  425 +
  426 + chr->opaque = d;
  427 + d->drv = drv;
  428 + chr->focus = -1;
  429 + chr->chr_write = mux_chr_write;
  430 + chr->chr_update_read_handler = mux_chr_update_read_handler;
  431 + chr->chr_accept_input = mux_chr_accept_input;
  432 + return chr;
  433 +}
  434 +
  435 +
  436 +#ifdef _WIN32
  437 +int send_all(int fd, const uint8_t *buf, int len1)
  438 +{
  439 + int ret, len;
  440 +
  441 + len = len1;
  442 + while (len > 0) {
  443 + ret = send(fd, buf, len, 0);
  444 + if (ret < 0) {
  445 + int errno;
  446 + errno = WSAGetLastError();
  447 + if (errno != WSAEWOULDBLOCK) {
  448 + return -1;
  449 + }
  450 + } else if (ret == 0) {
  451 + break;
  452 + } else {
  453 + buf += ret;
  454 + len -= ret;
  455 + }
  456 + }
  457 + return len1 - len;
  458 +}
  459 +
  460 +#else
  461 +
  462 +static int unix_write(int fd, const uint8_t *buf, int len1)
  463 +{
  464 + int ret, len;
  465 +
  466 + len = len1;
  467 + while (len > 0) {
  468 + ret = write(fd, buf, len);
  469 + if (ret < 0) {
  470 + if (errno != EINTR && errno != EAGAIN)
  471 + return -1;
  472 + } else if (ret == 0) {
  473 + break;
  474 + } else {
  475 + buf += ret;
  476 + len -= ret;
  477 + }
  478 + }
  479 + return len1 - len;
  480 +}
  481 +
  482 +inline int send_all(int fd, const uint8_t *buf, int len1)
  483 +{
  484 + return unix_write(fd, buf, len1);
  485 +}
  486 +#endif /* !_WIN32 */
  487 +
  488 +#ifndef _WIN32
  489 +
  490 +typedef struct {
  491 + int fd_in, fd_out;
  492 + int max_size;
  493 +} FDCharDriver;
  494 +
  495 +#define STDIO_MAX_CLIENTS 1
  496 +static int stdio_nb_clients = 0;
  497 +
  498 +static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  499 +{
  500 + FDCharDriver *s = chr->opaque;
  501 + return send_all(s->fd_out, buf, len);
  502 +}
  503 +
  504 +static int fd_chr_read_poll(void *opaque)
  505 +{
  506 + CharDriverState *chr = opaque;
  507 + FDCharDriver *s = chr->opaque;
  508 +
  509 + s->max_size = qemu_chr_can_read(chr);
  510 + return s->max_size;
  511 +}
  512 +
  513 +static void fd_chr_read(void *opaque)
  514 +{
  515 + CharDriverState *chr = opaque;
  516 + FDCharDriver *s = chr->opaque;
  517 + int size, len;
  518 + uint8_t buf[1024];
  519 +
  520 + len = sizeof(buf);
  521 + if (len > s->max_size)
  522 + len = s->max_size;
  523 + if (len == 0)
  524 + return;
  525 + size = read(s->fd_in, buf, len);
  526 + if (size == 0) {
  527 + /* FD has been closed. Remove it from the active list. */
  528 + qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
  529 + return;
  530 + }
  531 + if (size > 0) {
  532 + qemu_chr_read(chr, buf, size);
  533 + }
  534 +}
  535 +
  536 +static void fd_chr_update_read_handler(CharDriverState *chr)
  537 +{
  538 + FDCharDriver *s = chr->opaque;
  539 +
  540 + if (s->fd_in >= 0) {
  541 + if (nographic && s->fd_in == 0) {
  542 + } else {
  543 + qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
  544 + fd_chr_read, NULL, chr);
  545 + }
  546 + }
  547 +}
  548 +
  549 +static void fd_chr_close(struct CharDriverState *chr)
  550 +{
  551 + FDCharDriver *s = chr->opaque;
  552 +
  553 + if (s->fd_in >= 0) {
  554 + if (nographic && s->fd_in == 0) {
  555 + } else {
  556 + qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
  557 + }
  558 + }
  559 +
  560 + qemu_free(s);
  561 +}
  562 +
  563 +/* open a character device to a unix fd */
  564 +static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
  565 +{
  566 + CharDriverState *chr;
  567 + FDCharDriver *s;
  568 +
  569 + chr = qemu_mallocz(sizeof(CharDriverState));
  570 + if (!chr)
  571 + return NULL;
  572 + s = qemu_mallocz(sizeof(FDCharDriver));
  573 + if (!s) {
  574 + free(chr);
  575 + return NULL;
  576 + }
  577 + s->fd_in = fd_in;
  578 + s->fd_out = fd_out;
  579 + chr->opaque = s;
  580 + chr->chr_write = fd_chr_write;
  581 + chr->chr_update_read_handler = fd_chr_update_read_handler;
  582 + chr->chr_close = fd_chr_close;
  583 +
  584 + qemu_chr_reset(chr);
  585 +
  586 + return chr;
  587 +}
  588 +
  589 +static CharDriverState *qemu_chr_open_file_out(const char *file_out)
  590 +{
  591 + int fd_out;
  592 +
  593 + TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
  594 + if (fd_out < 0)
  595 + return NULL;
  596 + return qemu_chr_open_fd(-1, fd_out);
  597 +}
  598 +
  599 +static CharDriverState *qemu_chr_open_pipe(const char *filename)
  600 +{
  601 + int fd_in, fd_out;
  602 + char filename_in[256], filename_out[256];
  603 +
  604 + snprintf(filename_in, 256, "%s.in", filename);
  605 + snprintf(filename_out, 256, "%s.out", filename);
  606 + TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
  607 + TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
  608 + if (fd_in < 0 || fd_out < 0) {
  609 + if (fd_in >= 0)
  610 + close(fd_in);
  611 + if (fd_out >= 0)
  612 + close(fd_out);
  613 + TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
  614 + if (fd_in < 0)
  615 + return NULL;
  616 + }
  617 + return qemu_chr_open_fd(fd_in, fd_out);
  618 +}
  619 +
  620 +
  621 +/* for STDIO, we handle the case where several clients use it
  622 + (nographic mode) */
  623 +
  624 +#define TERM_FIFO_MAX_SIZE 1
  625 +
  626 +static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
  627 +static int term_fifo_size;
  628 +
  629 +static int stdio_read_poll(void *opaque)
  630 +{
  631 + CharDriverState *chr = opaque;
  632 +
  633 + /* try to flush the queue if needed */
  634 + if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
  635 + qemu_chr_read(chr, term_fifo, 1);
  636 + term_fifo_size = 0;
  637 + }
  638 + /* see if we can absorb more chars */
  639 + if (term_fifo_size == 0)
  640 + return 1;
  641 + else
  642 + return 0;
  643 +}
  644 +
  645 +static void stdio_read(void *opaque)
  646 +{
  647 + int size;
  648 + uint8_t buf[1];
  649 + CharDriverState *chr = opaque;
  650 +
  651 + size = read(0, buf, 1);
  652 + if (size == 0) {
  653 + /* stdin has been closed. Remove it from the active list. */
  654 + qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
  655 + return;
  656 + }
  657 + if (size > 0) {
  658 + if (qemu_chr_can_read(chr) > 0) {
  659 + qemu_chr_read(chr, buf, 1);
  660 + } else if (term_fifo_size == 0) {
  661 + term_fifo[term_fifo_size++] = buf[0];
  662 + }
  663 + }
  664 +}
  665 +
  666 +/* init terminal so that we can grab keys */
  667 +static struct termios oldtty;
  668 +static int old_fd0_flags;
  669 +static int term_atexit_done;
  670 +
  671 +static void term_exit(void)
  672 +{
  673 + tcsetattr (0, TCSANOW, &oldtty);
  674 + fcntl(0, F_SETFL, old_fd0_flags);
  675 +}
  676 +
  677 +static void term_init(void)
  678 +{
  679 + struct termios tty;
  680 +
  681 + tcgetattr (0, &tty);
  682 + oldtty = tty;
  683 + old_fd0_flags = fcntl(0, F_GETFL);
  684 +
  685 + tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  686 + |INLCR|IGNCR|ICRNL|IXON);
  687 + tty.c_oflag |= OPOST;
  688 + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  689 + /* if graphical mode, we allow Ctrl-C handling */
  690 + if (nographic)
  691 + tty.c_lflag &= ~ISIG;
  692 + tty.c_cflag &= ~(CSIZE|PARENB);
  693 + tty.c_cflag |= CS8;
  694 + tty.c_cc[VMIN] = 1;
  695 + tty.c_cc[VTIME] = 0;
  696 +
  697 + tcsetattr (0, TCSANOW, &tty);
  698 +
  699 + if (!term_atexit_done++)
  700 + atexit(term_exit);
  701 +
  702 + fcntl(0, F_SETFL, O_NONBLOCK);
  703 +}
  704 +
  705 +static void qemu_chr_close_stdio(struct CharDriverState *chr)
  706 +{
  707 + term_exit();
  708 + stdio_nb_clients--;
  709 + qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
  710 + fd_chr_close(chr);
  711 +}
  712 +
  713 +static CharDriverState *qemu_chr_open_stdio(void)
  714 +{
  715 + CharDriverState *chr;
  716 +
  717 + if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
  718 + return NULL;
  719 + chr = qemu_chr_open_fd(0, 1);
  720 + chr->chr_close = qemu_chr_close_stdio;
  721 + qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
  722 + stdio_nb_clients++;
  723 + term_init();
  724 +
  725 + return chr;
  726 +}
  727 +
  728 +#ifdef __sun__
  729 +/* Once Solaris has openpty(), this is going to be removed. */
  730 +int openpty(int *amaster, int *aslave, char *name,
  731 + struct termios *termp, struct winsize *winp)
  732 +{
  733 + const char *slave;
  734 + int mfd = -1, sfd = -1;
  735 +
  736 + *amaster = *aslave = -1;
  737 +
  738 + mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  739 + if (mfd < 0)
  740 + goto err;
  741 +
  742 + if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
  743 + goto err;
  744 +
  745 + if ((slave = ptsname(mfd)) == NULL)
  746 + goto err;
  747 +
  748 + if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
  749 + goto err;
  750 +
  751 + if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
  752 + (termp != NULL && tcgetattr(sfd, termp) < 0))
  753 + goto err;
  754 +
  755 + if (amaster)
  756 + *amaster = mfd;
  757 + if (aslave)
  758 + *aslave = sfd;
  759 + if (winp)
  760 + ioctl(sfd, TIOCSWINSZ, winp);
  761 +
  762 + return 0;
  763 +
  764 +err:
  765 + if (sfd != -1)
  766 + close(sfd);
  767 + close(mfd);
  768 + return -1;
  769 +}
  770 +
  771 +void cfmakeraw (struct termios *termios_p)
  772 +{
  773 + termios_p->c_iflag &=
  774 + ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  775 + termios_p->c_oflag &= ~OPOST;
  776 + termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  777 + termios_p->c_cflag &= ~(CSIZE|PARENB);
  778 + termios_p->c_cflag |= CS8;
  779 +
  780 + termios_p->c_cc[VMIN] = 0;
  781 + termios_p->c_cc[VTIME] = 0;
  782 +}
  783 +#endif
  784 +
  785 +#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  786 + || defined(__NetBSD__) || defined(__OpenBSD__)
  787 +
  788 +typedef struct {
  789 + int fd;
  790 + int connected;
  791 + int polling;
  792 + int read_bytes;
  793 + QEMUTimer *timer;
  794 +} PtyCharDriver;
  795 +
  796 +static void pty_chr_update_read_handler(CharDriverState *chr);
  797 +static void pty_chr_state(CharDriverState *chr, int connected);
  798 +
  799 +static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  800 +{
  801 + PtyCharDriver *s = chr->opaque;
  802 +
  803 + if (!s->connected) {
  804 + /* guest sends data, check for (re-)connect */
  805 + pty_chr_update_read_handler(chr);
  806 + return 0;
  807 + }
  808 + return send_all(s->fd, buf, len);
  809 +}
  810 +
  811 +static int pty_chr_read_poll(void *opaque)
  812 +{
  813 + CharDriverState *chr = opaque;
  814 + PtyCharDriver *s = chr->opaque;
  815 +
  816 + s->read_bytes = qemu_chr_can_read(chr);
  817 + return s->read_bytes;
  818 +}
  819 +
  820 +static void pty_chr_read(void *opaque)
  821 +{
  822 + CharDriverState *chr = opaque;
  823 + PtyCharDriver *s = chr->opaque;
  824 + int size, len;
  825 + uint8_t buf[1024];
  826 +
  827 + len = sizeof(buf);
  828 + if (len > s->read_bytes)
  829 + len = s->read_bytes;
  830 + if (len == 0)
  831 + return;
  832 + size = read(s->fd, buf, len);
  833 + if ((size == -1 && errno == EIO) ||
  834 + (size == 0)) {
  835 + pty_chr_state(chr, 0);
  836 + return;
  837 + }
  838 + if (size > 0) {
  839 + pty_chr_state(chr, 1);
  840 + qemu_chr_read(chr, buf, size);
  841 + }
  842 +}
  843 +
  844 +static void pty_chr_update_read_handler(CharDriverState *chr)
  845 +{
  846 + PtyCharDriver *s = chr->opaque;
  847 +
  848 + qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
  849 + pty_chr_read, NULL, chr);
  850 + s->polling = 1;
  851 + /*
  852 + * Short timeout here: just need wait long enougth that qemu makes
  853 + * it through the poll loop once. When reconnected we want a
  854 + * short timeout so we notice it almost instantly. Otherwise
  855 + * read() gives us -EIO instantly, making pty_chr_state() reset the
  856 + * timeout to the normal (much longer) poll interval before the
  857 + * timer triggers.
  858 + */
  859 + qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
  860 +}
  861 +
  862 +static void pty_chr_state(CharDriverState *chr, int connected)
  863 +{
  864 + PtyCharDriver *s = chr->opaque;
  865 +
  866 + if (!connected) {
  867 + qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  868 + s->connected = 0;
  869 + s->polling = 0;
  870 + /* (re-)connect poll interval for idle guests: once per second.
  871 + * We check more frequently in case the guests sends data to
  872 + * the virtual device linked to our pty. */
  873 + qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
  874 + } else {
  875 + if (!s->connected)
  876 + qemu_chr_reset(chr);
  877 + s->connected = 1;
  878 + }
  879 +}
  880 +
  881 +static void pty_chr_timer(void *opaque)
  882 +{
  883 + struct CharDriverState *chr = opaque;
  884 + PtyCharDriver *s = chr->opaque;
  885 +
  886 + if (s->connected)
  887 + return;
  888 + if (s->polling) {
  889 + /* If we arrive here without polling being cleared due
  890 + * read returning -EIO, then we are (re-)connected */
  891 + pty_chr_state(chr, 1);
  892 + return;
  893 + }
  894 +
  895 + /* Next poll ... */
  896 + pty_chr_update_read_handler(chr);
  897 +}
  898 +
  899 +static void pty_chr_close(struct CharDriverState *chr)
  900 +{
  901 + PtyCharDriver *s = chr->opaque;
  902 +
  903 + qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  904 + close(s->fd);
  905 + qemu_free(s);
  906 +}
  907 +
  908 +static CharDriverState *qemu_chr_open_pty(void)
  909 +{
  910 + CharDriverState *chr;
  911 + PtyCharDriver *s;
  912 + struct termios tty;
  913 + int slave_fd, len;
  914 +#if defined(__OpenBSD__)
  915 + char pty_name[PATH_MAX];
  916 +#define q_ptsname(x) pty_name
  917 +#else
  918 + char *pty_name = NULL;
  919 +#define q_ptsname(x) ptsname(x)
  920 +#endif
  921 +
  922 + chr = qemu_mallocz(sizeof(CharDriverState));
  923 + if (!chr)
  924 + return NULL;
  925 + s = qemu_mallocz(sizeof(PtyCharDriver));
  926 + if (!s) {
  927 + qemu_free(chr);
  928 + return NULL;
  929 + }
  930 +
  931 + if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
  932 + return NULL;
  933 + }
  934 +
  935 + /* Set raw attributes on the pty. */
  936 + cfmakeraw(&tty);
  937 + tcsetattr(slave_fd, TCSAFLUSH, &tty);
  938 + close(slave_fd);
  939 +
  940 + len = strlen(q_ptsname(s->fd)) + 5;
  941 + chr->filename = qemu_malloc(len);
  942 + snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
  943 + fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
  944 +
  945 + chr->opaque = s;
  946 + chr->chr_write = pty_chr_write;
  947 + chr->chr_update_read_handler = pty_chr_update_read_handler;
  948 + chr->chr_close = pty_chr_close;
  949 +
  950 + s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
  951 +
  952 + return chr;
  953 +}
  954 +
  955 +static void tty_serial_init(int fd, int speed,
  956 + int parity, int data_bits, int stop_bits)
  957 +{
  958 + struct termios tty;
  959 + speed_t spd;
  960 +
  961 +#if 0
  962 + printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
  963 + speed, parity, data_bits, stop_bits);
  964 +#endif
  965 + tcgetattr (fd, &tty);
  966 +
  967 +#define MARGIN 1.1
  968 + if (speed <= 50 * MARGIN)
  969 + spd = B50;
  970 + else if (speed <= 75 * MARGIN)
  971 + spd = B75;
  972 + else if (speed <= 300 * MARGIN)
  973 + spd = B300;
  974 + else if (speed <= 600 * MARGIN)
  975 + spd = B600;
  976 + else if (speed <= 1200 * MARGIN)
  977 + spd = B1200;
  978 + else if (speed <= 2400 * MARGIN)
  979 + spd = B2400;
  980 + else if (speed <= 4800 * MARGIN)
  981 + spd = B4800;
  982 + else if (speed <= 9600 * MARGIN)
  983 + spd = B9600;
  984 + else if (speed <= 19200 * MARGIN)
  985 + spd = B19200;
  986 + else if (speed <= 38400 * MARGIN)
  987 + spd = B38400;
  988 + else if (speed <= 57600 * MARGIN)
  989 + spd = B57600;
  990 + else if (speed <= 115200 * MARGIN)
  991 + spd = B115200;
  992 + else
  993 + spd = B115200;
  994 +
  995 + cfsetispeed(&tty, spd);
  996 + cfsetospeed(&tty, spd);
  997 +
  998 + tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  999 + |INLCR|IGNCR|ICRNL|IXON);
  1000 + tty.c_oflag |= OPOST;
  1001 + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
  1002 + tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
  1003 + switch(data_bits) {
  1004 + default:
  1005 + case 8:
  1006 + tty.c_cflag |= CS8;
  1007 + break;
  1008 + case 7:
  1009 + tty.c_cflag |= CS7;
  1010 + break;
  1011 + case 6:
  1012 + tty.c_cflag |= CS6;
  1013 + break;
  1014 + case 5:
  1015 + tty.c_cflag |= CS5;
  1016 + break;
  1017 + }
  1018 + switch(parity) {
  1019 + default:
  1020 + case 'N':
  1021 + break;
  1022 + case 'E':
  1023 + tty.c_cflag |= PARENB;
  1024 + break;
  1025 + case 'O':
  1026 + tty.c_cflag |= PARENB | PARODD;
  1027 + break;
  1028 + }
  1029 + if (stop_bits == 2)
  1030 + tty.c_cflag |= CSTOPB;
  1031 +
  1032 + tcsetattr (fd, TCSANOW, &tty);
  1033 +}
  1034 +
  1035 +static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
  1036 +{
  1037 + FDCharDriver *s = chr->opaque;
  1038 +
  1039 + switch(cmd) {
  1040 + case CHR_IOCTL_SERIAL_SET_PARAMS:
  1041 + {
  1042 + QEMUSerialSetParams *ssp = arg;
  1043 + tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
  1044 + ssp->data_bits, ssp->stop_bits);
  1045 + }
  1046 + break;
  1047 + case CHR_IOCTL_SERIAL_SET_BREAK:
  1048 + {
  1049 + int enable = *(int *)arg;
  1050 + if (enable)
  1051 + tcsendbreak(s->fd_in, 1);
  1052 + }
  1053 + break;
  1054 + case CHR_IOCTL_SERIAL_GET_TIOCM:
  1055 + {
  1056 + int sarg = 0;
  1057 + int *targ = (int *)arg;
  1058 + ioctl(s->fd_in, TIOCMGET, &sarg);
  1059 + *targ = 0;
  1060 + if (sarg | TIOCM_CTS)
  1061 + *targ |= CHR_TIOCM_CTS;
  1062 + if (sarg | TIOCM_CAR)
  1063 + *targ |= CHR_TIOCM_CAR;
  1064 + if (sarg | TIOCM_DSR)
  1065 + *targ |= CHR_TIOCM_DSR;
  1066 + if (sarg | TIOCM_RI)
  1067 + *targ |= CHR_TIOCM_RI;
  1068 + if (sarg | TIOCM_DTR)
  1069 + *targ |= CHR_TIOCM_DTR;
  1070 + if (sarg | TIOCM_RTS)
  1071 + *targ |= CHR_TIOCM_RTS;
  1072 + }
  1073 + break;
  1074 + case CHR_IOCTL_SERIAL_SET_TIOCM:
  1075 + {
  1076 + int sarg = *(int *)arg;
  1077 + int targ = 0;
  1078 + if (sarg | CHR_TIOCM_DTR)
  1079 + targ |= TIOCM_DTR;
  1080 + if (sarg | CHR_TIOCM_RTS)
  1081 + targ |= TIOCM_RTS;
  1082 + ioctl(s->fd_in, TIOCMSET, &targ);
  1083 + }
  1084 + break;
  1085 + default:
  1086 + return -ENOTSUP;
  1087 + }
  1088 + return 0;
  1089 +}
  1090 +
  1091 +static CharDriverState *qemu_chr_open_tty(const char *filename)
  1092 +{
  1093 + CharDriverState *chr;
  1094 + int fd;
  1095 +
  1096 + TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
  1097 + tty_serial_init(fd, 115200, 'N', 8, 1);
  1098 + chr = qemu_chr_open_fd(fd, fd);
  1099 + if (!chr) {
  1100 + close(fd);
  1101 + return NULL;
  1102 + }
  1103 + chr->chr_ioctl = tty_serial_ioctl;
  1104 + qemu_chr_reset(chr);
  1105 + return chr;
  1106 +}
  1107 +#else /* ! __linux__ && ! __sun__ */
  1108 +static CharDriverState *qemu_chr_open_pty(void)
  1109 +{
  1110 + return NULL;
  1111 +}
  1112 +#endif /* __linux__ || __sun__ */
  1113 +
  1114 +#if defined(__linux__)
  1115 +typedef struct {
  1116 + int fd;
  1117 + int mode;
  1118 +} ParallelCharDriver;
  1119 +
  1120 +static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
  1121 +{
  1122 + if (s->mode != mode) {
  1123 + int m = mode;
  1124 + if (ioctl(s->fd, PPSETMODE, &m) < 0)
  1125 + return 0;
  1126 + s->mode = mode;
  1127 + }
  1128 + return 1;
  1129 +}
  1130 +
  1131 +static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1132 +{
  1133 + ParallelCharDriver *drv = chr->opaque;
  1134 + int fd = drv->fd;
  1135 + uint8_t b;
  1136 +
  1137 + switch(cmd) {
  1138 + case CHR_IOCTL_PP_READ_DATA:
  1139 + if (ioctl(fd, PPRDATA, &b) < 0)
  1140 + return -ENOTSUP;
  1141 + *(uint8_t *)arg = b;
  1142 + break;
  1143 + case CHR_IOCTL_PP_WRITE_DATA:
  1144 + b = *(uint8_t *)arg;
  1145 + if (ioctl(fd, PPWDATA, &b) < 0)
  1146 + return -ENOTSUP;
  1147 + break;
  1148 + case CHR_IOCTL_PP_READ_CONTROL:
  1149 + if (ioctl(fd, PPRCONTROL, &b) < 0)
  1150 + return -ENOTSUP;
  1151 + /* Linux gives only the lowest bits, and no way to know data
  1152 + direction! For better compatibility set the fixed upper
  1153 + bits. */
  1154 + *(uint8_t *)arg = b | 0xc0;
  1155 + break;
  1156 + case CHR_IOCTL_PP_WRITE_CONTROL:
  1157 + b = *(uint8_t *)arg;
  1158 + if (ioctl(fd, PPWCONTROL, &b) < 0)
  1159 + return -ENOTSUP;
  1160 + break;
  1161 + case CHR_IOCTL_PP_READ_STATUS:
  1162 + if (ioctl(fd, PPRSTATUS, &b) < 0)
  1163 + return -ENOTSUP;
  1164 + *(uint8_t *)arg = b;
  1165 + break;
  1166 + case CHR_IOCTL_PP_DATA_DIR:
  1167 + if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
  1168 + return -ENOTSUP;
  1169 + break;
  1170 + case CHR_IOCTL_PP_EPP_READ_ADDR:
  1171 + if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1172 + struct ParallelIOArg *parg = arg;
  1173 + int n = read(fd, parg->buffer, parg->count);
  1174 + if (n != parg->count) {
  1175 + return -EIO;
  1176 + }
  1177 + }
  1178 + break;
  1179 + case CHR_IOCTL_PP_EPP_READ:
  1180 + if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1181 + struct ParallelIOArg *parg = arg;
  1182 + int n = read(fd, parg->buffer, parg->count);
  1183 + if (n != parg->count) {
  1184 + return -EIO;
  1185 + }
  1186 + }
  1187 + break;
  1188 + case CHR_IOCTL_PP_EPP_WRITE_ADDR:
  1189 + if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1190 + struct ParallelIOArg *parg = arg;
  1191 + int n = write(fd, parg->buffer, parg->count);
  1192 + if (n != parg->count) {
  1193 + return -EIO;
  1194 + }
  1195 + }
  1196 + break;
  1197 + case CHR_IOCTL_PP_EPP_WRITE:
  1198 + if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1199 + struct ParallelIOArg *parg = arg;
  1200 + int n = write(fd, parg->buffer, parg->count);
  1201 + if (n != parg->count) {
  1202 + return -EIO;
  1203 + }
  1204 + }
  1205 + break;
  1206 + default:
  1207 + return -ENOTSUP;
  1208 + }
  1209 + return 0;
  1210 +}
  1211 +
  1212 +static void pp_close(CharDriverState *chr)
  1213 +{
  1214 + ParallelCharDriver *drv = chr->opaque;
  1215 + int fd = drv->fd;
  1216 +
  1217 + pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
  1218 + ioctl(fd, PPRELEASE);
  1219 + close(fd);
  1220 + qemu_free(drv);
  1221 +}
  1222 +
  1223 +static CharDriverState *qemu_chr_open_pp(const char *filename)
  1224 +{
  1225 + CharDriverState *chr;
  1226 + ParallelCharDriver *drv;
  1227 + int fd;
  1228 +
  1229 + TFR(fd = open(filename, O_RDWR));
  1230 + if (fd < 0)
  1231 + return NULL;
  1232 +
  1233 + if (ioctl(fd, PPCLAIM) < 0) {
  1234 + close(fd);
  1235 + return NULL;
  1236 + }
  1237 +
  1238 + drv = qemu_mallocz(sizeof(ParallelCharDriver));
  1239 + if (!drv) {
  1240 + close(fd);
  1241 + return NULL;
  1242 + }
  1243 + drv->fd = fd;
  1244 + drv->mode = IEEE1284_MODE_COMPAT;
  1245 +
  1246 + chr = qemu_mallocz(sizeof(CharDriverState));
  1247 + if (!chr) {
  1248 + qemu_free(drv);
  1249 + close(fd);
  1250 + return NULL;
  1251 + }
  1252 + chr->chr_write = null_chr_write;
  1253 + chr->chr_ioctl = pp_ioctl;
  1254 + chr->chr_close = pp_close;
  1255 + chr->opaque = drv;
  1256 +
  1257 + qemu_chr_reset(chr);
  1258 +
  1259 + return chr;
  1260 +}
  1261 +#endif /* __linux__ */
  1262 +
  1263 +#else /* _WIN32 */
  1264 +
  1265 +typedef struct {
  1266 + int max_size;
  1267 + HANDLE hcom, hrecv, hsend;
  1268 + OVERLAPPED orecv, osend;
  1269 + BOOL fpipe;
  1270 + DWORD len;
  1271 +} WinCharState;
  1272 +
  1273 +#define NSENDBUF 2048
  1274 +#define NRECVBUF 2048
  1275 +#define MAXCONNECT 1
  1276 +#define NTIMEOUT 5000
  1277 +
  1278 +static int win_chr_poll(void *opaque);
  1279 +static int win_chr_pipe_poll(void *opaque);
  1280 +
  1281 +static void win_chr_close(CharDriverState *chr)
  1282 +{
  1283 + WinCharState *s = chr->opaque;
  1284 +
  1285 + if (s->hsend) {
  1286 + CloseHandle(s->hsend);
  1287 + s->hsend = NULL;
  1288 + }
  1289 + if (s->hrecv) {
  1290 + CloseHandle(s->hrecv);
  1291 + s->hrecv = NULL;
  1292 + }
  1293 + if (s->hcom) {
  1294 + CloseHandle(s->hcom);
  1295 + s->hcom = NULL;
  1296 + }
  1297 + if (s->fpipe)
  1298 + qemu_del_polling_cb(win_chr_pipe_poll, chr);
  1299 + else
  1300 + qemu_del_polling_cb(win_chr_poll, chr);
  1301 +}
  1302 +
  1303 +static int win_chr_init(CharDriverState *chr, const char *filename)
  1304 +{
  1305 + WinCharState *s = chr->opaque;
  1306 + COMMCONFIG comcfg;
  1307 + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
  1308 + COMSTAT comstat;
  1309 + DWORD size;
  1310 + DWORD err;
  1311 +
  1312 + s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1313 + if (!s->hsend) {
  1314 + fprintf(stderr, "Failed CreateEvent\n");
  1315 + goto fail;
  1316 + }
  1317 + s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1318 + if (!s->hrecv) {
  1319 + fprintf(stderr, "Failed CreateEvent\n");
  1320 + goto fail;
  1321 + }
  1322 +
  1323 + s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
  1324 + OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  1325 + if (s->hcom == INVALID_HANDLE_VALUE) {
  1326 + fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
  1327 + s->hcom = NULL;
  1328 + goto fail;
  1329 + }
  1330 +
  1331 + if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
  1332 + fprintf(stderr, "Failed SetupComm\n");
  1333 + goto fail;
  1334 + }
  1335 +
  1336 + ZeroMemory(&comcfg, sizeof(COMMCONFIG));
  1337 + size = sizeof(COMMCONFIG);
  1338 + GetDefaultCommConfig(filename, &comcfg, &size);
  1339 + comcfg.dcb.DCBlength = sizeof(DCB);
  1340 + CommConfigDialog(filename, NULL, &comcfg);
  1341 +
  1342 + if (!SetCommState(s->hcom, &comcfg.dcb)) {
  1343 + fprintf(stderr, "Failed SetCommState\n");
  1344 + goto fail;
  1345 + }
  1346 +
  1347 + if (!SetCommMask(s->hcom, EV_ERR)) {
  1348 + fprintf(stderr, "Failed SetCommMask\n");
  1349 + goto fail;
  1350 + }
  1351 +
  1352 + cto.ReadIntervalTimeout = MAXDWORD;
  1353 + if (!SetCommTimeouts(s->hcom, &cto)) {
  1354 + fprintf(stderr, "Failed SetCommTimeouts\n");
  1355 + goto fail;
  1356 + }
  1357 +
  1358 + if (!ClearCommError(s->hcom, &err, &comstat)) {
  1359 + fprintf(stderr, "Failed ClearCommError\n");
  1360 + goto fail;
  1361 + }
  1362 + qemu_add_polling_cb(win_chr_poll, chr);
  1363 + return 0;
  1364 +
  1365 + fail:
  1366 + win_chr_close(chr);
  1367 + return -1;
  1368 +}
  1369 +
  1370 +static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
  1371 +{
  1372 + WinCharState *s = chr->opaque;
  1373 + DWORD len, ret, size, err;
  1374 +
  1375 + len = len1;
  1376 + ZeroMemory(&s->osend, sizeof(s->osend));
  1377 + s->osend.hEvent = s->hsend;
  1378 + while (len > 0) {
  1379 + if (s->hsend)
  1380 + ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
  1381 + else
  1382 + ret = WriteFile(s->hcom, buf, len, &size, NULL);
  1383 + if (!ret) {
  1384 + err = GetLastError();
  1385 + if (err == ERROR_IO_PENDING) {
  1386 + ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
  1387 + if (ret) {
  1388 + buf += size;
  1389 + len -= size;
  1390 + } else {
  1391 + break;
  1392 + }
  1393 + } else {
  1394 + break;
  1395 + }
  1396 + } else {
  1397 + buf += size;
  1398 + len -= size;
  1399 + }
  1400 + }
  1401 + return len1 - len;
  1402 +}
  1403 +
  1404 +static int win_chr_read_poll(CharDriverState *chr)
  1405 +{
  1406 + WinCharState *s = chr->opaque;
  1407 +
  1408 + s->max_size = qemu_chr_can_read(chr);
  1409 + return s->max_size;
  1410 +}
  1411 +
  1412 +static void win_chr_readfile(CharDriverState *chr)
  1413 +{
  1414 + WinCharState *s = chr->opaque;
  1415 + int ret, err;
  1416 + uint8_t buf[1024];
  1417 + DWORD size;
  1418 +
  1419 + ZeroMemory(&s->orecv, sizeof(s->orecv));
  1420 + s->orecv.hEvent = s->hrecv;
  1421 + ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
  1422 + if (!ret) {
  1423 + err = GetLastError();
  1424 + if (err == ERROR_IO_PENDING) {
  1425 + ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
  1426 + }
  1427 + }
  1428 +
  1429 + if (size > 0) {
  1430 + qemu_chr_read(chr, buf, size);
  1431 + }
  1432 +}
  1433 +
  1434 +static void win_chr_read(CharDriverState *chr)
  1435 +{
  1436 + WinCharState *s = chr->opaque;
  1437 +
  1438 + if (s->len > s->max_size)
  1439 + s->len = s->max_size;
  1440 + if (s->len == 0)
  1441 + return;
  1442 +
  1443 + win_chr_readfile(chr);
  1444 +}
  1445 +
  1446 +static int win_chr_poll(void *opaque)
  1447 +{
  1448 + CharDriverState *chr = opaque;
  1449 + WinCharState *s = chr->opaque;
  1450 + COMSTAT status;
  1451 + DWORD comerr;
  1452 +
  1453 + ClearCommError(s->hcom, &comerr, &status);
  1454 + if (status.cbInQue > 0) {
  1455 + s->len = status.cbInQue;
  1456 + win_chr_read_poll(chr);
  1457 + win_chr_read(chr);
  1458 + return 1;
  1459 + }
  1460 + return 0;
  1461 +}
  1462 +
  1463 +static CharDriverState *qemu_chr_open_win(const char *filename)
  1464 +{
  1465 + CharDriverState *chr;
  1466 + WinCharState *s;
  1467 +
  1468 + chr = qemu_mallocz(sizeof(CharDriverState));
  1469 + if (!chr)
  1470 + return NULL;
  1471 + s = qemu_mallocz(sizeof(WinCharState));
  1472 + if (!s) {
  1473 + free(chr);
  1474 + return NULL;
  1475 + }
  1476 + chr->opaque = s;
  1477 + chr->chr_write = win_chr_write;
  1478 + chr->chr_close = win_chr_close;
  1479 +
  1480 + if (win_chr_init(chr, filename) < 0) {
  1481 + free(s);
  1482 + free(chr);
  1483 + return NULL;
  1484 + }
  1485 + qemu_chr_reset(chr);
  1486 + return chr;
  1487 +}
  1488 +
  1489 +static int win_chr_pipe_poll(void *opaque)
  1490 +{
  1491 + CharDriverState *chr = opaque;
  1492 + WinCharState *s = chr->opaque;
  1493 + DWORD size;
  1494 +
  1495 + PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
  1496 + if (size > 0) {
  1497 + s->len = size;
  1498 + win_chr_read_poll(chr);
  1499 + win_chr_read(chr);
  1500 + return 1;
  1501 + }
  1502 + return 0;
  1503 +}
  1504 +
  1505 +static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
  1506 +{
  1507 + WinCharState *s = chr->opaque;
  1508 + OVERLAPPED ov;
  1509 + int ret;
  1510 + DWORD size;
  1511 + char openname[256];
  1512 +
  1513 + s->fpipe = TRUE;
  1514 +
  1515 + s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1516 + if (!s->hsend) {
  1517 + fprintf(stderr, "Failed CreateEvent\n");
  1518 + goto fail;
  1519 + }
  1520 + s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1521 + if (!s->hrecv) {
  1522 + fprintf(stderr, "Failed CreateEvent\n");
  1523 + goto fail;
  1524 + }
  1525 +
  1526 + snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
  1527 + s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  1528 + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
  1529 + PIPE_WAIT,
  1530 + MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
  1531 + if (s->hcom == INVALID_HANDLE_VALUE) {
  1532 + fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
  1533 + s->hcom = NULL;
  1534 + goto fail;
  1535 + }
  1536 +
  1537 + ZeroMemory(&ov, sizeof(ov));
  1538 + ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  1539 + ret = ConnectNamedPipe(s->hcom, &ov);
  1540 + if (ret) {
  1541 + fprintf(stderr, "Failed ConnectNamedPipe\n");
  1542 + goto fail;
  1543 + }
  1544 +
  1545 + ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
  1546 + if (!ret) {
  1547 + fprintf(stderr, "Failed GetOverlappedResult\n");
  1548 + if (ov.hEvent) {
  1549 + CloseHandle(ov.hEvent);
  1550 + ov.hEvent = NULL;
  1551 + }
  1552 + goto fail;
  1553 + }
  1554 +
  1555 + if (ov.hEvent) {
  1556 + CloseHandle(ov.hEvent);
  1557 + ov.hEvent = NULL;
  1558 + }
  1559 + qemu_add_polling_cb(win_chr_pipe_poll, chr);
  1560 + return 0;
  1561 +
  1562 + fail:
  1563 + win_chr_close(chr);
  1564 + return -1;
  1565 +}
  1566 +
  1567 +
  1568 +static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
  1569 +{
  1570 + CharDriverState *chr;
  1571 + WinCharState *s;
  1572 +
  1573 + chr = qemu_mallocz(sizeof(CharDriverState));
  1574 + if (!chr)
  1575 + return NULL;
  1576 + s = qemu_mallocz(sizeof(WinCharState));
  1577 + if (!s) {
  1578 + free(chr);
  1579 + return NULL;
  1580 + }
  1581 + chr->opaque = s;
  1582 + chr->chr_write = win_chr_write;
  1583 + chr->chr_close = win_chr_close;
  1584 +
  1585 + if (win_chr_pipe_init(chr, filename) < 0) {
  1586 + free(s);
  1587 + free(chr);
  1588 + return NULL;
  1589 + }
  1590 + qemu_chr_reset(chr);
  1591 + return chr;
  1592 +}
  1593 +
  1594 +static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
  1595 +{
  1596 + CharDriverState *chr;
  1597 + WinCharState *s;
  1598 +
  1599 + chr = qemu_mallocz(sizeof(CharDriverState));
  1600 + if (!chr)
  1601 + return NULL;
  1602 + s = qemu_mallocz(sizeof(WinCharState));
  1603 + if (!s) {
  1604 + free(chr);
  1605 + return NULL;
  1606 + }
  1607 + s->hcom = fd_out;
  1608 + chr->opaque = s;
  1609 + chr->chr_write = win_chr_write;
  1610 + qemu_chr_reset(chr);
  1611 + return chr;
  1612 +}
  1613 +
  1614 +static CharDriverState *qemu_chr_open_win_con(const char *filename)
  1615 +{
  1616 + return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
  1617 +}
  1618 +
  1619 +static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
  1620 +{
  1621 + HANDLE fd_out;
  1622 +
  1623 + fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  1624 + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  1625 + if (fd_out == INVALID_HANDLE_VALUE)
  1626 + return NULL;
  1627 +
  1628 + return qemu_chr_open_win_file(fd_out);
  1629 +}
  1630 +#endif /* !_WIN32 */
  1631 +
  1632 +/***********************************************************/
  1633 +/* UDP Net console */
  1634 +
  1635 +typedef struct {
  1636 + int fd;
  1637 + struct sockaddr_in daddr;
  1638 + uint8_t buf[1024];
  1639 + int bufcnt;
  1640 + int bufptr;
  1641 + int max_size;
  1642 +} NetCharDriver;
  1643 +
  1644 +static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1645 +{
  1646 + NetCharDriver *s = chr->opaque;
  1647 +
  1648 + return sendto(s->fd, buf, len, 0,
  1649 + (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
  1650 +}
  1651 +
  1652 +static int udp_chr_read_poll(void *opaque)
  1653 +{
  1654 + CharDriverState *chr = opaque;
  1655 + NetCharDriver *s = chr->opaque;
  1656 +
  1657 + s->max_size = qemu_chr_can_read(chr);
  1658 +
  1659 + /* If there were any stray characters in the queue process them
  1660 + * first
  1661 + */
  1662 + while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1663 + qemu_chr_read(chr, &s->buf[s->bufptr], 1);
  1664 + s->bufptr++;
  1665 + s->max_size = qemu_chr_can_read(chr);
  1666 + }
  1667 + return s->max_size;
  1668 +}
  1669 +
  1670 +static void udp_chr_read(void *opaque)
  1671 +{
  1672 + CharDriverState *chr = opaque;
  1673 + NetCharDriver *s = chr->opaque;
  1674 +
  1675 + if (s->max_size == 0)
  1676 + return;
  1677 + s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
  1678 + s->bufptr = s->bufcnt;
  1679 + if (s->bufcnt <= 0)
  1680 + return;
  1681 +
  1682 + s->bufptr = 0;
  1683 + while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1684 + qemu_chr_read(chr, &s->buf[s->bufptr], 1);
  1685 + s->bufptr++;
  1686 + s->max_size = qemu_chr_can_read(chr);
  1687 + }
  1688 +}
  1689 +
  1690 +static void udp_chr_update_read_handler(CharDriverState *chr)
  1691 +{
  1692 + NetCharDriver *s = chr->opaque;
  1693 +
  1694 + if (s->fd >= 0) {
  1695 + qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
  1696 + udp_chr_read, NULL, chr);
  1697 + }
  1698 +}
  1699 +
  1700 +static CharDriverState *qemu_chr_open_udp(const char *def)
  1701 +{
  1702 + CharDriverState *chr = NULL;
  1703 + NetCharDriver *s = NULL;
  1704 + int fd = -1;
  1705 + struct sockaddr_in saddr;
  1706 +
  1707 + chr = qemu_mallocz(sizeof(CharDriverState));
  1708 + if (!chr)
  1709 + goto return_err;
  1710 + s = qemu_mallocz(sizeof(NetCharDriver));
  1711 + if (!s)
  1712 + goto return_err;
  1713 +
  1714 + fd = socket(PF_INET, SOCK_DGRAM, 0);
  1715 + if (fd < 0) {
  1716 + perror("socket(PF_INET, SOCK_DGRAM)");
  1717 + goto return_err;
  1718 + }
  1719 +
  1720 + if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
  1721 + printf("Could not parse: %s\n", def);
  1722 + goto return_err;
  1723 + }
  1724 +
  1725 + if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
  1726 + {
  1727 + perror("bind");
  1728 + goto return_err;
  1729 + }
  1730 +
  1731 + s->fd = fd;
  1732 + s->bufcnt = 0;
  1733 + s->bufptr = 0;
  1734 + chr->opaque = s;
  1735 + chr->chr_write = udp_chr_write;
  1736 + chr->chr_update_read_handler = udp_chr_update_read_handler;
  1737 + return chr;
  1738 +
  1739 +return_err:
  1740 + if (chr)
  1741 + free(chr);
  1742 + if (s)
  1743 + free(s);
  1744 + if (fd >= 0)
  1745 + closesocket(fd);
  1746 + return NULL;
  1747 +}
  1748 +
  1749 +/***********************************************************/
  1750 +/* TCP Net console */
  1751 +
  1752 +typedef struct {
  1753 + int fd, listen_fd;
  1754 + int connected;
  1755 + int max_size;
  1756 + int do_telnetopt;
  1757 + int do_nodelay;
  1758 + int is_unix;
  1759 +} TCPCharDriver;
  1760 +
  1761 +static void tcp_chr_accept(void *opaque);
  1762 +
  1763 +static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1764 +{
  1765 + TCPCharDriver *s = chr->opaque;
  1766 + if (s->connected) {
  1767 + return send_all(s->fd, buf, len);
  1768 + } else {
  1769 + /* XXX: indicate an error ? */
  1770 + return len;
  1771 + }
  1772 +}
  1773 +
  1774 +static int tcp_chr_read_poll(void *opaque)
  1775 +{
  1776 + CharDriverState *chr = opaque;
  1777 + TCPCharDriver *s = chr->opaque;
  1778 + if (!s->connected)
  1779 + return 0;
  1780 + s->max_size = qemu_chr_can_read(chr);
  1781 + return s->max_size;
  1782 +}
  1783 +
  1784 +#define IAC 255
  1785 +#define IAC_BREAK 243
  1786 +static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
  1787 + TCPCharDriver *s,
  1788 + uint8_t *buf, int *size)
  1789 +{
  1790 + /* Handle any telnet client's basic IAC options to satisfy char by
  1791 + * char mode with no echo. All IAC options will be removed from
  1792 + * the buf and the do_telnetopt variable will be used to track the
  1793 + * state of the width of the IAC information.
  1794 + *
  1795 + * IAC commands come in sets of 3 bytes with the exception of the
  1796 + * "IAC BREAK" command and the double IAC.
  1797 + */
  1798 +
  1799 + int i;
  1800 + int j = 0;
  1801 +
  1802 + for (i = 0; i < *size; i++) {
  1803 + if (s->do_telnetopt > 1) {
  1804 + if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
  1805 + /* Double IAC means send an IAC */
  1806 + if (j != i)
  1807 + buf[j] = buf[i];
  1808 + j++;
  1809 + s->do_telnetopt = 1;
  1810 + } else {
  1811 + if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
  1812 + /* Handle IAC break commands by sending a serial break */
  1813 + qemu_chr_event(chr, CHR_EVENT_BREAK);
  1814 + s->do_telnetopt++;
  1815 + }
  1816 + s->do_telnetopt++;
  1817 + }
  1818 + if (s->do_telnetopt >= 4) {
  1819 + s->do_telnetopt = 1;
  1820 + }
  1821 + } else {
  1822 + if ((unsigned char)buf[i] == IAC) {
  1823 + s->do_telnetopt = 2;
  1824 + } else {
  1825 + if (j != i)
  1826 + buf[j] = buf[i];
  1827 + j++;
  1828 + }
  1829 + }
  1830 + }
  1831 + *size = j;
  1832 +}
  1833 +
  1834 +static void tcp_chr_read(void *opaque)
  1835 +{
  1836 + CharDriverState *chr = opaque;
  1837 + TCPCharDriver *s = chr->opaque;
  1838 + uint8_t buf[1024];
  1839 + int len, size;
  1840 +
  1841 + if (!s->connected || s->max_size <= 0)
  1842 + return;
  1843 + len = sizeof(buf);
  1844 + if (len > s->max_size)
  1845 + len = s->max_size;
  1846 + size = recv(s->fd, buf, len, 0);
  1847 + if (size == 0) {
  1848 + /* connection closed */
  1849 + s->connected = 0;
  1850 + if (s->listen_fd >= 0) {
  1851 + qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
  1852 + }
  1853 + qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  1854 + closesocket(s->fd);
  1855 + s->fd = -1;
  1856 + } else if (size > 0) {
  1857 + if (s->do_telnetopt)
  1858 + tcp_chr_process_IAC_bytes(chr, s, buf, &size);
  1859 + if (size > 0)
  1860 + qemu_chr_read(chr, buf, size);
  1861 + }
  1862 +}
  1863 +
  1864 +static void tcp_chr_connect(void *opaque)
  1865 +{
  1866 + CharDriverState *chr = opaque;
  1867 + TCPCharDriver *s = chr->opaque;
  1868 +
  1869 + s->connected = 1;
  1870 + qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
  1871 + tcp_chr_read, NULL, chr);
  1872 + qemu_chr_reset(chr);
  1873 +}
  1874 +
  1875 +#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
  1876 +static void tcp_chr_telnet_init(int fd)
  1877 +{
  1878 + char buf[3];
  1879 + /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
  1880 + IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
  1881 + send(fd, (char *)buf, 3, 0);
  1882 + IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
  1883 + send(fd, (char *)buf, 3, 0);
  1884 + IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
  1885 + send(fd, (char *)buf, 3, 0);
  1886 + IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
  1887 + send(fd, (char *)buf, 3, 0);
  1888 +}
  1889 +
  1890 +static void socket_set_nodelay(int fd)
  1891 +{
  1892 + int val = 1;
  1893 + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
  1894 +}
  1895 +
  1896 +static void tcp_chr_accept(void *opaque)
  1897 +{
  1898 + CharDriverState *chr = opaque;
  1899 + TCPCharDriver *s = chr->opaque;
  1900 + struct sockaddr_in saddr;
  1901 +#ifndef _WIN32
  1902 + struct sockaddr_un uaddr;
  1903 +#endif
  1904 + struct sockaddr *addr;
  1905 + socklen_t len;
  1906 + int fd;
  1907 +
  1908 + for(;;) {
  1909 +#ifndef _WIN32
  1910 + if (s->is_unix) {
  1911 + len = sizeof(uaddr);
  1912 + addr = (struct sockaddr *)&uaddr;
  1913 + } else
  1914 +#endif
  1915 + {
  1916 + len = sizeof(saddr);
  1917 + addr = (struct sockaddr *)&saddr;
  1918 + }
  1919 + fd = accept(s->listen_fd, addr, &len);
  1920 + if (fd < 0 && errno != EINTR) {
  1921 + return;
  1922 + } else if (fd >= 0) {
  1923 + if (s->do_telnetopt)
  1924 + tcp_chr_telnet_init(fd);
  1925 + break;
  1926 + }
  1927 + }
  1928 + socket_set_nonblock(fd);
  1929 + if (s->do_nodelay)
  1930 + socket_set_nodelay(fd);
  1931 + s->fd = fd;
  1932 + qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
  1933 + tcp_chr_connect(chr);
  1934 +}
  1935 +
  1936 +static void tcp_chr_close(CharDriverState *chr)
  1937 +{
  1938 + TCPCharDriver *s = chr->opaque;
  1939 + if (s->fd >= 0)
  1940 + closesocket(s->fd);
  1941 + if (s->listen_fd >= 0)
  1942 + closesocket(s->listen_fd);
  1943 + qemu_free(s);
  1944 +}
  1945 +
  1946 +static CharDriverState *qemu_chr_open_tcp(const char *host_str,
  1947 + int is_telnet,
  1948 + int is_unix)
  1949 +{
  1950 + CharDriverState *chr = NULL;
  1951 + TCPCharDriver *s = NULL;
  1952 + int fd = -1, ret, err, val;
  1953 + int is_listen = 0;
  1954 + int is_waitconnect = 1;
  1955 + int do_nodelay = 0;
  1956 + const char *ptr;
  1957 + struct sockaddr_in saddr;
  1958 +#ifndef _WIN32
  1959 + struct sockaddr_un uaddr;
  1960 +#endif
  1961 + struct sockaddr *addr;
  1962 + socklen_t addrlen;
  1963 +
  1964 +#ifndef _WIN32
  1965 + if (is_unix) {
  1966 + addr = (struct sockaddr *)&uaddr;
  1967 + addrlen = sizeof(uaddr);
  1968 + if (parse_unix_path(&uaddr, host_str) < 0)
  1969 + goto fail;
  1970 + } else
  1971 +#endif
  1972 + {
  1973 + addr = (struct sockaddr *)&saddr;
  1974 + addrlen = sizeof(saddr);
  1975 + if (parse_host_port(&saddr, host_str) < 0)
  1976 + goto fail;
  1977 + }
  1978 +
  1979 + ptr = host_str;
  1980 + while((ptr = strchr(ptr,','))) {
  1981 + ptr++;
  1982 + if (!strncmp(ptr,"server",6)) {
  1983 + is_listen = 1;
  1984 + } else if (!strncmp(ptr,"nowait",6)) {
  1985 + is_waitconnect = 0;
  1986 + } else if (!strncmp(ptr,"nodelay",6)) {
  1987 + do_nodelay = 1;
  1988 + } else {
  1989 + printf("Unknown option: %s\n", ptr);
  1990 + goto fail;
  1991 + }
  1992 + }
  1993 + if (!is_listen)
  1994 + is_waitconnect = 0;
  1995 +
  1996 + chr = qemu_mallocz(sizeof(CharDriverState));
  1997 + if (!chr)
  1998 + goto fail;
  1999 + s = qemu_mallocz(sizeof(TCPCharDriver));
  2000 + if (!s)
  2001 + goto fail;
  2002 +
  2003 +#ifndef _WIN32
  2004 + if (is_unix)
  2005 + fd = socket(PF_UNIX, SOCK_STREAM, 0);
  2006 + else
  2007 +#endif
  2008 + fd = socket(PF_INET, SOCK_STREAM, 0);
  2009 +
  2010 + if (fd < 0)
  2011 + goto fail;
  2012 +
  2013 + if (!is_waitconnect)
  2014 + socket_set_nonblock(fd);
  2015 +
  2016 + s->connected = 0;
  2017 + s->fd = -1;
  2018 + s->listen_fd = -1;
  2019 + s->is_unix = is_unix;
  2020 + s->do_nodelay = do_nodelay && !is_unix;
  2021 +
  2022 + chr->opaque = s;
  2023 + chr->chr_write = tcp_chr_write;
  2024 + chr->chr_close = tcp_chr_close;
  2025 +
  2026 + if (is_listen) {
  2027 + /* allow fast reuse */
  2028 +#ifndef _WIN32
  2029 + if (is_unix) {
  2030 + char path[109];
  2031 + pstrcpy(path, sizeof(path), uaddr.sun_path);
  2032 + unlink(path);
  2033 + } else
  2034 +#endif
  2035 + {
  2036 + val = 1;
  2037 + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
  2038 + }
  2039 +
  2040 + ret = bind(fd, addr, addrlen);
  2041 + if (ret < 0)
  2042 + goto fail;
  2043 +
  2044 + ret = listen(fd, 0);
  2045 + if (ret < 0)
  2046 + goto fail;
  2047 +
  2048 + s->listen_fd = fd;
  2049 + qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
  2050 + if (is_telnet)
  2051 + s->do_telnetopt = 1;
  2052 + } else {
  2053 + for(;;) {
  2054 + ret = connect(fd, addr, addrlen);
  2055 + if (ret < 0) {
  2056 + err = socket_error();
  2057 + if (err == EINTR || err == EWOULDBLOCK) {
  2058 + } else if (err == EINPROGRESS) {
  2059 + break;
  2060 +#ifdef _WIN32
  2061 + } else if (err == WSAEALREADY) {
  2062 + break;
  2063 +#endif
  2064 + } else {
  2065 + goto fail;
  2066 + }
  2067 + } else {
  2068 + s->connected = 1;
  2069 + break;
  2070 + }
  2071 + }
  2072 + s->fd = fd;
  2073 + socket_set_nodelay(fd);
  2074 + if (s->connected)
  2075 + tcp_chr_connect(chr);
  2076 + else
  2077 + qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
  2078 + }
  2079 +
  2080 + if (is_listen && is_waitconnect) {
  2081 + printf("QEMU waiting for connection on: %s\n", host_str);
  2082 + tcp_chr_accept(chr);
  2083 + socket_set_nonblock(s->listen_fd);
  2084 + }
  2085 +
  2086 + return chr;
  2087 + fail:
  2088 + if (fd >= 0)
  2089 + closesocket(fd);
  2090 + qemu_free(s);
  2091 + qemu_free(chr);
  2092 + return NULL;
  2093 +}
  2094 +
  2095 +static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
  2096 += TAILQ_HEAD_INITIALIZER(chardevs);
  2097 +
  2098 +CharDriverState *qemu_chr_open(const char *label, const char *filename)
  2099 +{
  2100 + const char *p;
  2101 + CharDriverState *chr;
  2102 +
  2103 + if (!strcmp(filename, "vc")) {
  2104 + chr = text_console_init(&display_state, 0);
  2105 + } else
  2106 + if (strstart(filename, "vc:", &p)) {
  2107 + chr = text_console_init(&display_state, p);
  2108 + } else
  2109 + if (!strcmp(filename, "null")) {
  2110 + chr = qemu_chr_open_null();
  2111 + } else
  2112 + if (strstart(filename, "tcp:", &p)) {
  2113 + chr = qemu_chr_open_tcp(p, 0, 0);
  2114 + } else
  2115 + if (strstart(filename, "telnet:", &p)) {
  2116 + chr = qemu_chr_open_tcp(p, 1, 0);
  2117 + } else
  2118 + if (strstart(filename, "udp:", &p)) {
  2119 + chr = qemu_chr_open_udp(p);
  2120 + } else
  2121 + if (strstart(filename, "mon:", &p)) {
  2122 + chr = qemu_chr_open(label, p);
  2123 + if (chr) {
  2124 + chr = qemu_chr_open_mux(chr);
  2125 + monitor_init(chr, !nographic);
  2126 + } else {
  2127 + printf("Unable to open driver: %s\n", p);
  2128 + }
  2129 + } else
  2130 +#ifndef _WIN32
  2131 + if (strstart(filename, "unix:", &p)) {
  2132 + chr = qemu_chr_open_tcp(p, 0, 1);
  2133 + } else if (strstart(filename, "file:", &p)) {
  2134 + chr = qemu_chr_open_file_out(p);
  2135 + } else if (strstart(filename, "pipe:", &p)) {
  2136 + chr = qemu_chr_open_pipe(p);
  2137 + } else if (!strcmp(filename, "pty")) {
  2138 + chr = qemu_chr_open_pty();
  2139 + } else if (!strcmp(filename, "stdio")) {
  2140 + chr = qemu_chr_open_stdio();
  2141 + } else
  2142 +#if defined(__linux__)
  2143 + if (strstart(filename, "/dev/parport", NULL)) {
  2144 + chr = qemu_chr_open_pp(filename);
  2145 + } else
  2146 +#endif
  2147 +#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  2148 + || defined(__NetBSD__) || defined(__OpenBSD__)
  2149 + if (strstart(filename, "/dev/", NULL)) {
  2150 + chr = qemu_chr_open_tty(filename);
  2151 + } else
  2152 +#endif
  2153 +#else /* !_WIN32 */
  2154 + if (strstart(filename, "COM", NULL)) {
  2155 + chr = qemu_chr_open_win(filename);
  2156 + } else
  2157 + if (strstart(filename, "pipe:", &p)) {
  2158 + chr = qemu_chr_open_win_pipe(p);
  2159 + } else
  2160 + if (strstart(filename, "con:", NULL)) {
  2161 + chr = qemu_chr_open_win_con(filename);
  2162 + } else
  2163 + if (strstart(filename, "file:", &p)) {
  2164 + chr = qemu_chr_open_win_file_out(p);
  2165 + } else
  2166 +#endif
  2167 +#ifdef CONFIG_BRLAPI
  2168 + if (!strcmp(filename, "braille")) {
  2169 + chr = chr_baum_init();
  2170 + } else
  2171 +#endif
  2172 + {
  2173 + chr = NULL;
  2174 + }
  2175 +
  2176 + if (chr) {
  2177 + if (!chr->filename)
  2178 + chr->filename = qemu_strdup(filename);
  2179 + chr->label = qemu_strdup(label);
  2180 + TAILQ_INSERT_TAIL(&chardevs, chr, next);
  2181 + }
  2182 + return chr;
  2183 +}
  2184 +
  2185 +void qemu_chr_close(CharDriverState *chr)
  2186 +{
  2187 + TAILQ_REMOVE(&chardevs, chr, next);
  2188 + if (chr->chr_close)
  2189 + chr->chr_close(chr);
  2190 + qemu_free(chr->filename);
  2191 + qemu_free(chr->label);
  2192 + qemu_free(chr);
  2193 +}
  2194 +
  2195 +void qemu_chr_info(void)
  2196 +{
  2197 + CharDriverState *chr;
  2198 +
  2199 + TAILQ_FOREACH(chr, &chardevs, next) {
  2200 + term_printf("%s: filename=%s\n", chr->label, chr->filename);
  2201 + }
  2202 +}
... ...
... ... @@ -1735,354 +1735,7 @@ int qemu_timedate_diff(struct tm *tm)
1735 1735 return seconds - time(NULL);
1736 1736 }
1737 1737  
1738   -/***********************************************************/
1739   -/* character device */
1740   -
1741   -static void qemu_chr_event(CharDriverState *s, int event)
1742   -{
1743   - if (!s->chr_event)
1744   - return;
1745   - s->chr_event(s->handler_opaque, event);
1746   -}
1747   -
1748   -static void qemu_chr_reset_bh(void *opaque)
1749   -{
1750   - CharDriverState *s = opaque;
1751   - qemu_chr_event(s, CHR_EVENT_RESET);
1752   - qemu_bh_delete(s->bh);
1753   - s->bh = NULL;
1754   -}
1755   -
1756   -void qemu_chr_reset(CharDriverState *s)
1757   -{
1758   - if (s->bh == NULL) {
1759   - s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
1760   - qemu_bh_schedule(s->bh);
1761   - }
1762   -}
1763   -
1764   -int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
1765   -{
1766   - return s->chr_write(s, buf, len);
1767   -}
1768   -
1769   -int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
1770   -{
1771   - if (!s->chr_ioctl)
1772   - return -ENOTSUP;
1773   - return s->chr_ioctl(s, cmd, arg);
1774   -}
1775   -
1776   -int qemu_chr_can_read(CharDriverState *s)
1777   -{
1778   - if (!s->chr_can_read)
1779   - return 0;
1780   - return s->chr_can_read(s->handler_opaque);
1781   -}
1782   -
1783   -void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
1784   -{
1785   - s->chr_read(s->handler_opaque, buf, len);
1786   -}
1787   -
1788   -void qemu_chr_accept_input(CharDriverState *s)
1789   -{
1790   - if (s->chr_accept_input)
1791   - s->chr_accept_input(s);
1792   -}
1793   -
1794   -void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
1795   -{
1796   - char buf[4096];
1797   - va_list ap;
1798   - va_start(ap, fmt);
1799   - vsnprintf(buf, sizeof(buf), fmt, ap);
1800   - qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
1801   - va_end(ap);
1802   -}
1803   -
1804   -void qemu_chr_send_event(CharDriverState *s, int event)
1805   -{
1806   - if (s->chr_send_event)
1807   - s->chr_send_event(s, event);
1808   -}
1809   -
1810   -void qemu_chr_add_handlers(CharDriverState *s,
1811   - IOCanRWHandler *fd_can_read,
1812   - IOReadHandler *fd_read,
1813   - IOEventHandler *fd_event,
1814   - void *opaque)
1815   -{
1816   - s->chr_can_read = fd_can_read;
1817   - s->chr_read = fd_read;
1818   - s->chr_event = fd_event;
1819   - s->handler_opaque = opaque;
1820   - if (s->chr_update_read_handler)
1821   - s->chr_update_read_handler(s);
1822   -}
1823   -
1824   -static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1825   -{
1826   - return len;
1827   -}
1828   -
1829   -static CharDriverState *qemu_chr_open_null(void)
1830   -{
1831   - CharDriverState *chr;
1832   -
1833   - chr = qemu_mallocz(sizeof(CharDriverState));
1834   - if (!chr)
1835   - return NULL;
1836   - chr->chr_write = null_chr_write;
1837   - return chr;
1838   -}
1839   -
1840   -/* MUX driver for serial I/O splitting */
1841   -static int term_timestamps;
1842   -static int64_t term_timestamps_start;
1843   -#define MAX_MUX 4
1844   -#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
1845   -#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
1846   -typedef struct {
1847   - IOCanRWHandler *chr_can_read[MAX_MUX];
1848   - IOReadHandler *chr_read[MAX_MUX];
1849   - IOEventHandler *chr_event[MAX_MUX];
1850   - void *ext_opaque[MAX_MUX];
1851   - CharDriverState *drv;
1852   - unsigned char buffer[MUX_BUFFER_SIZE];
1853   - int prod;
1854   - int cons;
1855   - int mux_cnt;
1856   - int term_got_escape;
1857   - int max_size;
1858   -} MuxDriver;
1859   -
1860   -
1861   -static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1862   -{
1863   - MuxDriver *d = chr->opaque;
1864   - int ret;
1865   - if (!term_timestamps) {
1866   - ret = d->drv->chr_write(d->drv, buf, len);
1867   - } else {
1868   - int i;
1869   -
1870   - ret = 0;
1871   - for(i = 0; i < len; i++) {
1872   - ret += d->drv->chr_write(d->drv, buf+i, 1);
1873   - if (buf[i] == '\n') {
1874   - char buf1[64];
1875   - int64_t ti;
1876   - int secs;
1877   -
1878   - ti = qemu_get_clock(rt_clock);
1879   - if (term_timestamps_start == -1)
1880   - term_timestamps_start = ti;
1881   - ti -= term_timestamps_start;
1882   - secs = ti / 1000000000;
1883   - snprintf(buf1, sizeof(buf1),
1884   - "[%02d:%02d:%02d.%03d] ",
1885   - secs / 3600,
1886   - (secs / 60) % 60,
1887   - secs % 60,
1888   - (int)((ti / 1000000) % 1000));
1889   - d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
1890   - }
1891   - }
1892   - }
1893   - return ret;
1894   -}
1895   -
1896   -static const char * const mux_help[] = {
1897   - "% h print this help\n\r",
1898   - "% x exit emulator\n\r",
1899   - "% s save disk data back to file (if -snapshot)\n\r",
1900   - "% t toggle console timestamps\n\r"
1901   - "% b send break (magic sysrq)\n\r",
1902   - "% c switch between console and monitor\n\r",
1903   - "% % sends %\n\r",
1904   - NULL
1905   -};
1906   -
1907   -int term_escape_char = 0x01; /* ctrl-a is used for escape */
1908   -static void mux_print_help(CharDriverState *chr)
1909   -{
1910   - int i, j;
1911   - char ebuf[15] = "Escape-Char";
1912   - char cbuf[50] = "\n\r";
1913   -
1914   - if (term_escape_char > 0 && term_escape_char < 26) {
1915   - snprintf(cbuf, sizeof(cbuf), "\n\r");
1916   - snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
1917   - } else {
1918   - snprintf(cbuf, sizeof(cbuf),
1919   - "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
1920   - term_escape_char);
1921   - }
1922   - chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
1923   - for (i = 0; mux_help[i] != NULL; i++) {
1924   - for (j=0; mux_help[i][j] != '\0'; j++) {
1925   - if (mux_help[i][j] == '%')
1926   - chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
1927   - else
1928   - chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
1929   - }
1930   - }
1931   -}
1932   -
1933   -static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
1934   -{
1935   - if (d->term_got_escape) {
1936   - d->term_got_escape = 0;
1937   - if (ch == term_escape_char)
1938   - goto send_char;
1939   - switch(ch) {
1940   - case '?':
1941   - case 'h':
1942   - mux_print_help(chr);
1943   - break;
1944   - case 'x':
1945   - {
1946   - const char *term = "QEMU: Terminated\n\r";
1947   - chr->chr_write(chr,(uint8_t *)term,strlen(term));
1948   - exit(0);
1949   - break;
1950   - }
1951   - case 's':
1952   - {
1953   - int i;
1954   - for (i = 0; i < nb_drives; i++) {
1955   - bdrv_commit(drives_table[i].bdrv);
1956   - }
1957   - }
1958   - break;
1959   - case 'b':
1960   - qemu_chr_event(chr, CHR_EVENT_BREAK);
1961   - break;
1962   - case 'c':
1963   - /* Switch to the next registered device */
1964   - chr->focus++;
1965   - if (chr->focus >= d->mux_cnt)
1966   - chr->focus = 0;
1967   - break;
1968   - case 't':
1969   - term_timestamps = !term_timestamps;
1970   - term_timestamps_start = -1;
1971   - break;
1972   - }
1973   - } else if (ch == term_escape_char) {
1974   - d->term_got_escape = 1;
1975   - } else {
1976   - send_char:
1977   - return 1;
1978   - }
1979   - return 0;
1980   -}
1981   -
1982   -static void mux_chr_accept_input(CharDriverState *chr)
1983   -{
1984   - int m = chr->focus;
1985   - MuxDriver *d = chr->opaque;
1986   -
1987   - while (d->prod != d->cons &&
1988   - d->chr_can_read[m] &&
1989   - d->chr_can_read[m](d->ext_opaque[m])) {
1990   - d->chr_read[m](d->ext_opaque[m],
1991   - &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
1992   - }
1993   -}
1994   -
1995   -static int mux_chr_can_read(void *opaque)
1996   -{
1997   - CharDriverState *chr = opaque;
1998   - MuxDriver *d = chr->opaque;
1999   -
2000   - if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
2001   - return 1;
2002   - if (d->chr_can_read[chr->focus])
2003   - return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
2004   - return 0;
2005   -}
2006   -
2007   -static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
2008   -{
2009   - CharDriverState *chr = opaque;
2010   - MuxDriver *d = chr->opaque;
2011   - int m = chr->focus;
2012   - int i;
2013   -
2014   - mux_chr_accept_input (opaque);
2015   -
2016   - for(i = 0; i < size; i++)
2017   - if (mux_proc_byte(chr, d, buf[i])) {
2018   - if (d->prod == d->cons &&
2019   - d->chr_can_read[m] &&
2020   - d->chr_can_read[m](d->ext_opaque[m]))
2021   - d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
2022   - else
2023   - d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
2024   - }
2025   -}
2026   -
2027   -static void mux_chr_event(void *opaque, int event)
2028   -{
2029   - CharDriverState *chr = opaque;
2030   - MuxDriver *d = chr->opaque;
2031   - int i;
2032   -
2033   - /* Send the event to all registered listeners */
2034   - for (i = 0; i < d->mux_cnt; i++)
2035   - if (d->chr_event[i])
2036   - d->chr_event[i](d->ext_opaque[i], event);
2037   -}
2038   -
2039   -static void mux_chr_update_read_handler(CharDriverState *chr)
2040   -{
2041   - MuxDriver *d = chr->opaque;
2042   -
2043   - if (d->mux_cnt >= MAX_MUX) {
2044   - fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
2045   - return;
2046   - }
2047   - d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
2048   - d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
2049   - d->chr_read[d->mux_cnt] = chr->chr_read;
2050   - d->chr_event[d->mux_cnt] = chr->chr_event;
2051   - /* Fix up the real driver with mux routines */
2052   - if (d->mux_cnt == 0) {
2053   - qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
2054   - mux_chr_event, chr);
2055   - }
2056   - chr->focus = d->mux_cnt;
2057   - d->mux_cnt++;
2058   -}
2059   -
2060   -static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
2061   -{
2062   - CharDriverState *chr;
2063   - MuxDriver *d;
2064   -
2065   - chr = qemu_mallocz(sizeof(CharDriverState));
2066   - if (!chr)
2067   - return NULL;
2068   - d = qemu_mallocz(sizeof(MuxDriver));
2069   - if (!d) {
2070   - free(chr);
2071   - return NULL;
2072   - }
2073   -
2074   - chr->opaque = d;
2075   - d->drv = drv;
2076   - chr->focus = -1;
2077   - chr->chr_write = mux_chr_write;
2078   - chr->chr_update_read_handler = mux_chr_update_read_handler;
2079   - chr->chr_accept_input = mux_chr_accept_input;
2080   - return chr;
2081   -}
2082   -
2083   -
2084 1738 #ifdef _WIN32
2085   -
2086 1739 static void socket_cleanup(void)
2087 1740 {
2088 1741 WSACleanup();
... ... @@ -2102,1776 +1755,11 @@ static int socket_init(void)
2102 1755 atexit(socket_cleanup);
2103 1756 return 0;
2104 1757 }
  1758 +#endif
2105 1759  
2106   -int send_all(int fd, const uint8_t *buf, int len1)
2107   -{
2108   - int ret, len;
2109   -
2110   - len = len1;
2111   - while (len > 0) {
2112   - ret = send(fd, buf, len, 0);
2113   - if (ret < 0) {
2114   - int errno;
2115   - errno = WSAGetLastError();
2116   - if (errno != WSAEWOULDBLOCK) {
2117   - return -1;
2118   - }
2119   - } else if (ret == 0) {
2120   - break;
2121   - } else {
2122   - buf += ret;
2123   - len -= ret;
2124   - }
2125   - }
2126   - return len1 - len;
2127   -}
2128   -
2129   -#else
2130   -
2131   -static int unix_write(int fd, const uint8_t *buf, int len1)
2132   -{
2133   - int ret, len;
2134   -
2135   - len = len1;
2136   - while (len > 0) {
2137   - ret = write(fd, buf, len);
2138   - if (ret < 0) {
2139   - if (errno != EINTR && errno != EAGAIN)
2140   - return -1;
2141   - } else if (ret == 0) {
2142   - break;
2143   - } else {
2144   - buf += ret;
2145   - len -= ret;
2146   - }
2147   - }
2148   - return len1 - len;
2149   -}
2150   -
2151   -inline int send_all(int fd, const uint8_t *buf, int len1)
2152   -{
2153   - return unix_write(fd, buf, len1);
2154   -}
2155   -#endif /* !_WIN32 */
2156   -
2157   -#ifndef _WIN32
2158   -
2159   -typedef struct {
2160   - int fd_in, fd_out;
2161   - int max_size;
2162   -} FDCharDriver;
2163   -
2164   -#define STDIO_MAX_CLIENTS 1
2165   -static int stdio_nb_clients = 0;
2166   -
2167   -static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2168   -{
2169   - FDCharDriver *s = chr->opaque;
2170   - return send_all(s->fd_out, buf, len);
2171   -}
2172   -
2173   -static int fd_chr_read_poll(void *opaque)
2174   -{
2175   - CharDriverState *chr = opaque;
2176   - FDCharDriver *s = chr->opaque;
2177   -
2178   - s->max_size = qemu_chr_can_read(chr);
2179   - return s->max_size;
2180   -}
2181   -
2182   -static void fd_chr_read(void *opaque)
2183   -{
2184   - CharDriverState *chr = opaque;
2185   - FDCharDriver *s = chr->opaque;
2186   - int size, len;
2187   - uint8_t buf[1024];
2188   -
2189   - len = sizeof(buf);
2190   - if (len > s->max_size)
2191   - len = s->max_size;
2192   - if (len == 0)
2193   - return;
2194   - size = read(s->fd_in, buf, len);
2195   - if (size == 0) {
2196   - /* FD has been closed. Remove it from the active list. */
2197   - qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2198   - return;
2199   - }
2200   - if (size > 0) {
2201   - qemu_chr_read(chr, buf, size);
2202   - }
2203   -}
2204   -
2205   -static void fd_chr_update_read_handler(CharDriverState *chr)
2206   -{
2207   - FDCharDriver *s = chr->opaque;
2208   -
2209   - if (s->fd_in >= 0) {
2210   - if (nographic && s->fd_in == 0) {
2211   - } else {
2212   - qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
2213   - fd_chr_read, NULL, chr);
2214   - }
2215   - }
2216   -}
2217   -
2218   -static void fd_chr_close(struct CharDriverState *chr)
2219   -{
2220   - FDCharDriver *s = chr->opaque;
2221   -
2222   - if (s->fd_in >= 0) {
2223   - if (nographic && s->fd_in == 0) {
2224   - } else {
2225   - qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2226   - }
2227   - }
2228   -
2229   - qemu_free(s);
2230   -}
2231   -
2232   -/* open a character device to a unix fd */
2233   -static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
2234   -{
2235   - CharDriverState *chr;
2236   - FDCharDriver *s;
2237   -
2238   - chr = qemu_mallocz(sizeof(CharDriverState));
2239   - if (!chr)
2240   - return NULL;
2241   - s = qemu_mallocz(sizeof(FDCharDriver));
2242   - if (!s) {
2243   - free(chr);
2244   - return NULL;
2245   - }
2246   - s->fd_in = fd_in;
2247   - s->fd_out = fd_out;
2248   - chr->opaque = s;
2249   - chr->chr_write = fd_chr_write;
2250   - chr->chr_update_read_handler = fd_chr_update_read_handler;
2251   - chr->chr_close = fd_chr_close;
2252   -
2253   - qemu_chr_reset(chr);
2254   -
2255   - return chr;
2256   -}
2257   -
2258   -static CharDriverState *qemu_chr_open_file_out(const char *file_out)
2259   -{
2260   - int fd_out;
2261   -
2262   - TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
2263   - if (fd_out < 0)
2264   - return NULL;
2265   - return qemu_chr_open_fd(-1, fd_out);
2266   -}
2267   -
2268   -static CharDriverState *qemu_chr_open_pipe(const char *filename)
2269   -{
2270   - int fd_in, fd_out;
2271   - char filename_in[256], filename_out[256];
2272   -
2273   - snprintf(filename_in, 256, "%s.in", filename);
2274   - snprintf(filename_out, 256, "%s.out", filename);
2275   - TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2276   - TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
2277   - if (fd_in < 0 || fd_out < 0) {
2278   - if (fd_in >= 0)
2279   - close(fd_in);
2280   - if (fd_out >= 0)
2281   - close(fd_out);
2282   - TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
2283   - if (fd_in < 0)
2284   - return NULL;
2285   - }
2286   - return qemu_chr_open_fd(fd_in, fd_out);
2287   -}
2288   -
2289   -
2290   -/* for STDIO, we handle the case where several clients use it
2291   - (nographic mode) */
2292   -
2293   -#define TERM_FIFO_MAX_SIZE 1
2294   -
2295   -static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
2296   -static int term_fifo_size;
2297   -
2298   -static int stdio_read_poll(void *opaque)
2299   -{
2300   - CharDriverState *chr = opaque;
2301   -
2302   - /* try to flush the queue if needed */
2303   - if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2304   - qemu_chr_read(chr, term_fifo, 1);
2305   - term_fifo_size = 0;
2306   - }
2307   - /* see if we can absorb more chars */
2308   - if (term_fifo_size == 0)
2309   - return 1;
2310   - else
2311   - return 0;
2312   -}
2313   -
2314   -static void stdio_read(void *opaque)
2315   -{
2316   - int size;
2317   - uint8_t buf[1];
2318   - CharDriverState *chr = opaque;
2319   -
2320   - size = read(0, buf, 1);
2321   - if (size == 0) {
2322   - /* stdin has been closed. Remove it from the active list. */
2323   - qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2324   - return;
2325   - }
2326   - if (size > 0) {
2327   - if (qemu_chr_can_read(chr) > 0) {
2328   - qemu_chr_read(chr, buf, 1);
2329   - } else if (term_fifo_size == 0) {
2330   - term_fifo[term_fifo_size++] = buf[0];
2331   - }
2332   - }
2333   -}
2334   -
2335   -/* init terminal so that we can grab keys */
2336   -static struct termios oldtty;
2337   -static int old_fd0_flags;
2338   -static int term_atexit_done;
2339   -
2340   -static void term_exit(void)
2341   -{
2342   - tcsetattr (0, TCSANOW, &oldtty);
2343   - fcntl(0, F_SETFL, old_fd0_flags);
2344   -}
2345   -
2346   -static void term_init(void)
2347   -{
2348   - struct termios tty;
2349   -
2350   - tcgetattr (0, &tty);
2351   - oldtty = tty;
2352   - old_fd0_flags = fcntl(0, F_GETFL);
2353   -
2354   - tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2355   - |INLCR|IGNCR|ICRNL|IXON);
2356   - tty.c_oflag |= OPOST;
2357   - tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2358   - /* if graphical mode, we allow Ctrl-C handling */
2359   - if (nographic)
2360   - tty.c_lflag &= ~ISIG;
2361   - tty.c_cflag &= ~(CSIZE|PARENB);
2362   - tty.c_cflag |= CS8;
2363   - tty.c_cc[VMIN] = 1;
2364   - tty.c_cc[VTIME] = 0;
2365   -
2366   - tcsetattr (0, TCSANOW, &tty);
2367   -
2368   - if (!term_atexit_done++)
2369   - atexit(term_exit);
2370   -
2371   - fcntl(0, F_SETFL, O_NONBLOCK);
2372   -}
2373   -
2374   -static void qemu_chr_close_stdio(struct CharDriverState *chr)
2375   -{
2376   - term_exit();
2377   - stdio_nb_clients--;
2378   - qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2379   - fd_chr_close(chr);
2380   -}
2381   -
2382   -static CharDriverState *qemu_chr_open_stdio(void)
2383   -{
2384   - CharDriverState *chr;
2385   -
2386   - if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2387   - return NULL;
2388   - chr = qemu_chr_open_fd(0, 1);
2389   - chr->chr_close = qemu_chr_close_stdio;
2390   - qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2391   - stdio_nb_clients++;
2392   - term_init();
2393   -
2394   - return chr;
2395   -}
  1760 +/***********************************************************/
  1761 +/* network device redirectors */
2396 1762  
2397   -#ifdef __sun__
2398   -/* Once Solaris has openpty(), this is going to be removed. */
2399   -int openpty(int *amaster, int *aslave, char *name,
2400   - struct termios *termp, struct winsize *winp)
2401   -{
2402   - const char *slave;
2403   - int mfd = -1, sfd = -1;
2404   -
2405   - *amaster = *aslave = -1;
2406   -
2407   - mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
2408   - if (mfd < 0)
2409   - goto err;
2410   -
2411   - if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
2412   - goto err;
2413   -
2414   - if ((slave = ptsname(mfd)) == NULL)
2415   - goto err;
2416   -
2417   - if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
2418   - goto err;
2419   -
2420   - if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
2421   - (termp != NULL && tcgetattr(sfd, termp) < 0))
2422   - goto err;
2423   -
2424   - if (amaster)
2425   - *amaster = mfd;
2426   - if (aslave)
2427   - *aslave = sfd;
2428   - if (winp)
2429   - ioctl(sfd, TIOCSWINSZ, winp);
2430   -
2431   - return 0;
2432   -
2433   -err:
2434   - if (sfd != -1)
2435   - close(sfd);
2436   - close(mfd);
2437   - return -1;
2438   -}
2439   -
2440   -void cfmakeraw (struct termios *termios_p)
2441   -{
2442   - termios_p->c_iflag &=
2443   - ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
2444   - termios_p->c_oflag &= ~OPOST;
2445   - termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
2446   - termios_p->c_cflag &= ~(CSIZE|PARENB);
2447   - termios_p->c_cflag |= CS8;
2448   -
2449   - termios_p->c_cc[VMIN] = 0;
2450   - termios_p->c_cc[VTIME] = 0;
2451   -}
2452   -#endif
2453   -
2454   -#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2455   - || defined(__NetBSD__) || defined(__OpenBSD__)
2456   -
2457   -typedef struct {
2458   - int fd;
2459   - int connected;
2460   - int polling;
2461   - int read_bytes;
2462   - QEMUTimer *timer;
2463   -} PtyCharDriver;
2464   -
2465   -static void pty_chr_update_read_handler(CharDriverState *chr);
2466   -static void pty_chr_state(CharDriverState *chr, int connected);
2467   -
2468   -static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2469   -{
2470   - PtyCharDriver *s = chr->opaque;
2471   -
2472   - if (!s->connected) {
2473   - /* guest sends data, check for (re-)connect */
2474   - pty_chr_update_read_handler(chr);
2475   - return 0;
2476   - }
2477   - return send_all(s->fd, buf, len);
2478   -}
2479   -
2480   -static int pty_chr_read_poll(void *opaque)
2481   -{
2482   - CharDriverState *chr = opaque;
2483   - PtyCharDriver *s = chr->opaque;
2484   -
2485   - s->read_bytes = qemu_chr_can_read(chr);
2486   - return s->read_bytes;
2487   -}
2488   -
2489   -static void pty_chr_read(void *opaque)
2490   -{
2491   - CharDriverState *chr = opaque;
2492   - PtyCharDriver *s = chr->opaque;
2493   - int size, len;
2494   - uint8_t buf[1024];
2495   -
2496   - len = sizeof(buf);
2497   - if (len > s->read_bytes)
2498   - len = s->read_bytes;
2499   - if (len == 0)
2500   - return;
2501   - size = read(s->fd, buf, len);
2502   - if ((size == -1 && errno == EIO) ||
2503   - (size == 0)) {
2504   - pty_chr_state(chr, 0);
2505   - return;
2506   - }
2507   - if (size > 0) {
2508   - pty_chr_state(chr, 1);
2509   - qemu_chr_read(chr, buf, size);
2510   - }
2511   -}
2512   -
2513   -static void pty_chr_update_read_handler(CharDriverState *chr)
2514   -{
2515   - PtyCharDriver *s = chr->opaque;
2516   -
2517   - qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
2518   - pty_chr_read, NULL, chr);
2519   - s->polling = 1;
2520   - /*
2521   - * Short timeout here: just need wait long enougth that qemu makes
2522   - * it through the poll loop once. When reconnected we want a
2523   - * short timeout so we notice it almost instantly. Otherwise
2524   - * read() gives us -EIO instantly, making pty_chr_state() reset the
2525   - * timeout to the normal (much longer) poll interval before the
2526   - * timer triggers.
2527   - */
2528   - qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
2529   -}
2530   -
2531   -static void pty_chr_state(CharDriverState *chr, int connected)
2532   -{
2533   - PtyCharDriver *s = chr->opaque;
2534   -
2535   - if (!connected) {
2536   - qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
2537   - s->connected = 0;
2538   - s->polling = 0;
2539   - /* (re-)connect poll interval for idle guests: once per second.
2540   - * We check more frequently in case the guests sends data to
2541   - * the virtual device linked to our pty. */
2542   - qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
2543   - } else {
2544   - if (!s->connected)
2545   - qemu_chr_reset(chr);
2546   - s->connected = 1;
2547   - }
2548   -}
2549   -
2550   -static void pty_chr_timer(void *opaque)
2551   -{
2552   - struct CharDriverState *chr = opaque;
2553   - PtyCharDriver *s = chr->opaque;
2554   -
2555   - if (s->connected)
2556   - return;
2557   - if (s->polling) {
2558   - /* If we arrive here without polling being cleared due
2559   - * read returning -EIO, then we are (re-)connected */
2560   - pty_chr_state(chr, 1);
2561   - return;
2562   - }
2563   -
2564   - /* Next poll ... */
2565   - pty_chr_update_read_handler(chr);
2566   -}
2567   -
2568   -static void pty_chr_close(struct CharDriverState *chr)
2569   -{
2570   - PtyCharDriver *s = chr->opaque;
2571   -
2572   - qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
2573   - close(s->fd);
2574   - qemu_free(s);
2575   -}
2576   -
2577   -static CharDriverState *qemu_chr_open_pty(void)
2578   -{
2579   - CharDriverState *chr;
2580   - PtyCharDriver *s;
2581   - struct termios tty;
2582   - int slave_fd, len;
2583   -#if defined(__OpenBSD__)
2584   - char pty_name[PATH_MAX];
2585   -#define q_ptsname(x) pty_name
2586   -#else
2587   - char *pty_name = NULL;
2588   -#define q_ptsname(x) ptsname(x)
2589   -#endif
2590   -
2591   - chr = qemu_mallocz(sizeof(CharDriverState));
2592   - if (!chr)
2593   - return NULL;
2594   - s = qemu_mallocz(sizeof(PtyCharDriver));
2595   - if (!s) {
2596   - qemu_free(chr);
2597   - return NULL;
2598   - }
2599   -
2600   - if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
2601   - return NULL;
2602   - }
2603   -
2604   - /* Set raw attributes on the pty. */
2605   - cfmakeraw(&tty);
2606   - tcsetattr(slave_fd, TCSAFLUSH, &tty);
2607   - close(slave_fd);
2608   -
2609   - len = strlen(q_ptsname(s->fd)) + 5;
2610   - chr->filename = qemu_malloc(len);
2611   - snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
2612   - fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
2613   -
2614   - chr->opaque = s;
2615   - chr->chr_write = pty_chr_write;
2616   - chr->chr_update_read_handler = pty_chr_update_read_handler;
2617   - chr->chr_close = pty_chr_close;
2618   -
2619   - s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
2620   -
2621   - return chr;
2622   -}
2623   -
2624   -static void tty_serial_init(int fd, int speed,
2625   - int parity, int data_bits, int stop_bits)
2626   -{
2627   - struct termios tty;
2628   - speed_t spd;
2629   -
2630   -#if 0
2631   - printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2632   - speed, parity, data_bits, stop_bits);
2633   -#endif
2634   - tcgetattr (fd, &tty);
2635   -
2636   -#define MARGIN 1.1
2637   - if (speed <= 50 * MARGIN)
2638   - spd = B50;
2639   - else if (speed <= 75 * MARGIN)
2640   - spd = B75;
2641   - else if (speed <= 300 * MARGIN)
2642   - spd = B300;
2643   - else if (speed <= 600 * MARGIN)
2644   - spd = B600;
2645   - else if (speed <= 1200 * MARGIN)
2646   - spd = B1200;
2647   - else if (speed <= 2400 * MARGIN)
2648   - spd = B2400;
2649   - else if (speed <= 4800 * MARGIN)
2650   - spd = B4800;
2651   - else if (speed <= 9600 * MARGIN)
2652   - spd = B9600;
2653   - else if (speed <= 19200 * MARGIN)
2654   - spd = B19200;
2655   - else if (speed <= 38400 * MARGIN)
2656   - spd = B38400;
2657   - else if (speed <= 57600 * MARGIN)
2658   - spd = B57600;
2659   - else if (speed <= 115200 * MARGIN)
2660   - spd = B115200;
2661   - else
2662   - spd = B115200;
2663   -
2664   - cfsetispeed(&tty, spd);
2665   - cfsetospeed(&tty, spd);
2666   -
2667   - tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2668   - |INLCR|IGNCR|ICRNL|IXON);
2669   - tty.c_oflag |= OPOST;
2670   - tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
2671   - tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
2672   - switch(data_bits) {
2673   - default:
2674   - case 8:
2675   - tty.c_cflag |= CS8;
2676   - break;
2677   - case 7:
2678   - tty.c_cflag |= CS7;
2679   - break;
2680   - case 6:
2681   - tty.c_cflag |= CS6;
2682   - break;
2683   - case 5:
2684   - tty.c_cflag |= CS5;
2685   - break;
2686   - }
2687   - switch(parity) {
2688   - default:
2689   - case 'N':
2690   - break;
2691   - case 'E':
2692   - tty.c_cflag |= PARENB;
2693   - break;
2694   - case 'O':
2695   - tty.c_cflag |= PARENB | PARODD;
2696   - break;
2697   - }
2698   - if (stop_bits == 2)
2699   - tty.c_cflag |= CSTOPB;
2700   -
2701   - tcsetattr (fd, TCSANOW, &tty);
2702   -}
2703   -
2704   -static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
2705   -{
2706   - FDCharDriver *s = chr->opaque;
2707   -
2708   - switch(cmd) {
2709   - case CHR_IOCTL_SERIAL_SET_PARAMS:
2710   - {
2711   - QEMUSerialSetParams *ssp = arg;
2712   - tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
2713   - ssp->data_bits, ssp->stop_bits);
2714   - }
2715   - break;
2716   - case CHR_IOCTL_SERIAL_SET_BREAK:
2717   - {
2718   - int enable = *(int *)arg;
2719   - if (enable)
2720   - tcsendbreak(s->fd_in, 1);
2721   - }
2722   - break;
2723   - case CHR_IOCTL_SERIAL_GET_TIOCM:
2724   - {
2725   - int sarg = 0;
2726   - int *targ = (int *)arg;
2727   - ioctl(s->fd_in, TIOCMGET, &sarg);
2728   - *targ = 0;
2729   - if (sarg | TIOCM_CTS)
2730   - *targ |= CHR_TIOCM_CTS;
2731   - if (sarg | TIOCM_CAR)
2732   - *targ |= CHR_TIOCM_CAR;
2733   - if (sarg | TIOCM_DSR)
2734   - *targ |= CHR_TIOCM_DSR;
2735   - if (sarg | TIOCM_RI)
2736   - *targ |= CHR_TIOCM_RI;
2737   - if (sarg | TIOCM_DTR)
2738   - *targ |= CHR_TIOCM_DTR;
2739   - if (sarg | TIOCM_RTS)
2740   - *targ |= CHR_TIOCM_RTS;
2741   - }
2742   - break;
2743   - case CHR_IOCTL_SERIAL_SET_TIOCM:
2744   - {
2745   - int sarg = *(int *)arg;
2746   - int targ = 0;
2747   - if (sarg | CHR_TIOCM_DTR)
2748   - targ |= TIOCM_DTR;
2749   - if (sarg | CHR_TIOCM_RTS)
2750   - targ |= TIOCM_RTS;
2751   - ioctl(s->fd_in, TIOCMSET, &targ);
2752   - }
2753   - break;
2754   - default:
2755   - return -ENOTSUP;
2756   - }
2757   - return 0;
2758   -}
2759   -
2760   -static CharDriverState *qemu_chr_open_tty(const char *filename)
2761   -{
2762   - CharDriverState *chr;
2763   - int fd;
2764   -
2765   - TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
2766   - tty_serial_init(fd, 115200, 'N', 8, 1);
2767   - chr = qemu_chr_open_fd(fd, fd);
2768   - if (!chr) {
2769   - close(fd);
2770   - return NULL;
2771   - }
2772   - chr->chr_ioctl = tty_serial_ioctl;
2773   - qemu_chr_reset(chr);
2774   - return chr;
2775   -}
2776   -#else /* ! __linux__ && ! __sun__ */
2777   -static CharDriverState *qemu_chr_open_pty(void)
2778   -{
2779   - return NULL;
2780   -}
2781   -#endif /* __linux__ || __sun__ */
2782   -
2783   -#if defined(__linux__)
2784   -typedef struct {
2785   - int fd;
2786   - int mode;
2787   -} ParallelCharDriver;
2788   -
2789   -static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2790   -{
2791   - if (s->mode != mode) {
2792   - int m = mode;
2793   - if (ioctl(s->fd, PPSETMODE, &m) < 0)
2794   - return 0;
2795   - s->mode = mode;
2796   - }
2797   - return 1;
2798   -}
2799   -
2800   -static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2801   -{
2802   - ParallelCharDriver *drv = chr->opaque;
2803   - int fd = drv->fd;
2804   - uint8_t b;
2805   -
2806   - switch(cmd) {
2807   - case CHR_IOCTL_PP_READ_DATA:
2808   - if (ioctl(fd, PPRDATA, &b) < 0)
2809   - return -ENOTSUP;
2810   - *(uint8_t *)arg = b;
2811   - break;
2812   - case CHR_IOCTL_PP_WRITE_DATA:
2813   - b = *(uint8_t *)arg;
2814   - if (ioctl(fd, PPWDATA, &b) < 0)
2815   - return -ENOTSUP;
2816   - break;
2817   - case CHR_IOCTL_PP_READ_CONTROL:
2818   - if (ioctl(fd, PPRCONTROL, &b) < 0)
2819   - return -ENOTSUP;
2820   - /* Linux gives only the lowest bits, and no way to know data
2821   - direction! For better compatibility set the fixed upper
2822   - bits. */
2823   - *(uint8_t *)arg = b | 0xc0;
2824   - break;
2825   - case CHR_IOCTL_PP_WRITE_CONTROL:
2826   - b = *(uint8_t *)arg;
2827   - if (ioctl(fd, PPWCONTROL, &b) < 0)
2828   - return -ENOTSUP;
2829   - break;
2830   - case CHR_IOCTL_PP_READ_STATUS:
2831   - if (ioctl(fd, PPRSTATUS, &b) < 0)
2832   - return -ENOTSUP;
2833   - *(uint8_t *)arg = b;
2834   - break;
2835   - case CHR_IOCTL_PP_DATA_DIR:
2836   - if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
2837   - return -ENOTSUP;
2838   - break;
2839   - case CHR_IOCTL_PP_EPP_READ_ADDR:
2840   - if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2841   - struct ParallelIOArg *parg = arg;
2842   - int n = read(fd, parg->buffer, parg->count);
2843   - if (n != parg->count) {
2844   - return -EIO;
2845   - }
2846   - }
2847   - break;
2848   - case CHR_IOCTL_PP_EPP_READ:
2849   - if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2850   - struct ParallelIOArg *parg = arg;
2851   - int n = read(fd, parg->buffer, parg->count);
2852   - if (n != parg->count) {
2853   - return -EIO;
2854   - }
2855   - }
2856   - break;
2857   - case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2858   - if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2859   - struct ParallelIOArg *parg = arg;
2860   - int n = write(fd, parg->buffer, parg->count);
2861   - if (n != parg->count) {
2862   - return -EIO;
2863   - }
2864   - }
2865   - break;
2866   - case CHR_IOCTL_PP_EPP_WRITE:
2867   - if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2868   - struct ParallelIOArg *parg = arg;
2869   - int n = write(fd, parg->buffer, parg->count);
2870   - if (n != parg->count) {
2871   - return -EIO;
2872   - }
2873   - }
2874   - break;
2875   - default:
2876   - return -ENOTSUP;
2877   - }
2878   - return 0;
2879   -}
2880   -
2881   -static void pp_close(CharDriverState *chr)
2882   -{
2883   - ParallelCharDriver *drv = chr->opaque;
2884   - int fd = drv->fd;
2885   -
2886   - pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2887   - ioctl(fd, PPRELEASE);
2888   - close(fd);
2889   - qemu_free(drv);
2890   -}
2891   -
2892   -static CharDriverState *qemu_chr_open_pp(const char *filename)
2893   -{
2894   - CharDriverState *chr;
2895   - ParallelCharDriver *drv;
2896   - int fd;
2897   -
2898   - TFR(fd = open(filename, O_RDWR));
2899   - if (fd < 0)
2900   - return NULL;
2901   -
2902   - if (ioctl(fd, PPCLAIM) < 0) {
2903   - close(fd);
2904   - return NULL;
2905   - }
2906   -
2907   - drv = qemu_mallocz(sizeof(ParallelCharDriver));
2908   - if (!drv) {
2909   - close(fd);
2910   - return NULL;
2911   - }
2912   - drv->fd = fd;
2913   - drv->mode = IEEE1284_MODE_COMPAT;
2914   -
2915   - chr = qemu_mallocz(sizeof(CharDriverState));
2916   - if (!chr) {
2917   - qemu_free(drv);
2918   - close(fd);
2919   - return NULL;
2920   - }
2921   - chr->chr_write = null_chr_write;
2922   - chr->chr_ioctl = pp_ioctl;
2923   - chr->chr_close = pp_close;
2924   - chr->opaque = drv;
2925   -
2926   - qemu_chr_reset(chr);
2927   -
2928   - return chr;
2929   -}
2930   -#endif /* __linux__ */
2931   -
2932   -#else /* _WIN32 */
2933   -
2934   -typedef struct {
2935   - int max_size;
2936   - HANDLE hcom, hrecv, hsend;
2937   - OVERLAPPED orecv, osend;
2938   - BOOL fpipe;
2939   - DWORD len;
2940   -} WinCharState;
2941   -
2942   -#define NSENDBUF 2048
2943   -#define NRECVBUF 2048
2944   -#define MAXCONNECT 1
2945   -#define NTIMEOUT 5000
2946   -
2947   -static int win_chr_poll(void *opaque);
2948   -static int win_chr_pipe_poll(void *opaque);
2949   -
2950   -static void win_chr_close(CharDriverState *chr)
2951   -{
2952   - WinCharState *s = chr->opaque;
2953   -
2954   - if (s->hsend) {
2955   - CloseHandle(s->hsend);
2956   - s->hsend = NULL;
2957   - }
2958   - if (s->hrecv) {
2959   - CloseHandle(s->hrecv);
2960   - s->hrecv = NULL;
2961   - }
2962   - if (s->hcom) {
2963   - CloseHandle(s->hcom);
2964   - s->hcom = NULL;
2965   - }
2966   - if (s->fpipe)
2967   - qemu_del_polling_cb(win_chr_pipe_poll, chr);
2968   - else
2969   - qemu_del_polling_cb(win_chr_poll, chr);
2970   -}
2971   -
2972   -static int win_chr_init(CharDriverState *chr, const char *filename)
2973   -{
2974   - WinCharState *s = chr->opaque;
2975   - COMMCONFIG comcfg;
2976   - COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2977   - COMSTAT comstat;
2978   - DWORD size;
2979   - DWORD err;
2980   -
2981   - s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2982   - if (!s->hsend) {
2983   - fprintf(stderr, "Failed CreateEvent\n");
2984   - goto fail;
2985   - }
2986   - s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2987   - if (!s->hrecv) {
2988   - fprintf(stderr, "Failed CreateEvent\n");
2989   - goto fail;
2990   - }
2991   -
2992   - s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2993   - OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2994   - if (s->hcom == INVALID_HANDLE_VALUE) {
2995   - fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2996   - s->hcom = NULL;
2997   - goto fail;
2998   - }
2999   -
3000   - if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
3001   - fprintf(stderr, "Failed SetupComm\n");
3002   - goto fail;
3003   - }
3004   -
3005   - ZeroMemory(&comcfg, sizeof(COMMCONFIG));
3006   - size = sizeof(COMMCONFIG);
3007   - GetDefaultCommConfig(filename, &comcfg, &size);
3008   - comcfg.dcb.DCBlength = sizeof(DCB);
3009   - CommConfigDialog(filename, NULL, &comcfg);
3010   -
3011   - if (!SetCommState(s->hcom, &comcfg.dcb)) {
3012   - fprintf(stderr, "Failed SetCommState\n");
3013   - goto fail;
3014   - }
3015   -
3016   - if (!SetCommMask(s->hcom, EV_ERR)) {
3017   - fprintf(stderr, "Failed SetCommMask\n");
3018   - goto fail;
3019   - }
3020   -
3021   - cto.ReadIntervalTimeout = MAXDWORD;
3022   - if (!SetCommTimeouts(s->hcom, &cto)) {
3023   - fprintf(stderr, "Failed SetCommTimeouts\n");
3024   - goto fail;
3025   - }
3026   -
3027   - if (!ClearCommError(s->hcom, &err, &comstat)) {
3028   - fprintf(stderr, "Failed ClearCommError\n");
3029   - goto fail;
3030   - }
3031   - qemu_add_polling_cb(win_chr_poll, chr);
3032   - return 0;
3033   -
3034   - fail:
3035   - win_chr_close(chr);
3036   - return -1;
3037   -}
3038   -
3039   -static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
3040   -{
3041   - WinCharState *s = chr->opaque;
3042   - DWORD len, ret, size, err;
3043   -
3044   - len = len1;
3045   - ZeroMemory(&s->osend, sizeof(s->osend));
3046   - s->osend.hEvent = s->hsend;
3047   - while (len > 0) {
3048   - if (s->hsend)
3049   - ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
3050   - else
3051   - ret = WriteFile(s->hcom, buf, len, &size, NULL);
3052   - if (!ret) {
3053   - err = GetLastError();
3054   - if (err == ERROR_IO_PENDING) {
3055   - ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
3056   - if (ret) {
3057   - buf += size;
3058   - len -= size;
3059   - } else {
3060   - break;
3061   - }
3062   - } else {
3063   - break;
3064   - }
3065   - } else {
3066   - buf += size;
3067   - len -= size;
3068   - }
3069   - }
3070   - return len1 - len;
3071   -}
3072   -
3073   -static int win_chr_read_poll(CharDriverState *chr)
3074   -{
3075   - WinCharState *s = chr->opaque;
3076   -
3077   - s->max_size = qemu_chr_can_read(chr);
3078   - return s->max_size;
3079   -}
3080   -
3081   -static void win_chr_readfile(CharDriverState *chr)
3082   -{
3083   - WinCharState *s = chr->opaque;
3084   - int ret, err;
3085   - uint8_t buf[1024];
3086   - DWORD size;
3087   -
3088   - ZeroMemory(&s->orecv, sizeof(s->orecv));
3089   - s->orecv.hEvent = s->hrecv;
3090   - ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
3091   - if (!ret) {
3092   - err = GetLastError();
3093   - if (err == ERROR_IO_PENDING) {
3094   - ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
3095   - }
3096   - }
3097   -
3098   - if (size > 0) {
3099   - qemu_chr_read(chr, buf, size);
3100   - }
3101   -}
3102   -
3103   -static void win_chr_read(CharDriverState *chr)
3104   -{
3105   - WinCharState *s = chr->opaque;
3106   -
3107   - if (s->len > s->max_size)
3108   - s->len = s->max_size;
3109   - if (s->len == 0)
3110   - return;
3111   -
3112   - win_chr_readfile(chr);
3113   -}
3114   -
3115   -static int win_chr_poll(void *opaque)
3116   -{
3117   - CharDriverState *chr = opaque;
3118   - WinCharState *s = chr->opaque;
3119   - COMSTAT status;
3120   - DWORD comerr;
3121   -
3122   - ClearCommError(s->hcom, &comerr, &status);
3123   - if (status.cbInQue > 0) {
3124   - s->len = status.cbInQue;
3125   - win_chr_read_poll(chr);
3126   - win_chr_read(chr);
3127   - return 1;
3128   - }
3129   - return 0;
3130   -}
3131   -
3132   -static CharDriverState *qemu_chr_open_win(const char *filename)
3133   -{
3134   - CharDriverState *chr;
3135   - WinCharState *s;
3136   -
3137   - chr = qemu_mallocz(sizeof(CharDriverState));
3138   - if (!chr)
3139   - return NULL;
3140   - s = qemu_mallocz(sizeof(WinCharState));
3141   - if (!s) {
3142   - free(chr);
3143   - return NULL;
3144   - }
3145   - chr->opaque = s;
3146   - chr->chr_write = win_chr_write;
3147   - chr->chr_close = win_chr_close;
3148   -
3149   - if (win_chr_init(chr, filename) < 0) {
3150   - free(s);
3151   - free(chr);
3152   - return NULL;
3153   - }
3154   - qemu_chr_reset(chr);
3155   - return chr;
3156   -}
3157   -
3158   -static int win_chr_pipe_poll(void *opaque)
3159   -{
3160   - CharDriverState *chr = opaque;
3161   - WinCharState *s = chr->opaque;
3162   - DWORD size;
3163   -
3164   - PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
3165   - if (size > 0) {
3166   - s->len = size;
3167   - win_chr_read_poll(chr);
3168   - win_chr_read(chr);
3169   - return 1;
3170   - }
3171   - return 0;
3172   -}
3173   -
3174   -static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
3175   -{
3176   - WinCharState *s = chr->opaque;
3177   - OVERLAPPED ov;
3178   - int ret;
3179   - DWORD size;
3180   - char openname[256];
3181   -
3182   - s->fpipe = TRUE;
3183   -
3184   - s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
3185   - if (!s->hsend) {
3186   - fprintf(stderr, "Failed CreateEvent\n");
3187   - goto fail;
3188   - }
3189   - s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
3190   - if (!s->hrecv) {
3191   - fprintf(stderr, "Failed CreateEvent\n");
3192   - goto fail;
3193   - }
3194   -
3195   - snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
3196   - s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
3197   - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
3198   - PIPE_WAIT,
3199   - MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
3200   - if (s->hcom == INVALID_HANDLE_VALUE) {
3201   - fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
3202   - s->hcom = NULL;
3203   - goto fail;
3204   - }
3205   -
3206   - ZeroMemory(&ov, sizeof(ov));
3207   - ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
3208   - ret = ConnectNamedPipe(s->hcom, &ov);
3209   - if (ret) {
3210   - fprintf(stderr, "Failed ConnectNamedPipe\n");
3211   - goto fail;
3212   - }
3213   -
3214   - ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
3215   - if (!ret) {
3216   - fprintf(stderr, "Failed GetOverlappedResult\n");
3217   - if (ov.hEvent) {
3218   - CloseHandle(ov.hEvent);
3219   - ov.hEvent = NULL;
3220   - }
3221   - goto fail;
3222   - }
3223   -
3224   - if (ov.hEvent) {
3225   - CloseHandle(ov.hEvent);
3226   - ov.hEvent = NULL;
3227   - }
3228   - qemu_add_polling_cb(win_chr_pipe_poll, chr);
3229   - return 0;
3230   -
3231   - fail:
3232   - win_chr_close(chr);
3233   - return -1;
3234   -}
3235   -
3236   -
3237   -static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
3238   -{
3239   - CharDriverState *chr;
3240   - WinCharState *s;
3241   -
3242   - chr = qemu_mallocz(sizeof(CharDriverState));
3243   - if (!chr)
3244   - return NULL;
3245   - s = qemu_mallocz(sizeof(WinCharState));
3246   - if (!s) {
3247   - free(chr);
3248   - return NULL;
3249   - }
3250   - chr->opaque = s;
3251   - chr->chr_write = win_chr_write;
3252   - chr->chr_close = win_chr_close;
3253   -
3254   - if (win_chr_pipe_init(chr, filename) < 0) {
3255   - free(s);
3256   - free(chr);
3257   - return NULL;
3258   - }
3259   - qemu_chr_reset(chr);
3260   - return chr;
3261   -}
3262   -
3263   -static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
3264   -{
3265   - CharDriverState *chr;
3266   - WinCharState *s;
3267   -
3268   - chr = qemu_mallocz(sizeof(CharDriverState));
3269   - if (!chr)
3270   - return NULL;
3271   - s = qemu_mallocz(sizeof(WinCharState));
3272   - if (!s) {
3273   - free(chr);
3274   - return NULL;
3275   - }
3276   - s->hcom = fd_out;
3277   - chr->opaque = s;
3278   - chr->chr_write = win_chr_write;
3279   - qemu_chr_reset(chr);
3280   - return chr;
3281   -}
3282   -
3283   -static CharDriverState *qemu_chr_open_win_con(const char *filename)
3284   -{
3285   - return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
3286   -}
3287   -
3288   -static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
3289   -{
3290   - HANDLE fd_out;
3291   -
3292   - fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3293   - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3294   - if (fd_out == INVALID_HANDLE_VALUE)
3295   - return NULL;
3296   -
3297   - return qemu_chr_open_win_file(fd_out);
3298   -}
3299   -#endif /* !_WIN32 */
3300   -
3301   -/***********************************************************/
3302   -/* UDP Net console */
3303   -
3304   -typedef struct {
3305   - int fd;
3306   - struct sockaddr_in daddr;
3307   - uint8_t buf[1024];
3308   - int bufcnt;
3309   - int bufptr;
3310   - int max_size;
3311   -} NetCharDriver;
3312   -
3313   -static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3314   -{
3315   - NetCharDriver *s = chr->opaque;
3316   -
3317   - return sendto(s->fd, buf, len, 0,
3318   - (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
3319   -}
3320   -
3321   -static int udp_chr_read_poll(void *opaque)
3322   -{
3323   - CharDriverState *chr = opaque;
3324   - NetCharDriver *s = chr->opaque;
3325   -
3326   - s->max_size = qemu_chr_can_read(chr);
3327   -
3328   - /* If there were any stray characters in the queue process them
3329   - * first
3330   - */
3331   - while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3332   - qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3333   - s->bufptr++;
3334   - s->max_size = qemu_chr_can_read(chr);
3335   - }
3336   - return s->max_size;
3337   -}
3338   -
3339   -static void udp_chr_read(void *opaque)
3340   -{
3341   - CharDriverState *chr = opaque;
3342   - NetCharDriver *s = chr->opaque;
3343   -
3344   - if (s->max_size == 0)
3345   - return;
3346   - s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
3347   - s->bufptr = s->bufcnt;
3348   - if (s->bufcnt <= 0)
3349   - return;
3350   -
3351   - s->bufptr = 0;
3352   - while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3353   - qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3354   - s->bufptr++;
3355   - s->max_size = qemu_chr_can_read(chr);
3356   - }
3357   -}
3358   -
3359   -static void udp_chr_update_read_handler(CharDriverState *chr)
3360   -{
3361   - NetCharDriver *s = chr->opaque;
3362   -
3363   - if (s->fd >= 0) {
3364   - qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
3365   - udp_chr_read, NULL, chr);
3366   - }
3367   -}
3368   -
3369   -static CharDriverState *qemu_chr_open_udp(const char *def)
3370   -{
3371   - CharDriverState *chr = NULL;
3372   - NetCharDriver *s = NULL;
3373   - int fd = -1;
3374   - struct sockaddr_in saddr;
3375   -
3376   - chr = qemu_mallocz(sizeof(CharDriverState));
3377   - if (!chr)
3378   - goto return_err;
3379   - s = qemu_mallocz(sizeof(NetCharDriver));
3380   - if (!s)
3381   - goto return_err;
3382   -
3383   - fd = socket(PF_INET, SOCK_DGRAM, 0);
3384   - if (fd < 0) {
3385   - perror("socket(PF_INET, SOCK_DGRAM)");
3386   - goto return_err;
3387   - }
3388   -
3389   - if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
3390   - printf("Could not parse: %s\n", def);
3391   - goto return_err;
3392   - }
3393   -
3394   - if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
3395   - {
3396   - perror("bind");
3397   - goto return_err;
3398   - }
3399   -
3400   - s->fd = fd;
3401   - s->bufcnt = 0;
3402   - s->bufptr = 0;
3403   - chr->opaque = s;
3404   - chr->chr_write = udp_chr_write;
3405   - chr->chr_update_read_handler = udp_chr_update_read_handler;
3406   - return chr;
3407   -
3408   -return_err:
3409   - if (chr)
3410   - free(chr);
3411   - if (s)
3412   - free(s);
3413   - if (fd >= 0)
3414   - closesocket(fd);
3415   - return NULL;
3416   -}
3417   -
3418   -/***********************************************************/
3419   -/* TCP Net console */
3420   -
3421   -typedef struct {
3422   - int fd, listen_fd;
3423   - int connected;
3424   - int max_size;
3425   - int do_telnetopt;
3426   - int do_nodelay;
3427   - int is_unix;
3428   -} TCPCharDriver;
3429   -
3430   -static void tcp_chr_accept(void *opaque);
3431   -
3432   -static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3433   -{
3434   - TCPCharDriver *s = chr->opaque;
3435   - if (s->connected) {
3436   - return send_all(s->fd, buf, len);
3437   - } else {
3438   - /* XXX: indicate an error ? */
3439   - return len;
3440   - }
3441   -}
3442   -
3443   -static int tcp_chr_read_poll(void *opaque)
3444   -{
3445   - CharDriverState *chr = opaque;
3446   - TCPCharDriver *s = chr->opaque;
3447   - if (!s->connected)
3448   - return 0;
3449   - s->max_size = qemu_chr_can_read(chr);
3450   - return s->max_size;
3451   -}
3452   -
3453   -#define IAC 255
3454   -#define IAC_BREAK 243
3455   -static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
3456   - TCPCharDriver *s,
3457   - uint8_t *buf, int *size)
3458   -{
3459   - /* Handle any telnet client's basic IAC options to satisfy char by
3460   - * char mode with no echo. All IAC options will be removed from
3461   - * the buf and the do_telnetopt variable will be used to track the
3462   - * state of the width of the IAC information.
3463   - *
3464   - * IAC commands come in sets of 3 bytes with the exception of the
3465   - * "IAC BREAK" command and the double IAC.
3466   - */
3467   -
3468   - int i;
3469   - int j = 0;
3470   -
3471   - for (i = 0; i < *size; i++) {
3472   - if (s->do_telnetopt > 1) {
3473   - if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3474   - /* Double IAC means send an IAC */
3475   - if (j != i)
3476   - buf[j] = buf[i];
3477   - j++;
3478   - s->do_telnetopt = 1;
3479   - } else {
3480   - if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3481   - /* Handle IAC break commands by sending a serial break */
3482   - qemu_chr_event(chr, CHR_EVENT_BREAK);
3483   - s->do_telnetopt++;
3484   - }
3485   - s->do_telnetopt++;
3486   - }
3487   - if (s->do_telnetopt >= 4) {
3488   - s->do_telnetopt = 1;
3489   - }
3490   - } else {
3491   - if ((unsigned char)buf[i] == IAC) {
3492   - s->do_telnetopt = 2;
3493   - } else {
3494   - if (j != i)
3495   - buf[j] = buf[i];
3496   - j++;
3497   - }
3498   - }
3499   - }
3500   - *size = j;
3501   -}
3502   -
3503   -static void tcp_chr_read(void *opaque)
3504   -{
3505   - CharDriverState *chr = opaque;
3506   - TCPCharDriver *s = chr->opaque;
3507   - uint8_t buf[1024];
3508   - int len, size;
3509   -
3510   - if (!s->connected || s->max_size <= 0)
3511   - return;
3512   - len = sizeof(buf);
3513   - if (len > s->max_size)
3514   - len = s->max_size;
3515   - size = recv(s->fd, buf, len, 0);
3516   - if (size == 0) {
3517   - /* connection closed */
3518   - s->connected = 0;
3519   - if (s->listen_fd >= 0) {
3520   - qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3521   - }
3522   - qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3523   - closesocket(s->fd);
3524   - s->fd = -1;
3525   - } else if (size > 0) {
3526   - if (s->do_telnetopt)
3527   - tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3528   - if (size > 0)
3529   - qemu_chr_read(chr, buf, size);
3530   - }
3531   -}
3532   -
3533   -static void tcp_chr_connect(void *opaque)
3534   -{
3535   - CharDriverState *chr = opaque;
3536   - TCPCharDriver *s = chr->opaque;
3537   -
3538   - s->connected = 1;
3539   - qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3540   - tcp_chr_read, NULL, chr);
3541   - qemu_chr_reset(chr);
3542   -}
3543   -
3544   -#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3545   -static void tcp_chr_telnet_init(int fd)
3546   -{
3547   - char buf[3];
3548   - /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3549   - IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3550   - send(fd, (char *)buf, 3, 0);
3551   - IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3552   - send(fd, (char *)buf, 3, 0);
3553   - IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3554   - send(fd, (char *)buf, 3, 0);
3555   - IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3556   - send(fd, (char *)buf, 3, 0);
3557   -}
3558   -
3559   -static void socket_set_nodelay(int fd)
3560   -{
3561   - int val = 1;
3562   - setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3563   -}
3564   -
3565   -static void tcp_chr_accept(void *opaque)
3566   -{
3567   - CharDriverState *chr = opaque;
3568   - TCPCharDriver *s = chr->opaque;
3569   - struct sockaddr_in saddr;
3570   -#ifndef _WIN32
3571   - struct sockaddr_un uaddr;
3572   -#endif
3573   - struct sockaddr *addr;
3574   - socklen_t len;
3575   - int fd;
3576   -
3577   - for(;;) {
3578   -#ifndef _WIN32
3579   - if (s->is_unix) {
3580   - len = sizeof(uaddr);
3581   - addr = (struct sockaddr *)&uaddr;
3582   - } else
3583   -#endif
3584   - {
3585   - len = sizeof(saddr);
3586   - addr = (struct sockaddr *)&saddr;
3587   - }
3588   - fd = accept(s->listen_fd, addr, &len);
3589   - if (fd < 0 && errno != EINTR) {
3590   - return;
3591   - } else if (fd >= 0) {
3592   - if (s->do_telnetopt)
3593   - tcp_chr_telnet_init(fd);
3594   - break;
3595   - }
3596   - }
3597   - socket_set_nonblock(fd);
3598   - if (s->do_nodelay)
3599   - socket_set_nodelay(fd);
3600   - s->fd = fd;
3601   - qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3602   - tcp_chr_connect(chr);
3603   -}
3604   -
3605   -static void tcp_chr_close(CharDriverState *chr)
3606   -{
3607   - TCPCharDriver *s = chr->opaque;
3608   - if (s->fd >= 0)
3609   - closesocket(s->fd);
3610   - if (s->listen_fd >= 0)
3611   - closesocket(s->listen_fd);
3612   - qemu_free(s);
3613   -}
3614   -
3615   -static CharDriverState *qemu_chr_open_tcp(const char *host_str,
3616   - int is_telnet,
3617   - int is_unix)
3618   -{
3619   - CharDriverState *chr = NULL;
3620   - TCPCharDriver *s = NULL;
3621   - int fd = -1, ret, err, val;
3622   - int is_listen = 0;
3623   - int is_waitconnect = 1;
3624   - int do_nodelay = 0;
3625   - const char *ptr;
3626   - struct sockaddr_in saddr;
3627   -#ifndef _WIN32
3628   - struct sockaddr_un uaddr;
3629   -#endif
3630   - struct sockaddr *addr;
3631   - socklen_t addrlen;
3632   -
3633   -#ifndef _WIN32
3634   - if (is_unix) {
3635   - addr = (struct sockaddr *)&uaddr;
3636   - addrlen = sizeof(uaddr);
3637   - if (parse_unix_path(&uaddr, host_str) < 0)
3638   - goto fail;
3639   - } else
3640   -#endif
3641   - {
3642   - addr = (struct sockaddr *)&saddr;
3643   - addrlen = sizeof(saddr);
3644   - if (parse_host_port(&saddr, host_str) < 0)
3645   - goto fail;
3646   - }
3647   -
3648   - ptr = host_str;
3649   - while((ptr = strchr(ptr,','))) {
3650   - ptr++;
3651   - if (!strncmp(ptr,"server",6)) {
3652   - is_listen = 1;
3653   - } else if (!strncmp(ptr,"nowait",6)) {
3654   - is_waitconnect = 0;
3655   - } else if (!strncmp(ptr,"nodelay",6)) {
3656   - do_nodelay = 1;
3657   - } else {
3658   - printf("Unknown option: %s\n", ptr);
3659   - goto fail;
3660   - }
3661   - }
3662   - if (!is_listen)
3663   - is_waitconnect = 0;
3664   -
3665   - chr = qemu_mallocz(sizeof(CharDriverState));
3666   - if (!chr)
3667   - goto fail;
3668   - s = qemu_mallocz(sizeof(TCPCharDriver));
3669   - if (!s)
3670   - goto fail;
3671   -
3672   -#ifndef _WIN32
3673   - if (is_unix)
3674   - fd = socket(PF_UNIX, SOCK_STREAM, 0);
3675   - else
3676   -#endif
3677   - fd = socket(PF_INET, SOCK_STREAM, 0);
3678   -
3679   - if (fd < 0)
3680   - goto fail;
3681   -
3682   - if (!is_waitconnect)
3683   - socket_set_nonblock(fd);
3684   -
3685   - s->connected = 0;
3686   - s->fd = -1;
3687   - s->listen_fd = -1;
3688   - s->is_unix = is_unix;
3689   - s->do_nodelay = do_nodelay && !is_unix;
3690   -
3691   - chr->opaque = s;
3692   - chr->chr_write = tcp_chr_write;
3693   - chr->chr_close = tcp_chr_close;
3694   -
3695   - if (is_listen) {
3696   - /* allow fast reuse */
3697   -#ifndef _WIN32
3698   - if (is_unix) {
3699   - char path[109];
3700   - pstrcpy(path, sizeof(path), uaddr.sun_path);
3701   - unlink(path);
3702   - } else
3703   -#endif
3704   - {
3705   - val = 1;
3706   - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3707   - }
3708   -
3709   - ret = bind(fd, addr, addrlen);
3710   - if (ret < 0)
3711   - goto fail;
3712   -
3713   - ret = listen(fd, 0);
3714   - if (ret < 0)
3715   - goto fail;
3716   -
3717   - s->listen_fd = fd;
3718   - qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3719   - if (is_telnet)
3720   - s->do_telnetopt = 1;
3721   - } else {
3722   - for(;;) {
3723   - ret = connect(fd, addr, addrlen);
3724   - if (ret < 0) {
3725   - err = socket_error();
3726   - if (err == EINTR || err == EWOULDBLOCK) {
3727   - } else if (err == EINPROGRESS) {
3728   - break;
3729   -#ifdef _WIN32
3730   - } else if (err == WSAEALREADY) {
3731   - break;
3732   -#endif
3733   - } else {
3734   - goto fail;
3735   - }
3736   - } else {
3737   - s->connected = 1;
3738   - break;
3739   - }
3740   - }
3741   - s->fd = fd;
3742   - socket_set_nodelay(fd);
3743   - if (s->connected)
3744   - tcp_chr_connect(chr);
3745   - else
3746   - qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3747   - }
3748   -
3749   - if (is_listen && is_waitconnect) {
3750   - printf("QEMU waiting for connection on: %s\n", host_str);
3751   - tcp_chr_accept(chr);
3752   - socket_set_nonblock(s->listen_fd);
3753   - }
3754   -
3755   - return chr;
3756   - fail:
3757   - if (fd >= 0)
3758   - closesocket(fd);
3759   - qemu_free(s);
3760   - qemu_free(chr);
3761   - return NULL;
3762   -}
3763   -
3764   -static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
3765   -= TAILQ_HEAD_INITIALIZER(chardevs);
3766   -
3767   -CharDriverState *qemu_chr_open(const char *label, const char *filename)
3768   -{
3769   - const char *p;
3770   - CharDriverState *chr;
3771   -
3772   - if (!strcmp(filename, "vc")) {
3773   - chr = text_console_init(&display_state, 0);
3774   - } else
3775   - if (strstart(filename, "vc:", &p)) {
3776   - chr = text_console_init(&display_state, p);
3777   - } else
3778   - if (!strcmp(filename, "null")) {
3779   - chr = qemu_chr_open_null();
3780   - } else
3781   - if (strstart(filename, "tcp:", &p)) {
3782   - chr = qemu_chr_open_tcp(p, 0, 0);
3783   - } else
3784   - if (strstart(filename, "telnet:", &p)) {
3785   - chr = qemu_chr_open_tcp(p, 1, 0);
3786   - } else
3787   - if (strstart(filename, "udp:", &p)) {
3788   - chr = qemu_chr_open_udp(p);
3789   - } else
3790   - if (strstart(filename, "mon:", &p)) {
3791   - chr = qemu_chr_open(label, p);
3792   - if (chr) {
3793   - chr = qemu_chr_open_mux(chr);
3794   - monitor_init(chr, !nographic);
3795   - } else {
3796   - printf("Unable to open driver: %s\n", p);
3797   - }
3798   - } else
3799   -#ifndef _WIN32
3800   - if (strstart(filename, "unix:", &p)) {
3801   - chr = qemu_chr_open_tcp(p, 0, 1);
3802   - } else if (strstart(filename, "file:", &p)) {
3803   - chr = qemu_chr_open_file_out(p);
3804   - } else if (strstart(filename, "pipe:", &p)) {
3805   - chr = qemu_chr_open_pipe(p);
3806   - } else if (!strcmp(filename, "pty")) {
3807   - chr = qemu_chr_open_pty();
3808   - } else if (!strcmp(filename, "stdio")) {
3809   - chr = qemu_chr_open_stdio();
3810   - } else
3811   -#if defined(__linux__)
3812   - if (strstart(filename, "/dev/parport", NULL)) {
3813   - chr = qemu_chr_open_pp(filename);
3814   - } else
3815   -#endif
3816   -#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
3817   - || defined(__NetBSD__) || defined(__OpenBSD__)
3818   - if (strstart(filename, "/dev/", NULL)) {
3819   - chr = qemu_chr_open_tty(filename);
3820   - } else
3821   -#endif
3822   -#else /* !_WIN32 */
3823   - if (strstart(filename, "COM", NULL)) {
3824   - chr = qemu_chr_open_win(filename);
3825   - } else
3826   - if (strstart(filename, "pipe:", &p)) {
3827   - chr = qemu_chr_open_win_pipe(p);
3828   - } else
3829   - if (strstart(filename, "con:", NULL)) {
3830   - chr = qemu_chr_open_win_con(filename);
3831   - } else
3832   - if (strstart(filename, "file:", &p)) {
3833   - chr = qemu_chr_open_win_file_out(p);
3834   - } else
3835   -#endif
3836   -#ifdef CONFIG_BRLAPI
3837   - if (!strcmp(filename, "braille")) {
3838   - chr = chr_baum_init();
3839   - } else
3840   -#endif
3841   - {
3842   - chr = NULL;
3843   - }
3844   -
3845   - if (chr) {
3846   - if (!chr->filename)
3847   - chr->filename = qemu_strdup(filename);
3848   - chr->label = qemu_strdup(label);
3849   - TAILQ_INSERT_TAIL(&chardevs, chr, next);
3850   - }
3851   - return chr;
3852   -}
3853   -
3854   -void qemu_chr_close(CharDriverState *chr)
3855   -{
3856   - TAILQ_REMOVE(&chardevs, chr, next);
3857   - if (chr->chr_close)
3858   - chr->chr_close(chr);
3859   - qemu_free(chr->filename);
3860   - qemu_free(chr->label);
3861   - qemu_free(chr);
3862   -}
3863   -
3864   -void qemu_chr_info(void)
3865   -{
3866   - CharDriverState *chr;
3867   -
3868   - TAILQ_FOREACH(chr, &chardevs, next) {
3869   - term_printf("%s: filename=%s\n", chr->label, chr->filename);
3870   - }
3871   -}
3872   -
3873   -/***********************************************************/
3874   -/* network device redirectors */
3875 1763  
3876 1764 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
3877 1765 static void hex_dump(FILE *f, const uint8_t *buf, int size)
... ...