Make screendump async to provide correct screendumps. HMP doesn't have async support, so it has to remain sync to avoid potential races.
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1230527 Signed-off-by: Marc-André Lureau <[email protected]> --- hmp.c | 2 +- ui/console.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/ui/console.h | 1 + qapi-schema.json | 11 ++++++++- 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/hmp.c b/hmp.c index 336e7bf..b161453 100644 --- a/hmp.c +++ b/hmp.c @@ -1809,7 +1809,7 @@ void hmp_screendump(Monitor *mon, const QDict *qdict) const char *filename = qdict_get_str(qdict, "filename"); Error *err = NULL; - qmp_screendump(filename, &err); + hmp_screendump_sync(filename, &err); hmp_handle_error(mon, &err); } diff --git a/ui/console.c b/ui/console.c index 7ca5d19..85f099c 100644 --- a/ui/console.c +++ b/ui/console.c @@ -114,6 +114,12 @@ typedef enum { TEXT_CONSOLE_FIXED_SIZE } console_type_t; +struct qmp_screendump { + gchar *filename; + QmpReturn *ret; + QLIST_ENTRY(qmp_screendump) link; +}; + struct QemuConsole { Object parent; @@ -162,6 +168,8 @@ struct QemuConsole { QEMUFIFO out_fifo; uint8_t out_fifo_buf[16]; QEMUTimer *kbd_timer; + + QLIST_HEAD(, qmp_screendump) qmp_screendumps; }; struct DisplayState { @@ -187,6 +195,8 @@ static void dpy_refresh(DisplayState *s); static DisplayState *get_alloc_displaystate(void); static void text_console_update_cursor_timer(void); static void text_console_update_cursor(void *opaque); +static void ppm_save(const char *filename, DisplaySurface *ds, + Error **errp); static void gui_update(void *opaque) { @@ -253,8 +263,39 @@ static void gui_setup_refresh(DisplayState *ds) ds->have_text = have_text; } +static void qmp_screendump_finish(QemuConsole *con, const char *filename, + QmpReturn *ret) +{ + Error *err = NULL; + DisplaySurface *surface; + + if (qmp_return_is_cancelled(ret)) { + return; + } + + surface = qemu_console_surface(con); + + /* FIXME: async save with coroutine? it would have to copy or lock + * the surface. */ + ppm_save(filename, surface, &err); + + if (err) { + qmp_return_error(ret, err); + } else { + qmp_screendump_return(ret); + } +} + void graphic_hw_update_done(QemuConsole *con) { + struct qmp_screendump *dump, *next; + + QLIST_FOREACH_SAFE(dump, &con->qmp_screendumps, link, next) { + qmp_screendump_finish(con, dump->filename, dump->ret); + g_free(dump->filename); + QLIST_REMOVE(dump, link); + g_free(dump); + } } bool graphic_hw_update(QemuConsole *con) @@ -345,7 +386,8 @@ write_err: goto out; } -void qmp_screendump(const char *filename, Error **errp) +/* this sync screendump may produce outdated dumps: use qmp instead */ +void hmp_screendump_sync(const char *filename, Error **errp) { QemuConsole *con = qemu_console_lookup_by_index(0); DisplaySurface *surface; @@ -360,6 +402,29 @@ void qmp_screendump(const char *filename, Error **errp) ppm_save(filename, surface, errp); } +void qmp_screendump(const char *filename, QmpReturn *qret) +{ + QemuConsole *con = qemu_console_lookup_by_index(0); + bool async; + Error *err = NULL; + + if (con == NULL) { + error_setg(&err, "There is no QemuConsole I can screendump from."); + qmp_return_error(qret, err); + return; + } + + async = graphic_hw_update(con); + if (async) { + struct qmp_screendump *dump = g_new(struct qmp_screendump, 1); + dump->filename = g_strdup(filename); + dump->ret = qret; + QLIST_INSERT_HEAD(&con->qmp_screendumps, dump, link); + } else { + qmp_screendump_finish(con, filename, qret); + } +} + void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata) { if (!con) { @@ -1235,6 +1300,7 @@ static QemuConsole *new_console(DisplayState *ds, console_type_t console_type, obj = object_new(TYPE_QEMU_CONSOLE); s = QEMU_CONSOLE(obj); s->head = head; + QLIST_INIT(&s->qmp_screendumps); object_property_add_link(obj, "device", TYPE_DEVICE, (Object **)&s->device, object_property_allow_set_link, diff --git a/include/ui/console.h b/include/ui/console.h index fd9f648..7c4f35b 100644 --- a/include/ui/console.h +++ b/include/ui/console.h @@ -75,6 +75,7 @@ struct MouseTransformInfo { }; void hmp_mouse_set(Monitor *mon, const QDict *qdict); +void hmp_screendump_sync(const char *filename, Error **errp); /* keysym is a unicode code except for special keys (see QEMU_KEY_xxx constants) */ diff --git a/qapi-schema.json b/qapi-schema.json index 117ace8..80e9aeb 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -3570,9 +3570,18 @@ # # Returns: Nothing on success # +# Note: the operation can be cancelled and the file not written if the +# client disconnects before completion. +# # Since: 0.14.0 +# +# Example: +# +# -> { "execute": "screendump", "arguments": { "filename": "/tmp/image" }, id: X } +# <- { "return": {}, id: X } +# ## -{ 'command': 'screendump', 'data': {'filename': 'str'} } +{ 'command': 'screendump', 'data': {'filename': 'str'}, 'async': true } ## -- 2.10.0
