Skip to content

Commit

Permalink
char: introduce CharBackend
Browse files Browse the repository at this point in the history
This new structure is meant to keep the details associated with a char
driver usage. On initialization, it gets a tag from the mux backend.
It can change its handlers thanks to qemu_chr_fe_set_handlers().

This structure is introduced so that all frontend will be moved to hold
and use a CharBackend. This will allow to better track char usage and
allocation, and help prevent some memory leaks or corruption.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20161022095318.17775-10-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
elmarco authored and bonzini committed Oct 24, 2016
1 parent 6dfa829 commit 94a40fc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
50 changes: 50 additions & 0 deletions include/sysemu/char.h
Expand Up @@ -72,6 +72,12 @@ typedef enum {
QEMU_CHAR_FEATURE_LAST,
} CharDriverFeature;

/* This is the backend as seen by frontend, the actual backend is
* CharDriverState */
typedef struct CharBackend {
CharDriverState *chr;
int tag;
} CharBackend;

struct CharDriverState {
QemuMutex chr_write_lock;
Expand Down Expand Up @@ -156,6 +162,8 @@ void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
* Returns: a new character backend
*/
CharDriverState *qemu_chr_new(const char *label, const char *filename);


/**
* @qemu_chr_disconnect:
*
Expand Down Expand Up @@ -425,6 +433,48 @@ void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len);
*/
void qemu_chr_be_event(CharDriverState *s, int event);

/**
* @qemu_chr_fe_init:
*
* Initializes a front end for the given CharBackend and CharDriver.
*
* Returns: false on error.
*/
bool qemu_chr_fe_init(CharBackend *b, CharDriverState *s, Error **errp);

/**
* @qemu_chr_fe_get_driver:
*
* Returns the driver associated with a CharBackend or NULL.
*/
CharDriverState *qemu_chr_fe_get_driver(CharBackend *be);

/**
* @qemu_chr_fe_set_handlers:
* @b: a CharBackend
* @fd_can_read: callback to get the amount of data the frontend may
* receive
* @fd_read: callback to receive data from char
* @fd_event: event callback
* @opaque: an opaque pointer for the callbacks
* @context: a main loop context or NULL for the default
*
* Set the front end char handlers.
*/
void qemu_chr_fe_set_handlers(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
IOEventHandler *fd_event,
void *opaque,
GMainContext *context);

/**
* @qemu_chr_fe_take_focus:
*
* Take the focus (if the front end is muxed)
*/
void qemu_chr_fe_take_focus(CharBackend *b);

void qemu_chr_add_handlers(CharDriverState *s,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
Expand Down
50 changes: 50 additions & 0 deletions qemu-char.c
Expand Up @@ -910,6 +910,53 @@ static CharDriverState *qemu_chr_open_mux(const char *id,
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) {
tag = mux_chr_new_handler_tag(s, errp);
if (tag < 0) {
return false;
}
}

b->tag = tag;
b->chr = s;

return true;
}

void qemu_chr_fe_set_handlers(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
IOEventHandler *fd_event,
void *opaque,
GMainContext *context)
{
if (!b->chr) {
return;
}

qemu_chr_set_handlers(b->chr, fd_can_read, fd_read,
fd_event, opaque, context, b->tag);

if (b->chr->is_mux) {
mux_chr_set_handlers(b->chr, context);
}
}

void qemu_chr_fe_take_focus(CharBackend *b)
{
if (b->chr->is_mux) {
mux_set_focus(b->chr->opaque, b->tag);
}
}

typedef struct IOWatchPoll
{
Expand Down Expand Up @@ -4184,6 +4231,9 @@ void qemu_chr_disconnect(CharDriverState *chr)

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) {
Expand Down

0 comments on commit 94a40fc

Please sign in to comment.