blob: 715b06de82ccefe2e492def8a0dcd3b95249e85a [file] [log] [blame]
/*
* QEMU System Emulator
*
* Copyright (c) 2003-2008 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qemu/cutils.h"
#include "monitor/monitor.h"
#include "sysemu/sysemu.h"
#include "sysemu/block-backend.h"
#include "qemu/error-report.h"
#include "qemu/timer.h"
#include "sysemu/char.h"
#include "hw/usb.h"
#include "qmp-commands.h"
#include "qapi/clone-visitor.h"
#include "qapi-visit.h"
#include "qemu/base64.h"
#include "io/channel-socket.h"
#include "io/channel-file.h"
#include "io/channel-tls.h"
#include "sysemu/replay.h"
#include "qemu/help_option.h"
#include <zlib.h>
#ifndef _WIN32
#include <sys/times.h>
#include <sys/wait.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/select.h>
#ifdef CONFIG_BSD
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <dev/ppbus/ppi.h>
#include <dev/ppbus/ppbconf.h>
#elif defined(__DragonFly__)
#include <dev/misc/ppi/ppi.h>
#include <bus/ppbus/ppbconf.h>
#endif
#else
#ifdef __linux__
#include <linux/ppdev.h>
#include <linux/parport.h>
#endif
#ifdef __sun__
#include <sys/ethernet.h>
#include <sys/sockio.h>
#include <netinet/arp.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h> // must come after ip.h
#include <netinet/udp.h>
#include <netinet/tcp.h>
#endif
#endif
#endif
#include "qemu/sockets.h"
#include "ui/qemu-spice.h"
#define READ_BUF_LEN 4096
#define READ_RETRIES 10
#define TCP_MAX_FDS 16
typedef struct MuxDriver MuxDriver;
/***********************************************************/
/* Socket address helpers */
static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr,
bool is_listen, bool is_telnet)
{
switch (addr->type) {
case SOCKET_ADDRESS_KIND_INET:
return g_strdup_printf("%s%s:%s:%s%s", prefix,
is_telnet ? "telnet" : "tcp",
addr->u.inet.data->host,
addr->u.inet.data->port,
is_listen ? ",server" : "");
break;
case SOCKET_ADDRESS_KIND_UNIX:
return g_strdup_printf("%sunix:%s%s", prefix,
addr->u.q_unix.data->path,
is_listen ? ",server" : "");
break;
case SOCKET_ADDRESS_KIND_FD:
return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.data->str,
is_listen ? ",server" : "");
break;
default:
abort();
}
}
static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len,
struct sockaddr_storage *ps, socklen_t ps_len,
bool is_listen, bool is_telnet)
{
char shost[NI_MAXHOST], sserv[NI_MAXSERV];
char phost[NI_MAXHOST], pserv[NI_MAXSERV];
const char *left = "", *right = "";
switch (ss->ss_family) {
#ifndef _WIN32
case AF_UNIX:
return g_strdup_printf("unix:%s%s",
((struct sockaddr_un *)(ss))->sun_path,
is_listen ? ",server" : "");
#endif
case AF_INET6:
left = "[";
right = "]";
/* fall through */
case AF_INET:
getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
is_telnet ? "telnet" : "tcp",
left, shost, right, sserv,
is_listen ? ",server" : "",
left, phost, right, pserv);
default:
return g_strdup_printf("unknown");
}
}
/***********************************************************/
/* character device */
static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
QTAILQ_HEAD_INITIALIZER(chardevs);
static void qemu_chr_free_common(CharDriverState *chr);
CharDriverState *qemu_chr_alloc(ChardevCommon *backend, Error **errp)
{
CharDriverState *chr = g_malloc0(sizeof(CharDriverState));
qemu_mutex_init(&chr->chr_write_lock);
if (backend->has_logfile) {
int flags = O_WRONLY | O_CREAT;
if (backend->has_logappend &&
backend->logappend) {
flags |= O_APPEND;
} else {
flags |= O_TRUNC;
}
chr->logfd = qemu_open(backend->logfile, flags, 0666);
if (chr->logfd < 0) {
error_setg_errno(errp, errno,
"Unable to open logfile %s",
backend->logfile);
g_free(chr);
return NULL;
}
} else {
chr->logfd = -1;
}
return chr;
}
void qemu_chr_be_event(CharDriverState *s, int event)
{
CharBackend *be = s->be;
/* Keep track if the char device is open */
switch (event) {
case CHR_EVENT_OPENED:
s->be_open = 1;
break;
case CHR_EVENT_CLOSED:
s->be_open = 0;
break;
}
if (!be || !be->chr_event) {
return;
}
be->chr_event(be->opaque, event);
}
void qemu_chr_be_generic_open(CharDriverState *s)
{
qemu_chr_be_event(s, CHR_EVENT_OPENED);
}
/* Not reporting errors from writing to logfile, as logs are
* defined to be "best effort" only */
static void qemu_chr_fe_write_log(CharDriverState *s,
const uint8_t *buf, size_t len)
{
size_t done = 0;
ssize_t ret;
if (s->logfd < 0) {
return;
}
while (done < len) {
retry:
ret = write(s->logfd, buf + done, len - done);
if (ret == -1 && errno == EAGAIN) {
g_usleep(100);
goto retry;
}
if (ret <= 0) {
return;
}
done += ret;
}
}
static int qemu_chr_fe_write_buffer(CharDriverState *s, const uint8_t *buf, int len, int *offset)
{
int res = 0;
*offset = 0;
qemu_mutex_lock(&s->chr_write_lock);
while (*offset < len) {
retry:
res = s->chr_write(s, buf + *offset, len - *offset);
if (res < 0 && errno == EAGAIN) {
g_usleep(100);
goto retry;
}
if (res <= 0) {
break;
}
*offset += res;
}
if (*offset > 0) {
qemu_chr_fe_write_log(s, buf, *offset);
}
qemu_mutex_unlock(&s->chr_write_lock);
return res;
}
int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)
{
CharDriverState *s = be->chr;
int ret;
if (!s) {
return 0;
}
if (s->replay && replay_mode == REPLAY_MODE_PLAY) {
int offset;
replay_char_write_event_load(&ret, &offset);
assert(offset <= len);
qemu_chr_fe_write_buffer(s, buf, offset, &offset);
return ret;
}
qemu_mutex_lock(&s->chr_write_lock);
ret = s->chr_write(s, buf, len);
if (ret > 0) {
qemu_chr_fe_write_log(s, buf, ret);
}
qemu_mutex_unlock(&s->chr_write_lock);
if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_write_event_save(ret, ret < 0 ? 0 : ret);
}
return ret;
}
static int qemu_chr_write_all(CharDriverState *s, const uint8_t *buf, int len)
{
int offset;
int res;
if (s->replay && replay_mode == REPLAY_MODE_PLAY) {
replay_char_write_event_load(&res, &offset);
assert(offset <= len);
qemu_chr_fe_write_buffer(s, buf, offset, &offset);
return res;
}
res = qemu_chr_fe_write_buffer(s, buf, len, &offset);
if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_write_event_save(res, offset);
}
if (res < 0) {
return res;
}
return offset;
}
int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len)
{
CharDriverState *s = be->chr;
if (!s) {
return 0;
}
return qemu_chr_write_all(s, buf, len);
}
int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)
{
CharDriverState *s = be->chr;
int offset = 0, counter = 10;
int res;
if (!s || !s->chr_sync_read) {
return 0;
}
if (s->replay && replay_mode == REPLAY_MODE_PLAY) {
return replay_char_read_all_load(buf);
}
while (offset < len) {
retry:
res = s->chr_sync_read(s, buf + offset, len - offset);
if (res == -1 && errno == EAGAIN) {
g_usleep(100);
goto retry;
}
if (res == 0) {
break;
}
if (res < 0) {
if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_read_all_save_error(res);
}
return res;
}
offset += res;
if (!counter--) {
break;
}
}
if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_read_all_save_buf(buf, offset);
}
return offset;
}
int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
{
CharDriverState *s = be->chr;
int res;
if (!s || !s->chr_ioctl || s->replay) {
res = -ENOTSUP;
} else {
res = s->chr_ioctl(s, cmd, arg);
}
return res;
}
int qemu_chr_be_can_write(CharDriverState *s)
{
CharBackend *be = s->be;
if (!be || !be->chr_can_read) {
return 0;
}
return be->chr_can_read(be->opaque);
}
void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len)
{
CharBackend *be = s->be;
if (be && be->chr_read) {
be->chr_read(be->opaque, buf, len);
}
}
void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
{
if (s->replay) {
if (replay_mode == REPLAY_MODE_PLAY) {
return;
}
replay_chr_be_write(s, buf, len);
} else {
qemu_chr_be_write_impl(s, buf, len);
}
}
int qemu_chr_fe_get_msgfd(CharBackend *be)
{
CharDriverState *s = be->chr;
int fd;
int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1;
if (s && s->replay) {
fprintf(stderr,
"Replay: get msgfd is not supported for serial devices yet\n");
exit(1);
}
return res;
}
int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
{
CharDriverState *s = be->chr;
if (!s) {
return -1;
}
return s->get_msgfds ? s->get_msgfds(s, fds, len) : -1;
}
int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
{
CharDriverState *s = be->chr;
if (!s) {
return -1;
}
return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1;
}
int qemu_chr_add_client(CharDriverState *s, int fd)
{
return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
}
void qemu_chr_fe_accept_input(CharBackend *be)
{
CharDriverState *s = be->chr;
if (!s) {
return;
}
if (s->chr_accept_input)
s->chr_accept_input(s);
qemu_notify_event();
}
void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
{
char buf[READ_BUF_LEN];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
/* XXX this blocks entire thread. Rewrite to use
* qemu_chr_fe_write and background I/O callbacks */
qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf));
va_end(ap);
}
static void remove_fd_in_watch(CharDriverState *chr);
static void mux_chr_set_handlers(CharDriverState *chr, GMainContext *context);
static void mux_set_focus(MuxDriver *d, int focus);
static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
return len;
}
static CharDriverState *qemu_chr_open_null(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
CharDriverState *chr;
ChardevCommon *common = backend->u.null.data;
chr = qemu_chr_alloc(common, errp);
if (!chr) {
return NULL;
}
chr->chr_write = null_chr_write;
*be_opened = false;
return chr;
}
/* MUX driver for serial I/O splitting */
#define MAX_MUX 4
#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
struct MuxDriver {
CharBackend *backends[MAX_MUX];
CharBackend chr;
int focus;
int mux_cnt;
int term_got_escape;
int max_size;
/* Intermediate input buffer allows to catch escape sequences even if the
currently active device is not accepting any input - but only until it
is full as well. */
unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
int prod[MAX_MUX];
int cons[MAX_MUX];
int timestamps;
/* Protected by the CharDriverState chr_write_lock. */
int linestart;
int64_t timestamps_start;
};
/* Called with chr_write_lock held. */
static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
MuxDriver *d = chr->opaque;
int ret;
if (!d->timestamps) {
ret = qemu_chr_fe_write(&d->chr, buf, len);
} else {
int i;
ret = 0;
for (i = 0; i < len; i++) {
if (d->linestart) {
char buf1[64];
int64_t ti;
int secs;
ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
if (d->timestamps_start == -1)
d->timestamps_start = ti;
ti -= d->timestamps_start;
secs = ti / 1000;
snprintf(buf1, sizeof(buf1),
"[%02d:%02d:%02d.%03d] ",
secs / 3600,
(secs / 60) % 60,
secs % 60,
(int)(ti % 1000));
/* XXX this blocks entire thread. Rewrite to use
* qemu_chr_fe_write and background I/O callbacks */
qemu_chr_fe_write_all(&d->chr,
(uint8_t *)buf1, strlen(buf1));
d->linestart = 0;
}
ret += qemu_chr_fe_write(&d->chr, buf + i, 1);
if (buf[i] == '\n') {
d->linestart = 1;
}
}
}
return ret;
}
static const char * const mux_help[] = {
"% h print this help\n\r",
"% x exit emulator\n\r",
"% s save disk data back to file (if -snapshot)\n\r",
"% t toggle console timestamps\n\r",
"% b send break (magic sysrq)\n\r",
"% c switch between console and monitor\n\r",
"% % sends %\n\r",
NULL
};
int term_escape_char = 0x01; /* ctrl-a is used for escape */
static void mux_print_help(CharDriverState *chr)
{
int i, j;
char ebuf[15] = "Escape-Char";
char cbuf[50] = "\n\r";
if (term_escape_char > 0 && term_escape_char < 26) {
snprintf(cbuf, sizeof(cbuf), "\n\r");
snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
} else {
snprintf(cbuf, sizeof(cbuf),
"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
term_escape_char);
}
/* XXX this blocks entire thread. Rewrite to use
* qemu_chr_fe_write and background I/O callbacks */
qemu_chr_write_all(chr, (uint8_t *)cbuf, strlen(cbuf));
for (i = 0; mux_help[i] != NULL; i++) {
for (j=0; mux_help[i][j] != '\0'; j++) {
if (mux_help[i][j] == '%')
qemu_chr_write_all(chr, (uint8_t *)ebuf, strlen(ebuf));
else
qemu_chr_write_all(chr, (uint8_t *)&mux_help[i][j], 1);
}
}
}
static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
{
CharBackend *be = d->backends[mux_nr];
if (be && be->chr_event) {
be->chr_event(be->opaque, event);
}
}
static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
{
if (d->term_got_escape) {
d->term_got_escape = 0;
if (ch == term_escape_char)
goto send_char;
switch(ch) {
case '?':
case 'h':
mux_print_help(chr);
break;
case 'x':
{
const char *term = "QEMU: Terminated\n\r";
qemu_chr_write_all(chr, (uint8_t *)term, strlen(term));
exit(0);
break;
}
case 's':
blk_commit_all();
break;
case 'b':
qemu_chr_be_event(chr, CHR_EVENT_BREAK);
break;
case 'c':
assert(d->mux_cnt > 0); /* handler registered with first fe */
/* Switch to the next registered device */
mux_set_focus(d, (d->focus + 1) % d->mux_cnt);
break;
case 't':
d->timestamps = !d->timestamps;
d->timestamps_start = -1;
d->linestart = 0;
break;
}
} else if (ch == term_escape_char) {
d->term_got_escape = 1;
} else {
send_char:
return 1;
}
return 0;
}
static void mux_chr_accept_input(CharDriverState *chr)
{
MuxDriver *d = chr->opaque;
int m = d->focus;
CharBackend *be = d->backends[m];
while (be && d->prod[m] != d->cons[m] &&
be->chr_can_read && be->chr_can_read(be->opaque)) {
be->chr_read(be->opaque,
&d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
}
}
static int mux_chr_can_read(void *opaque)
{
CharDriverState *chr = opaque;
MuxDriver *d = chr->opaque;
int m = d->focus;
CharBackend *be = d->backends[m];
if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) {
return 1;
}
if (be && be->chr_can_read) {
return be->chr_can_read(be->opaque);
}
return 0;
}
static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
{
CharDriverState *chr = opaque;
MuxDriver *d = chr->opaque;
int m = d->focus;
CharBackend *be = d->backends[m];
int i;
mux_chr_accept_input(opaque);
for (i = 0; i < size; i++)
if (mux_proc_byte(chr, d, buf[i])) {
if (d->prod[m] == d->cons[m] &&
be && be->chr_can_read &&
be->chr_can_read(be->opaque))
be->chr_read(be->opaque, &buf[i], 1);
else
d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
}
}
static bool muxes_realized;
static void mux_chr_event(void *opaque, int event)
{
CharDriverState *chr = opaque;
MuxDriver *d = chr->opaque;
int i;
if (!muxes_realized) {
return;
}
/* Send the event to all registered listeners */
for (i = 0; i < d->mux_cnt; i++)
mux_chr_send_event(d, i, event);
}
/**
* Called after processing of default and command-line-specified
* chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
* to a mux chardev. This is done here to ensure that
* output/prompts/banners are only displayed for the FE that has
* focus when initial command-line processing/machine init is
* completed.
*
* After this point, any new FE attached to any new or existing
* mux will receive CHR_EVENT_OPENED notifications for the BE
* immediately.
*/
static void muxes_realize_done(Notifier *notifier, void *unused)
{
CharDriverState *chr;
QTAILQ_FOREACH(chr, &chardevs, next) {
if (chr->is_mux) {
MuxDriver *d = chr->opaque;
int i;
/* send OPENED to all already-attached FEs */
for (i = 0; i < d->mux_cnt; i++) {
mux_chr_send_event(d, i, CHR_EVENT_OPENED);
}
/* mark mux as OPENED so any new FEs will immediately receive
* OPENED event
*/
qemu_chr_be_generic_open(chr);
}
}
muxes_realized = true;
}
static Notifier muxes_realize_notify = {
.notify = muxes_realize_done,
};
static GSource *mux_chr_add_watch(CharDriverState *s, GIOCondition cond)
{
MuxDriver *d = s->opaque;
CharDriverState *chr = qemu_chr_fe_get_driver(&d->chr);
return chr->chr_add_watch(chr, cond);
}
static void mux_chr_free(struct CharDriverState *chr)
{
MuxDriver *d = chr->opaque;
int i;
for (i = 0; i < d->mux_cnt; i++) {
CharBackend *be = d->backends[i];
if (be) {
be->chr = NULL;
}
}
qemu_chr_fe_deinit(&d->chr);
g_free(d);
}
static void mux_chr_set_handlers(CharDriverState *chr, GMainContext *context)
{
MuxDriver *d = chr->opaque;
/* Fix up the real driver with mux routines */
qemu_chr_fe_set_handlers(&d->chr,
mux_chr_can_read,
mux_chr_read,
mux_chr_event,
chr,
context, true);
}
static void mux_set_focus(MuxDriver *d, int focus)
{
assert(focus >= 0);
assert(focus < d->mux_cnt);
if (d->focus != -1) {
mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
}
d->focus = focus;
mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
}
static CharDriverState *qemu_chr_open_mux(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
ChardevMux *mux = backend->u.mux.data;
CharDriverState *chr, *drv;
MuxDriver *d;
ChardevCommon *common = qapi_ChardevMux_base(mux);
drv = qemu_chr_find(mux->chardev);
if (drv == NULL) {
error_setg(errp, "mux: base chardev %s not found", mux->chardev);
return NULL;
}
chr = qemu_chr_alloc(common, errp);
if (!chr) {
return NULL;
}
d = g_new0(MuxDriver, 1);
chr->opaque = d;
d->focus = -1;
chr->chr_free = mux_chr_free;
chr->chr_write = mux_chr_write;
chr->chr_accept_input = mux_chr_accept_input;
/* Frontend guest-open / -close notification is not support with muxes */
chr->chr_set_fe_open = NULL;
if (drv->chr_add_watch) {
chr->chr_add_watch = mux_chr_add_watch;
}
/* only default to opened state if we've realized the initial
* set of muxes
*/
*be_opened = muxes_realized;
chr->is_mux = 1;
if (!qemu_chr_fe_init(&d->chr, drv, errp)) {
qemu_chr_free(chr);
return NULL;
}
return chr;
}
CharDriverState *qemu_chr_fe_get_driver(CharBackend *be)
{
return be->chr;
}
bool qemu_chr_fe_init(CharBackend *b, CharDriverState *s, Error **errp)
{
int tag = 0;
if (s->is_mux) {
MuxDriver *d = s->opaque;
if (d->mux_cnt >= MAX_MUX) {
goto unavailable;
}
d->backends[d->mux_cnt] = b;
tag = d->mux_cnt++;
} else if (s->be) {
goto unavailable;
} else {
s->be = b;
}
b->fe_open = false;
b->tag = tag;
b->chr = s;
return true;
unavailable:
error_setg(errp, QERR_DEVICE_IN_USE, s->label);
return false;
}
static bool qemu_chr_is_busy(CharDriverState *s)
{
if (s->is_mux) {
MuxDriver *d = s->opaque;
return d->mux_cnt >= 0;
} else {
return s->be != NULL;
}
}
void qemu_chr_fe_deinit(CharBackend *b)
{
assert(b);
if (b->chr) {
qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, true);
b->chr->be = NULL;
if (b->chr->is_mux) {
MuxDriver *d = b->chr->opaque;
d->backends[b->tag] = NULL;
}
b->chr = NULL;
}
}
void qemu_chr_fe_set_handlers(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
IOEventHandler *fd_event,
void *opaque,
GMainContext *context,
bool set_open)
{
CharDriverState *s;
int fe_open;
s = b->chr;
if (!s) {
return;
}
if (!opaque && !fd_can_read && !fd_read && !fd_event) {
fe_open = 0;
remove_fd_in_watch(s);
} else {
fe_open = 1;
}
b->chr_can_read = fd_can_read;
b->chr_read = fd_read;
b->chr_event = fd_event;
b->opaque = opaque;
if (s->chr_update_read_handler) {
s->chr_update_read_handler(s, context);
}
if (set_open) {
qemu_chr_fe_set_open(b, fe_open);
}
if (fe_open) {
qemu_chr_fe_take_focus(b);
/* We're connecting to an already opened device, so let's make sure we
also get the open event */
if (s->be_open) {
qemu_chr_be_generic_open(s);
}
}
if (s->is_mux) {
mux_chr_set_handlers(s, context);
}
}
void qemu_chr_fe_take_focus(CharBackend *b)
{
if (!b->chr) {
return;
}
if (b->chr->is_mux) {
mux_set_focus(b->chr->opaque, b->tag);
}
}
typedef struct IOWatchPoll
{
GSource parent;
QIOChannel *ioc;
GSource *src;
IOCanReadHandler *fd_can_read;
GSourceFunc fd_read;
void *opaque;
GMainContext *context;
} IOWatchPoll;
static IOWatchPoll *io_watch_poll_from_source(GSource *source)
{
return container_of(source, IOWatchPoll, parent);
}
static gboolean io_watch_poll_prepare(GSource *source,
gint *timeout_)
{
IOWatchPoll *iwp = io_watch_poll_from_source(source);
bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
bool was_active = iwp->src != NULL;
if (was_active == now_active) {
return FALSE;
}
if (now_active) {
iwp->src = qio_channel_create_watch(
iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
g_source_attach(iwp->src, iwp->context);
} else {
g_source_destroy(iwp->src);
g_source_unref(iwp->src);
iwp->src = NULL;
}
return FALSE;
}
static gboolean io_watch_poll_check(GSource *source)
{
return FALSE;
}
static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
gpointer user_data)
{
abort();
}
static void io_watch_poll_finalize(GSource *source)
{
/* Due to a glib bug, removing the last reference to a source
* inside a finalize callback causes recursive locking (and a
* deadlock). This is not a problem inside other callbacks,
* including dispatch callbacks, so we call io_remove_watch_poll
* to remove this source. At this point, iwp->src must
* be NULL, or we would leak it.
*
* This would be solved much more elegantly by child sources,
* but we support older glib versions that do not have them.
*/
IOWatchPoll *iwp = io_watch_poll_from_source(source);
assert(iwp->src == NULL);
(void)iwp;
}
static GSourceFuncs io_watch_poll_funcs = {
.prepare = io_watch_poll_prepare,
.check = io_watch_poll_check,
.dispatch = io_watch_poll_dispatch,
.finalize = io_watch_poll_finalize,
};
/* Can only be used for read */
static guint io_add_watch_poll(CharDriverState *chr,
QIOChannel *ioc,
IOCanReadHandler *fd_can_read,
QIOChannelFunc fd_read,
gpointer user_data,
GMainContext *context)
{
IOWatchPoll *iwp;
int tag;
char *name;
iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs,
sizeof(IOWatchPoll));
iwp->fd_can_read = fd_can_read;
iwp->opaque = user_data;
iwp->ioc = ioc;
iwp->fd_read = (GSourceFunc) fd_read;
iwp->src = NULL;
iwp->context = context;
name = g_strdup_printf("chardev-iowatch-%s", chr->label);
g_source_set_name((GSource *)iwp, name);
g_free(name);
tag = g_source_attach(&iwp->parent, context);
g_source_unref(&iwp->parent);
return tag;
}
static void io_remove_watch_poll(guint tag)
{
GSource *source;
IOWatchPoll *iwp;
g_return_if_fail (tag > 0);
source = g_main_context_find_source_by_id(NULL, tag);
g_return_if_fail (source != NULL);
iwp = io_watch_poll_from_source(source);
if (iwp->src) {
g_source_destroy(iwp->src);
g_source_unref(iwp->src);
iwp->src = NULL;
}
g_source_destroy(&iwp->parent);
}
static void remove_fd_in_watch(CharDriverState *chr)
{
if (chr->fd_in_tag) {
io_remove_watch_poll(chr->fd_in_tag);
chr->fd_in_tag = 0;
}
}
static int io_channel_send_full(QIOChannel *ioc,
const void *buf, size_t len,
int *fds, size_t nfds)
{
size_t offset = 0;
while (offset < len) {
ssize_t ret = 0;
struct iovec iov = { .iov_base = (char *)buf + offset,
.iov_len = len - offset };
ret = qio_channel_writev_full(
ioc, &iov, 1,
fds, nfds, NULL);
if (ret == QIO_CHANNEL_ERR_BLOCK) {
if (offset) {
return offset;
}
errno = EAGAIN;
return -1;
} else if (ret < 0) {
errno = EINVAL;
return -1;
}
offset += ret;
}
return offset;
}
#ifndef _WIN32
static int io_channel_send(QIOChannel *ioc, const void *buf, size_t len)
{
return io_channel_send_full(ioc, buf, len, NULL, 0);
}
typedef struct FDCharDriver {
CharDriverState *chr;
QIOChannel *ioc_in, *ioc_out;
int max_size;
} FDCharDriver;
/* Called with chr_write_lock held. */
static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
FDCharDriver *s = chr->opaque;
return io_channel_send(s->ioc_out, buf, len);
}
static gboolean fd_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
{
CharDriverState *chr = opaque;
FDCharDriver *s = chr->opaque;
int len;
uint8_t buf[READ_BUF_LEN];
ssize_t ret;
len = sizeof(buf);
if (len > s->max_size) {
len = s->max_size;
}
if (len == 0) {
return TRUE;
}
ret = qio_channel_read(
chan, (gchar *)buf, len, NULL);
if (ret == 0) {
remove_fd_in_watch(chr);
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
return FALSE;
}
if (ret > 0) {
qemu_chr_be_write(chr, buf, ret);
}
return TRUE;
}
static int fd_chr_read_poll(void *opaque)
{
CharDriverState *chr = opaque;
FDCharDriver *s = chr->opaque;
s->max_size = qemu_chr_be_can_write(chr);
return s->max_size;
}
static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
{
FDCharDriver *s = chr->opaque;
return qio_channel_create_watch(s->ioc_out, cond);
}
static void fd_chr_update_read_handler(CharDriverState *chr,
GMainContext *context)
{
FDCharDriver *s = chr->opaque;
remove_fd_in_watch(chr);
if (s->ioc_in) {
chr->fd_in_tag = io_add_watch_poll(chr, s->ioc_in,
fd_chr_read_poll,
fd_chr_read, chr,
context);
}
}
static void fd_chr_free(struct CharDriverState *chr)
{
FDCharDriver *s = chr->opaque;
remove_fd_in_watch(chr);
if (s->ioc_in) {
object_unref(OBJECT(s->ioc_in));
}
if (s->ioc_out) {
object_unref(OBJECT(s->ioc_out));
}
g_free(s);
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
/* open a character device to a unix fd */
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out,
ChardevCommon *backend, Error **errp)
{
CharDriverState *chr;
FDCharDriver *s;
char *name;
chr = qemu_chr_alloc(backend, errp);
if (!chr) {
return NULL;
}
s = g_new0(FDCharDriver, 1);
s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
name = g_strdup_printf("chardev-file-in-%s", chr->label);
qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
g_free(name);
s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out));
name = g_strdup_printf("chardev-file-out-%s", chr->label);
qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name);
g_free(name);
qemu_set_nonblock(fd_out);
s->chr = chr;
chr->opaque = s;
chr->chr_add_watch = fd_chr_add_watch;
chr->chr_write = fd_chr_write;
chr->chr_update_read_handler = fd_chr_update_read_handler;
chr->chr_free = fd_chr_free;
return chr;
}
static CharDriverState *qemu_chr_open_pipe(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
ChardevHostdev *opts = backend->u.pipe.data;
int fd_in, fd_out;
char *filename_in;
char *filename_out;
const char *filename = opts->device;
ChardevCommon *common = qapi_ChardevHostdev_base(opts);
filename_in = g_strdup_printf("%s.in", filename);
filename_out = g_strdup_printf("%s.out", filename);
TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
g_free(filename_in);
g_free(filename_out);
if (fd_in < 0 || fd_out < 0) {
if (fd_in >= 0)
close(fd_in);
if (fd_out >= 0)
close(fd_out);
TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
if (fd_in < 0) {
error_setg_file_open(errp, errno, filename);
return NULL;
}
}
return qemu_chr_open_fd(fd_in, fd_out, common, errp);
}
/* init terminal so that we can grab keys */
static struct termios oldtty;
static int old_fd0_flags;
static bool stdio_in_use;
static bool stdio_allow_signal;
static bool stdio_echo_state;
static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo);
static void term_exit(void)
{
tcsetattr (0, TCSANOW, &oldtty);
fcntl(0, F_SETFL, old_fd0_flags);
}
static void term_stdio_handler(int sig)
{
/* restore echo after resume from suspend. */
qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
}
static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
{
struct termios tty;
stdio_echo_state = echo;
tty = oldtty;
if (!echo) {
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
tty.c_oflag |= OPOST;
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_cflag |= CS8;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
}
if (!stdio_allow_signal)
tty.c_lflag &= ~ISIG;
tcsetattr (0, TCSANOW, &tty);
}
static void qemu_chr_free_stdio(struct CharDriverState *chr)
{
term_exit();
fd_chr_free(chr);
}
static CharDriverState *qemu_chr_open_stdio(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
ChardevStdio *opts = backend->u.stdio.data;
CharDriverState *chr;
struct sigaction act;
ChardevCommon *common = qapi_ChardevStdio_base(opts);
if (is_daemonized()) {
error_setg(errp, "cannot use stdio with -daemonize");
return NULL;
}
if (stdio_in_use) {
error_setg(errp, "cannot use stdio by multiple character devices");
return NULL;
}
stdio_in_use = true;
old_fd0_flags = fcntl(0, F_GETFL);
tcgetattr(0, &oldtty);
qemu_set_nonblock(0);
atexit(term_exit);
memset(&act, 0, sizeof(act));
act.sa_handler = term_stdio_handler;
sigaction(SIGCONT, &act, NULL);
chr = qemu_chr_open_fd(0, 1, common, errp);
if (!chr) {
return NULL;
}
chr->chr_free = qemu_chr_free_stdio;
chr->chr_set_echo = qemu_chr_set_echo_stdio;
if (opts->has_signal) {
stdio_allow_signal = opts->signal;
}
qemu_chr_set_echo_stdio(chr, false);
return chr;
}
#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|| defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
|| defined(__GLIBC__)
#define HAVE_CHARDEV_SERIAL 1
#define HAVE_CHARDEV_PTY 1
typedef struct {
QIOChannel *ioc;
int read_bytes;
/* Protected by the CharDriverState chr_write_lock. */
int connected;
guint timer_tag;
guint open_tag;
} PtyCharDriver;
static void pty_chr_update_read_handler_locked(CharDriverState *chr);
static void pty_chr_state(CharDriverState *chr, int connected);
static gboolean pty_chr_timer(gpointer opaque)
{
struct CharDriverState *chr = opaque;
PtyCharDriver *s = chr->opaque;
qemu_mutex_lock(&chr->chr_write_lock);
s->timer_tag = 0;
s->open_tag = 0;
if (!s->connected) {
/* Next poll ... */
pty_chr_update_read_handler_locked(chr);
}
qemu_mutex_unlock(&chr->chr_write_lock);
return FALSE;
}
/* Called with chr_write_lock held. */
static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
{
PtyCharDriver *s = chr->opaque;
char *name;
if (s->timer_tag) {
g_source_remove(s->timer_tag);
s->timer_tag = 0;
}
if (ms == 1000) {
name = g_strdup_printf("pty-timer-secs-%s", chr->label);
s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
} else {
name = g_strdup_printf("pty-timer-ms-%s", chr->label);
s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
}
g_source_set_name_by_id(s->timer_tag, name);
g_free(name);
}
/* Called with chr_write_lock held. */
static void pty_chr_update_read_handler_locked(CharDriverState *chr)
{
PtyCharDriver *s = chr->opaque;
GPollFD pfd;
int rc;
QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc);
pfd.fd = fioc->fd;
pfd.events = G_IO_OUT;
pfd.revents = 0;
do {
rc = g_poll(&pfd, 1, 0);
} while (rc == -1 && errno == EINTR);
assert(rc >= 0);
if (pfd.revents & G_IO_HUP) {
pty_chr_state(chr, 0);
} else {
pty_chr_state(chr, 1);
}
}
static void pty_chr_update_read_handler(CharDriverState *chr,
GMainContext *context)
{
qemu_mutex_lock(&chr->chr_write_lock);
pty_chr_update_read_handler_locked(chr);
qemu_mutex_unlock(&chr->chr_write_lock);
}
/* Called with chr_write_lock held. */
static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
PtyCharDriver *s = chr->opaque;
if (!s->connected) {
/* guest sends data, check for (re-)connect */
pty_chr_update_read_handler_locked(chr);
if (!s->connected) {
return 0;
}
}
return io_channel_send(s->ioc, buf, len);
}
static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
{
PtyCharDriver *s = chr->opaque;
if (!s->connected) {
return NULL;
}
return qio_channel_create_watch(s->ioc, cond);
}
static int pty_chr_read_poll(void *opaque)
{
CharDriverState *chr = opaque;
PtyCharDriver *s = chr->opaque;
s->read_bytes = qemu_chr_be_can_write(chr);
return s->read_bytes;
}
static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
{
CharDriverState *chr = opaque;
PtyCharDriver *s = chr->opaque;
gsize len;
uint8_t buf[READ_BUF_LEN];
ssize_t ret;
len = sizeof(buf);
if (len > s->read_bytes)
len = s->read_bytes;
if (len == 0) {
return TRUE;
}
ret = qio_channel_read(s->ioc, (char *)buf, len, NULL);
if (ret <= 0) {
pty_chr_state(chr, 0);
return FALSE;
} else {
pty_chr_state(chr, 1);
qemu_chr_be_write(chr, buf, ret);
}
return TRUE;
}
static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
{
CharDriverState *chr = opaque;
PtyCharDriver *s = chr->opaque;
s->open_tag = 0;
qemu_chr_be_generic_open(chr);
return FALSE;
}
/* Called with chr_write_lock held. */
static void pty_chr_state(CharDriverState *chr, int connected)
{
PtyCharDriver *s = chr->opaque;
if (!connected) {
if (s->open_tag) {
g_source_remove(s->open_tag);
s->open_tag = 0;
}
remove_fd_in_watch(chr);
s->connected = 0;
/* (re-)connect poll interval for idle guests: once per second.
* We check more frequently in case the guests sends data to
* the virtual device linked to our pty. */
pty_chr_rearm_timer(chr, 1000);
} else {
if (s->timer_tag) {
g_source_remove(s->timer_tag);
s->timer_tag = 0;
}
if (!s->connected) {
g_assert(s->open_tag == 0);
s->connected = 1;
s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
}
if (!chr->fd_in_tag) {
chr->fd_in_tag = io_add_watch_poll(chr, s->ioc,
pty_chr_read_poll,
pty_chr_read,
chr, NULL);
}
}
}
static void pty_chr_free(struct CharDriverState *chr)
{
PtyCharDriver *s = chr->opaque;
qemu_mutex_lock(&chr->chr_write_lock);
pty_chr_state(chr, 0);
object_unref(OBJECT(s->ioc));
if (s->timer_tag) {
g_source_remove(s->timer_tag);
s->timer_tag = 0;
}
qemu_mutex_unlock(&chr->chr_write_lock);
g_free(s);
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
static CharDriverState *qemu_chr_open_pty(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
CharDriverState *chr;
PtyCharDriver *s;
int master_fd, slave_fd;
char pty_name[PATH_MAX];
ChardevCommon *common = backend->u.pty.data;
char *name;
master_fd = qemu_openpty_raw(&slave_fd, pty_name);
if (master_fd < 0) {
error_setg_errno(errp, errno, "Failed to create PTY");
return NULL;
}
close(slave_fd);
qemu_set_nonblock(master_fd);
chr = qemu_chr_alloc(common, errp);
if (!chr) {
close(master_fd);
return NULL;
}
chr->filename = g_strdup_printf("pty:%s", pty_name);
ret->pty = g_strdup(pty_name);
ret->has_pty = true;
fprintf(stderr, "char device redirected to %s (label %s)\n",
pty_name, id);
s = g_new0(PtyCharDriver, 1);
chr->opaque = s;
chr->chr_write = pty_chr_write;
chr->chr_update_read_handler = pty_chr_update_read_handler;
chr->chr_free = pty_chr_free;
chr->chr_add_watch = pty_chr_add_watch;
*be_opened = false;
s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd));
name = g_strdup_printf("chardev-pty-%s", chr->label);
qio_channel_set_name(QIO_CHANNEL(s->ioc), name);
g_free(name);
s->timer_tag = 0;
return chr;
}
static void tty_serial_init(int fd, int speed,
int parity, int data_bits, int stop_bits)
{
struct termios tty;
speed_t spd;
#if 0
printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
speed, parity, data_bits, stop_bits);
#endif
tcgetattr (fd, &tty);
#define check_speed(val) if (speed <= val) { spd = B##val; break; }
speed = speed * 10 / 11;
do {
check_speed(50);
check_speed(75);
check_speed(110);
check_speed(134);
check_speed(150);
check_speed(200);
check_speed(300);
check_speed(600);
check_speed(1200);
check_speed(1800);
check_speed(2400);
check_speed(4800);
check_speed(9600);
check_speed(19200);
check_speed(38400);
/* Non-Posix values follow. They may be unsupported on some systems. */
check_speed(57600);
check_speed(115200);
#ifdef B230400
check_speed(230400);
#endif
#ifdef B460800
check_speed(460800);
#endif
#ifdef B500000
check_speed(500000);
#endif
#ifdef B576000
check_speed(576000);
#endif
#ifdef B921600
check_speed(921600);
#endif
#ifdef B1000000
check_speed(1000000);
#endif
#ifdef B1152000
check_speed(1152000);
#endif
#ifdef B1500000
check_speed(1500000);
#endif
#ifdef B2000000
check_speed(2000000);
#endif
#ifdef B2500000
check_speed(2500000);
#endif
#ifdef B3000000
check_speed(3000000);
#endif
#ifdef B3500000
check_speed(3500000);
#endif
#ifdef B4000000
check_speed(4000000);
#endif
spd = B115200;
} while (0);
cfsetispeed(&tty, spd);
cfsetospeed(&tty, spd);
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
tty.c_oflag |= OPOST;
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
switch(data_bits) {
default:
case 8:
tty.c_cflag |= CS8;
break;
case 7:
tty.c_cflag |= CS7;
break;
case 6:
tty.c_cflag |= CS6;
break;
case 5:
tty.c_cflag |= CS5;
break;
}
switch(parity) {
default:
case 'N':
break;
case 'E':
tty.c_cflag |= PARENB;
break;
case 'O':
tty.c_cflag |= PARENB | PARODD;
break;
}
if (stop_bits == 2)
tty.c_cflag |= CSTOPB;
tcsetattr (fd, TCSANOW, &tty);
}
static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
{
FDCharDriver *s = chr->opaque;
QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc_in);
switch(cmd) {
case CHR_IOCTL_SERIAL_SET_PARAMS:
{
QEMUSerialSetParams *ssp = arg;
tty_serial_init(fioc->fd,
ssp->speed, ssp->parity,
ssp->data_bits, ssp->stop_bits);
}
break;
case CHR_IOCTL_SERIAL_SET_BREAK:
{
int enable = *(int *)arg;
if (enable) {
tcsendbreak(fioc->fd, 1);
}
}
break;
case CHR_IOCTL_SERIAL_GET_TIOCM:
{
int sarg = 0;
int *targ = (int *)arg;
ioctl(fioc->fd, TIOCMGET, &sarg);
*targ = 0;
if (sarg & TIOCM_CTS)
*targ |= CHR_TIOCM_CTS;
if (sarg & TIOCM_CAR)
*targ |= CHR_TIOCM_CAR;
if (sarg & TIOCM_DSR)
*targ |= CHR_TIOCM_DSR;
if (sarg & TIOCM_RI)
*targ |= CHR_TIOCM_RI;
if (sarg & TIOCM_DTR)
*targ |= CHR_TIOCM_DTR;
if (sarg & TIOCM_RTS)
*targ |= CHR_TIOCM_RTS;
}
break;
case CHR_IOCTL_SERIAL_SET_TIOCM:
{
int sarg = *(int *)arg;
int targ = 0;
ioctl(fioc->fd, TIOCMGET, &targ);
targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
| CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
if (sarg & CHR_TIOCM_CTS)
targ |= TIOCM_CTS;
if (sarg & CHR_TIOCM_CAR)
targ |= TIOCM_CAR;
if (sarg & CHR_TIOCM_DSR)
targ |= TIOCM_DSR;
if (sarg & CHR_TIOCM_RI)
targ |= TIOCM_RI;
if (sarg & CHR_TIOCM_DTR)
targ |= TIOCM_DTR;
if (sarg & CHR_TIOCM_RTS)
targ |= TIOCM_RTS;
ioctl(fioc->fd, TIOCMSET, &targ);
}
break;
default:
return -ENOTSUP;
}
return 0;
}
static void qemu_chr_free_tty(CharDriverState *chr)
{
fd_chr_free(chr);
}
static CharDriverState *qemu_chr_open_tty_fd(int fd,
ChardevCommon *backend,
bool *be_opened,
Error **errp)
{
CharDriverState *chr;
tty_serial_init(fd, 115200, 'N', 8, 1);
chr = qemu_chr_open_fd(fd, fd, backend, errp);
if (!chr) {
return NULL;
}
chr->chr_ioctl = tty_serial_ioctl;
chr->chr_free = qemu_chr_free_tty;
return chr;
}
#endif /* __linux__ || __sun__ */
#if defined(__linux__)
#define HAVE_CHARDEV_PARPORT 1
typedef struct {
int fd;
int mode;
} ParallelCharDriver;
static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
{
if (s->mode != mode) {
int m = mode;
if (ioctl(s->fd, PPSETMODE, &m) < 0)
return 0;
s->mode = mode;
}
return 1;
}
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
{
ParallelCharDriver *drv = chr->opaque;
int fd = drv->fd;
uint8_t b;
switch(cmd) {
case CHR_IOCTL_PP_READ_DATA:
if (ioctl(fd, PPRDATA, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
case CHR_IOCTL_PP_WRITE_DATA:
b = *(uint8_t *)arg;
if (ioctl(fd, PPWDATA, &b) < 0)
return -ENOTSUP;
break;
case CHR_IOCTL_PP_READ_CONTROL:
if (ioctl(fd, PPRCONTROL, &b) < 0)
return -ENOTSUP;
/* Linux gives only the lowest bits, and no way to know data
direction! For better compatibility set the fixed upper
bits. */
*(uint8_t *)arg = b | 0xc0;
break;
case CHR_IOCTL_PP_WRITE_CONTROL:
b = *(uint8_t *)arg;
if (ioctl(fd, PPWCONTROL, &b) < 0)
return -ENOTSUP;
break;
case CHR_IOCTL_PP_READ_STATUS:
if (ioctl(fd, PPRSTATUS, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
case CHR_IOCTL_PP_DATA_DIR:
if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
return -ENOTSUP;
break;
case CHR_IOCTL_PP_EPP_READ_ADDR:
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
struct ParallelIOArg *parg = arg;
int n = read(fd, parg->buffer, parg->count);
if (n != parg->count) {
return -EIO;
}
}
break;
case CHR_IOCTL_PP_EPP_READ:
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
struct ParallelIOArg *parg = arg;
int n = read(fd, parg->buffer, parg->count);
if (n != parg->count) {
return -EIO;
}
}
break;
case CHR_IOCTL_PP_EPP_WRITE_ADDR:
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
struct ParallelIOArg *parg = arg;
int n = write(fd, parg->buffer, parg->count);
if (n != parg->count) {
return -EIO;
}
}
break;
case CHR_IOCTL_PP_EPP_WRITE:
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
struct ParallelIOArg *parg = arg;
int n = write(fd, parg->buffer, parg->count);
if (n != parg->count) {
return -EIO;
}
}
break;
default:
return -ENOTSUP;
}
return 0;
}
static void pp_free(CharDriverState *chr)
{
ParallelCharDriver *drv = chr->opaque;
int fd = drv->fd;
pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
ioctl(fd, PPRELEASE);
close(fd);
g_free(drv);
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
static CharDriverState *qemu_chr_open_pp_fd(int fd,
ChardevCommon *backend,
bool *be_opened,
Error **errp)
{
CharDriverState *chr;
ParallelCharDriver *drv;
if (ioctl(fd, PPCLAIM) < 0) {
error_setg_errno(errp, errno, "not a parallel port");
close(fd);
return NULL;
}
chr = qemu_chr_alloc(backend, errp);
if (!chr) {
return NULL;
}
drv = g_new0(ParallelCharDriver, 1);
chr->opaque = drv;
chr->chr_write = null_chr_write;
chr->chr_ioctl = pp_ioctl;
chr->chr_free = pp_free;
drv->fd = fd;
drv->mode = IEEE1284_MODE_COMPAT;
return chr;
}
#endif /* __linux__ */
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#define HAVE_CHARDEV_PARPORT 1
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
{
int fd = (int)(intptr_t)chr->opaque;
uint8_t b;
switch(cmd) {
case CHR_IOCTL_PP_READ_DATA:
if (ioctl(fd, PPIGDATA, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
case CHR_IOCTL_PP_WRITE_DATA:
b = *(uint8_t *)arg;
if (ioctl(fd, PPISDATA, &b) < 0)
return -ENOTSUP;
break;
case CHR_IOCTL_PP_READ_CONTROL:
if (ioctl(fd, PPIGCTRL, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
case CHR_IOCTL_PP_WRITE_CONTROL:
b = *(uint8_t *)arg;
if (ioctl(fd, PPISCTRL, &b) < 0)
return -ENOTSUP;
break;
case CHR_IOCTL_PP_READ_STATUS:
if (ioctl(fd, PPIGSTATUS, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
default:
return -ENOTSUP;
}
return 0;
}
static CharDriverState *qemu_chr_open_pp_fd(int fd,
ChardevCommon *backend,
bool *be_opened,
Error **errp)
{
CharDriverState *chr;
chr = qemu_chr_alloc(backend, errp);
if (!chr) {
return NULL;
}
chr->opaque = (void *)(intptr_t)fd;
chr->chr_write = null_chr_write;
chr->chr_ioctl = pp_ioctl;
*be_opened = false;
return chr;
}
#endif
#else /* _WIN32 */
#define HAVE_CHARDEV_SERIAL 1
typedef struct {
int max_size;
HANDLE hcom, hrecv, hsend;
OVERLAPPED orecv;
BOOL fpipe;
DWORD len;
/* Protected by the CharDriverState chr_write_lock. */
OVERLAPPED osend;
} WinCharState;
typedef struct {
HANDLE hStdIn;
HANDLE hInputReadyEvent;
HANDLE hInputDoneEvent;
HANDLE hInputThread;
uint8_t win_stdio_buf;
} WinStdioCharState;
#define NSENDBUF 2048
#define NRECVBUF 2048
#define MAXCONNECT 1
#define NTIMEOUT 5000
static int win_chr_poll(void *opaque);
static int win_chr_pipe_poll(void *opaque);
static void win_chr_free(CharDriverState *chr)
{
WinCharState *s = chr->opaque;
if (s->hsend) {
CloseHandle(s->hsend);
s->hsend = NULL;
}
if (s->hrecv) {
CloseHandle(s->hrecv);
s->hrecv = NULL;
}
if (s->hcom) {
CloseHandle(s->hcom);
s->hcom = NULL;
}
if (s->fpipe)
qemu_del_polling_cb(win_chr_pipe_poll, chr);
else
qemu_del_polling_cb(win_chr_poll, chr);
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}
static int win_chr_init(CharDriverState *chr, const char *filename, Error **errp)
{
WinCharState *s = chr->opaque;
COMMCONFIG comcfg;
COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
COMSTAT comstat;
DWORD size;
DWORD err;
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!s->hsend) {
error_setg(errp, "Failed CreateEvent");
goto fail;
}
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!s->hrecv) {
error_setg(errp, "Failed CreateEvent");
goto fail;
}
s->hcom = win32CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (s->hcom == INVALID_HANDLE_VALUE) {
error_setg(errp, "Failed CreateFile (%lu)", GetLastError());
s->hcom = NULL;
goto fail;
}
if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
error_setg(errp, "Failed SetupComm");
goto fail;
}
ZeroMemory(&comcfg, sizeof(COMMCONFIG));
size = sizeof(COMMCONFIG);
GetDefaultCommConfig(filename, &comcfg, &size);
comcfg.dcb.DCBlength = sizeof(DCB);
CommConfigDialog(filename, NULL, &comcfg);
if (!SetCommState(s->hcom, &comcfg.dcb)) {
error_setg(errp, "Failed SetCommState");
goto fail;
}
if (!SetCommMask(s->hcom, EV_ERR)) {
error_setg(errp, "Failed SetCommMask");
goto fail;
}
cto.ReadIntervalTimeout = MAXDWORD;
if (!SetCommTimeouts(s->hcom, &cto)) {
error_setg(errp, "Failed SetCommTimeouts");
goto fail;
}
if (!ClearCommError(s->hcom, &err, &comstat)) {
error_setg(errp, "Failed ClearCommError");
goto fail;
}
qemu_add_polling_cb(win_chr_poll, chr);
return 0;
fail:
win_chr_free(chr);
return -1;
}
/* Called with chr_write_lock held. */
static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
{
WinCharState *s = chr->opaque;
DWORD len, ret, size, err;
len = len1;
ZeroMemory(&s->osend, sizeof(s->osend));
s->osend.hEvent = s->hsend;
while (len > 0) {
if (s->hsend)
ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
else
ret = WriteFile(s->hcom, buf, len, &size, NULL);
if (!ret) {
err = GetLastError();
if (err == ERROR_IO_PENDING) {
ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
if (ret) {
buf += size;
len -= size;
} else {
break;
}
} else {
break;
}
} else {
buf += size;
len -= size;
}
}
return len1 - len;
}
static int win_chr_read_poll(CharDriverState *chr)
{
WinCharState *s = chr->opaque;
s->max_size = qemu_chr_be_can_write(chr);
return s->max_size;
}
static void win_chr_readfile(CharDriverState *chr)
{
WinCharState *s = chr->opaque;
int ret, err;
uint8_t buf[READ_BUF_LEN];
DWORD size;
ZeroMemory(&s->orecv, sizeof(s->orecv));
s->orecv.hEvent = s->hrecv;
ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
if (!ret) {
err = GetLastError();
if (err == ERROR_IO_PENDING) {
ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
}
}
if (size > 0) {
qemu_chr_be_write(chr, buf, size);
}
}
static void win_chr_read(CharDriverState *chr)
{
WinCharState *s = chr->opaque;
if (s->len > s->max_size)
s->len = s->max_size;
if (s->len == 0)
return;
win_chr_readfile(chr);
}
static int win_chr_poll(void *opaque)
{
CharDriverState *chr = opaque;
WinCharState *s = chr->opaque;
COMSTAT status;
DWORD comerr;
ClearCommError(s->hcom, &comerr, &status);
if (status.cbInQue > 0) {
s->len = status.cbInQue;
win_chr_read_poll(chr);
win_chr_read(chr);
return 1;
}
return 0;
}
static CharDriverState *qemu_chr_open_win_path(const char *filename,
ChardevCommon *backend,
Error **errp)
{
CharDriverState *chr;
WinCharState *s;
chr = qemu_chr_alloc(backend, errp);
if (!chr) {
return NULL;
}
s = g_new0(WinCharState, 1);
chr->opaque = s;
chr->chr_write = win_chr_write;
chr->chr_free = win_chr_free;
if (win_chr_init(chr, filename, errp) < 0) {
g_free(s);
qemu_chr_free_common(chr);
return NULL;
}
return chr;
}
static int win_chr_pipe_poll(void *opaque)
{
CharDriverState *chr = opaque;
WinCharState *s = chr->opaque;
DWORD size;
PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
if (size > 0) {
s->len = size;
win_chr_read_poll(chr);
win_chr_read(chr);
return 1;
}
return 0;
}
static int win_chr_pipe_init(CharDriverState *chr, const char *filename,
Error **errp)
{
WinCharState *s = chr->opaque;
OVERLAPPED ov;
int ret;
DWORD size;
char *openname;
s->fpipe = TRUE;
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!s->hsend) {
error_setg(errp, "Failed CreateEvent");
goto fail;
}
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!s->hrecv) {
error_setg(errp, "Failed CreateEvent");
goto fail;
}
openname = g_strdup_printf("\\\\.\\pipe\\%s", filename);
s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
PIPE_WAIT,
MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
g_free(openname);
if (s->hcom == INVALID_HANDLE_VALUE) {
error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
s->hcom = NULL;
goto fail;
}
ZeroMemory(&ov, sizeof(ov));
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ret = ConnectNamedPipe(s->hcom, &ov);
if (ret) {
error_setg(errp, "Failed ConnectNamedPipe");
goto fail;
}
ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
if (!ret) {
error_setg(errp, "Failed GetOverlappedResult");
if (ov.hEvent) {
CloseHandle(ov.hEvent);
ov.hEvent = NULL;
}
goto fail;
}
if (ov.hEvent) {
CloseHandle(ov.hEvent);
ov.hEvent = NULL;
}
qemu_add_polling_cb(win_chr_pipe_poll, chr);
return 0;
fail:
win_chr_free(chr);
return -1;
}
static CharDriverState *qemu_chr_open_pipe(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
ChardevHostdev *opts = backend->u.pipe.data;
const char *filename = opts->device;
CharDriverState *chr;
WinCharState *s;
ChardevCommon *common = qapi_ChardevHostdev_base(opts);
chr = qemu_chr_alloc(common, errp);
if (!chr) {
return NULL;
}
s = g_new0(WinCharState, 1);
chr->opaque = s;
chr->chr_write = win_chr_write;
chr->chr_free = win_chr_free;
if (win_chr_pipe_init(chr, filename, errp) < 0) {
g_free(s);
qemu_chr_free_common(chr);
return NULL;
}
return chr;
}
static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out,
ChardevCommon *backend,
Error **errp)
{
CharDriverState *chr;
WinCharState *s;
chr = qemu_chr_alloc(backend, errp);
if (!chr) {
return NULL;
}
s = g_new0(WinCharState, 1);
s->hcom = fd_out;
chr->opaque = s;
chr->chr_write = win_chr_write;
return chr;
}
static CharDriverState *qemu_chr_open_win_con(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
ChardevCommon *common = backend->u.console.data;
return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE),
common, errp);
}
static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwSize;
int len1;
len1 = len;
while (len1 > 0) {
if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
break;
}
buf += dwSize;
len1 -= dwSize;
}
return len - len1;
}
static void win_stdio_wait_func(void *opaque)
{
CharDriverState *chr = opaque;
WinStdioCharState *stdio = chr->opaque;
INPUT_RECORD buf[4];
int ret;
DWORD dwSize;
int i;
// On Windows QEMU may share its console window with other processes,
// e.g. cmd.exe which launched it. This means we can't be sure that
// any console event we were told about is still there (other process may
// have already handled it), and instead of getting the event QEMU might
// just block on the following ReadConsoleInput() call.
// So let's first check if there's anything left to read.
DWORD eventCount = 0;
BOOL getRet = GetNumberOfConsoleInputEvents(stdio->hStdIn, &eventCount);
if (!getRet) {
fprintf(stderr, "GetNumberOfConsoleInputEvents() API call failed,"
" code %lu\n", GetLastError());
return;
}
if (eventCount == 0) {
// we're too late, nothing to read
return;
}
ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
if (!ret) {
/* Avoid error storm */
qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
return;
}
for (i = 0; i < dwSize; i++) {
KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
int j;
if (kev->uChar.AsciiChar != 0) {
for (j = 0; j < kev->wRepeatCount; j++) {
if (qemu_chr_be_can_write(chr)) {
uint8_t c = kev->uChar.AsciiChar;
qemu_chr_be_write(chr, &c, 1);
}
}
}
}
}
}
static DWORD WINAPI win_stdio_thread(LPVOID param)
{
CharDriverState *chr = param;
WinStdioCharState *stdio = chr->opaque;
int ret;
DWORD dwSize;
while (1) {
/* Wait for one byte */
ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
/* Exit in case of error, continue if nothing read */
if (!ret) {
break;
}
if (!dwSize) {
continue;
}
/* Some terminal emulator returns \r\n for Enter, just pass \n */
if (stdio->win_stdio_buf == '\r') {
continue;
}
/* Signal the main thread and wait until the byte was eaten */
if (!SetEvent(stdio->hInputReadyEvent)) {
break;
}
if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
!= WAIT_OBJECT_0) {
break;
}
}
qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
return 0;
}
static void win_stdio_thread_wait_func(void *opaque)
{
CharDriverState *chr = opaque;
WinStdioCharState *stdio = chr->opaque;
if (qemu_chr_be_can_write(chr)) {
qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
}
SetEvent(stdio->hInputDoneEvent);
}
static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
{
WinStdioCharState *stdio = chr->opaque;
DWORD dwMode = 0;
GetConsoleMode(stdio->hStdIn, &dwMode);
if (echo) {
SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
} else {
SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
}
}
static void win_stdio_free(CharDriverState *chr)
{
WinStdioCharState *stdio = chr->opaque;
if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
CloseHandle(stdio->hInputReadyEvent);
}
if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
CloseHandle(stdio->hInputDoneEvent);
}
if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
TerminateThread(stdio->hInputThread, 0);
}
g_free(chr->opaque);
}
static CharDriverState *qemu_chr_open_stdio(const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Error **errp)
{
CharDriverState *chr;
WinStdioCharState *stdio;
DWORD dwMode;
int is_console = 0;
ChardevCommon *common = qapi_ChardevStdio_base(backend->u.stdio.data);
chr = qemu_chr_alloc(common, errp);
if (!chr) {
return NULL;
}
stdio = g_new0(WinStdioCharState, 1);
stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
error_setg(errp, "cannot open stdio: invalid handle");
return NULL;
}
is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
chr->opaque = stdio;
chr->chr_write = win_stdio_write;
chr->chr_free = win_stdio_free;
if (is_console) {
if (qemu_add_wait_object(stdio->hStdIn,
win_stdio_wait_func, chr)) {
error_setg(errp, "qemu_add_wait_object: failed");
goto err1;
}
} else {
DWORD dwId;
stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
|| stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
error_setg(errp, "cannot create event");
goto err2;
}
if (qemu_add_wait_object(stdio->hInputReadyEvent,
win_stdio_thread_wait_func, chr)) {
error_setg(errp, "qemu_add_wait_object: failed");
goto err2;
}
stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
chr, 0, &dwId);
if (stdio->hInputThread == INVALID_HANDLE_VALUE) {
error_setg(errp, "cannot create stdio thread");
goto err3;
}
}
dwMode |= ENABLE_LINE_INPUT;
if (is_console) {
/* set the terminal in raw mode */
/* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */