* CharDriverState */
typedef struct CharBackend {
CharDriverState *chr;
+ IOEventHandler *chr_event;
+ IOCanReadHandler *chr_can_read;
+ IOReadHandler *chr_read;
+ void *opaque;
int tag;
} CharBackend;
const uint8_t *buf, int len);
GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
void (*chr_update_read_handler)(struct CharDriverState *s,
- GMainContext *context, int tag);
+ GMainContext *context);
int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
int (*chr_add_client)(struct CharDriverState *chr, int fd);
int (*chr_wait_connected)(struct CharDriverState *chr, Error **errp);
- IOEventHandler *chr_event;
- IOCanReadHandler *chr_can_read;
- IOReadHandler *chr_read;
- void *handler_opaque;
void (*chr_close)(struct CharDriverState *chr);
void (*chr_disconnect)(struct CharDriverState *chr);
void (*chr_accept_input)(struct CharDriverState *chr);
void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
void (*chr_fe_event)(struct CharDriverState *chr, int event);
+ CharBackend *be;
void *opaque;
char *label;
char *filename;
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:
break;
}
- if (!s->chr_event)
+ if (!be || !be->chr_event) {
return;
- s->chr_event(s->handler_opaque, event);
+ }
+
+ be->chr_event(be->opaque, event);
}
void qemu_chr_be_generic_open(CharDriverState *s)
int qemu_chr_be_can_write(CharDriverState *s)
{
- if (!s->chr_can_read)
+ CharBackend *be = s->be;
+
+ if (!be || !be->chr_can_read) {
return 0;
- return s->chr_can_read(s->handler_opaque);
+ }
+
+ return be->chr_can_read(be->opaque);
}
void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len)
{
- if (s->chr_read) {
- s->chr_read(s->handler_opaque, buf, len);
+ CharBackend *be = s->be;
+
+ if (be && be->chr_read) {
+ be->chr_read(be->opaque, buf, len);
}
}
}
static void remove_fd_in_watch(CharDriverState *chr);
-static int mux_chr_new_handler_tag(CharDriverState *chr, Error **errp);
static void mux_chr_set_handlers(CharDriverState *chr, GMainContext *context);
static void mux_set_focus(MuxDriver *d, int focus);
#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
struct MuxDriver {
- IOCanReadHandler *chr_can_read[MAX_MUX];
- IOReadHandler *chr_read[MAX_MUX];
- IOEventHandler *chr_event[MAX_MUX];
- void *ext_opaque[MAX_MUX];
+ CharBackend *backends[MAX_MUX];
CharBackend chr;
int focus;
int mux_cnt;
static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
{
- if (d->chr_event[mux_nr])
- d->chr_event[mux_nr](d->ext_opaque[mux_nr], 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)
{
MuxDriver *d = chr->opaque;
int m = d->focus;
+ CharBackend *be = d->backends[m];
- while (d->prod[m] != d->cons[m] &&
- d->chr_can_read[m] &&
- d->chr_can_read[m](d->ext_opaque[m])) {
- d->chr_read[m](d->ext_opaque[m],
- &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
+ 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);
}
}
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)
+ if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) {
return 1;
- if (d->chr_can_read[m])
- return d->chr_can_read[m](d->ext_opaque[m]);
+ }
+
+ if (be && be->chr_can_read) {
+ return be->chr_can_read(be->opaque);
+ }
+
return 0;
}
CharDriverState *chr = opaque;
MuxDriver *d = chr->opaque;
int m = d->focus;
+ CharBackend *be = d->backends[m];
int i;
- mux_chr_accept_input (opaque);
+ mux_chr_accept_input(opaque);
- for(i = 0; i < size; i++)
+ for (i = 0; i < size; i++)
if (mux_proc_byte(chr, d, buf[i])) {
if (d->prod[m] == d->cons[m] &&
- d->chr_can_read[m] &&
- d->chr_can_read[m](d->ext_opaque[m]))
- d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
+ 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];
}
mux_chr_send_event(d, i, event);
}
-static void mux_chr_update_read_handler(CharDriverState *chr,
- GMainContext *context,
- int tag)
-{
- MuxDriver *d = chr->opaque;
-
- assert(tag >= 0);
- assert(tag < d->mux_cnt);
-
- d->ext_opaque[tag] = chr->handler_opaque;
- d->chr_can_read[tag] = chr->chr_can_read;
- d->chr_read[tag] = chr->chr_read;
- d->chr_event[tag] = chr->chr_event;
-}
-
static bool muxes_realized;
/**
static void mux_chr_close(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 int mux_chr_new_handler_tag(CharDriverState *chr, Error **errp)
+static int mux_chr_new_fe(CharDriverState *chr, CharBackend *be, Error **errp)
{
MuxDriver *d = chr->opaque;
return -1;
}
+ d->backends[d->mux_cnt] = be;
+
return d->mux_cnt++;
}
d->focus = -1;
chr->chr_close = mux_chr_close;
chr->chr_write = mux_chr_write;
- chr->chr_update_read_handler = mux_chr_update_read_handler;
chr->chr_accept_input = mux_chr_accept_input;
/* Frontend guest-open / -close notification is not support with muxes */
chr->chr_set_fe_open = NULL;
int tag = 0;
if (s->is_mux) {
- tag = mux_chr_new_handler_tag(s, errp);
+ tag = mux_chr_new_fe(s, b, errp);
if (tag < 0) {
return false;
}
+ } else {
+ s->be = b;
}
b->tag = tag;
return true;
}
+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);
b->chr->avail_connections++;
+ b->chr->be = NULL;
+ if (b->chr->is_mux) {
+ MuxDriver *d = b->chr->opaque;
+ d->backends[b->tag] = NULL;
+ }
b->chr = NULL;
}
}
} else {
fe_open = 1;
}
- s->chr_can_read = fd_can_read;
- s->chr_read = fd_read;
- s->chr_event = fd_event;
- s->handler_opaque = opaque;
+ 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, b->tag);
+ s->chr_update_read_handler(s, context);
}
if (!s->explicit_fe_open) {
}
static void fd_chr_update_read_handler(CharDriverState *chr,
- GMainContext *context,
- int tag)
+ GMainContext *context)
{
FDCharDriver *s = chr->opaque;
}
static void pty_chr_update_read_handler(CharDriverState *chr,
- GMainContext *context,
- int tag)
+ GMainContext *context)
{
qemu_mutex_lock(&chr->chr_write_lock);
pty_chr_update_read_handler_locked(chr);
}
static void udp_chr_update_read_handler(CharDriverState *chr,
- GMainContext *context,
- int tag)
+ GMainContext *context)
{
NetCharDriver *s = chr->opaque;
}
static void tcp_chr_update_read_handler(CharDriverState *chr,
- GMainContext *context,
- int tag)
+ GMainContext *context)
{
TCPCharDriver *s = chr->opaque;
static void qemu_chr_free_common(CharDriverState *chr)
{
+ if (chr->be) {
+ chr->be->chr = NULL;
+ }
g_free(chr->filename);
g_free(chr->label);
if (chr->logfd != -1) {
error_setg(errp, "Chardev '%s' not found", id);
return;
}
- if (chr->chr_can_read || chr->chr_read ||
- chr->chr_event || chr->handler_opaque) {
+ if (qemu_chr_is_busy(chr)) {
error_setg(errp, "Chardev '%s' is busy", id);
return;
}