[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 2/7] ppm-save: pass opened fd
From: |
Marc-André Lureau |
Subject: |
[PULL 2/7] ppm-save: pass opened fd |
Date: |
Thu, 2 Jan 2020 15:54:54 +0400 |
This will allow to pre-open the file before running the async finish
handler and avoid potential monitor fdset races.
(note: this is preliminary work for asynchronous screendump support)
Signed-off-by: Marc-André Lureau <address@hidden>
Reviewed-by: Daniel P. Berrangé <address@hidden>
---
ui/console.c | 45 ++++++++++++++++++++++-----------------------
ui/trace-events | 2 +-
2 files changed, 23 insertions(+), 24 deletions(-)
diff --git a/ui/console.c b/ui/console.c
index 3c941528d2..77d62fe76d 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -193,6 +193,7 @@ 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 bool ppm_save(int fd, DisplaySurface *ds, Error **errp);
static void gui_update(void *opaque)
{
@@ -308,29 +309,22 @@ void graphic_hw_invalidate(QemuConsole *con)
}
}
-static void ppm_save(const char *filename, DisplaySurface *ds,
- Error **errp)
+static bool ppm_save(int fd, DisplaySurface *ds, Error **errp)
{
int width = pixman_image_get_width(ds->image);
int height = pixman_image_get_height(ds->image);
- int fd;
FILE *f;
int y;
int ret;
pixman_image_t *linebuf;
+ bool success = false;
- trace_ppm_save(filename, ds);
- fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
- if (fd == -1) {
- error_setg(errp, "failed to open file '%s': %s", filename,
- strerror(errno));
- return;
- }
+ trace_ppm_save(fd, ds);
f = fdopen(fd, "wb");
ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
if (ret < 0) {
linebuf = NULL;
- goto write_err;
+ goto end;
}
linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
for (y = 0; y < height; y++) {
@@ -339,21 +333,16 @@ static void ppm_save(const char *filename, DisplaySurface
*ds,
ret = fwrite(pixman_image_get_data(linebuf), 1,
pixman_image_get_stride(linebuf), f);
(void)ret;
- if (ferror(f)) {
- goto write_err;
- }
+ success = !ferror(f);
}
-out:
+end:
+ if (!success) {
+ error_setg(errp, "failed to write to PPM file: %s", strerror(errno));
+ }
qemu_pixman_image_unref(linebuf);
fclose(f);
- return;
-
-write_err:
- error_setg(errp, "failed to write to file '%s': %s", filename,
- strerror(errno));
- unlink(filename);
- goto out;
+ return success;
}
void qmp_screendump(const char *filename, bool has_device, const char *device,
@@ -361,6 +350,7 @@ void qmp_screendump(const char *filename, bool has_device,
const char *device,
{
QemuConsole *con;
DisplaySurface *surface;
+ int fd;
if (has_device) {
con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
@@ -387,7 +377,16 @@ void qmp_screendump(const char *filename, bool has_device,
const char *device,
return;
}
- ppm_save(filename, surface, errp);
+ fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
+ if (fd == -1) {
+ error_setg(errp, "failed to open file '%s': %s", filename,
+ strerror(errno));
+ return;
+ }
+
+ if (!ppm_save(fd, surface, errp)) {
+ unlink(filename);
+ }
}
void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
diff --git a/ui/trace-events b/ui/trace-events
index 63de72a798..0dcda393c1 100644
--- a/ui/trace-events
+++ b/ui/trace-events
@@ -15,7 +15,7 @@ displaysurface_create_pixman(void *display_surface)
"surface=%p"
displaysurface_free(void *display_surface) "surface=%p"
displaychangelistener_register(void *dcl, const char *name) "%p [ %s ]"
displaychangelistener_unregister(void *dcl, const char *name) "%p [ %s ]"
-ppm_save(const char *filename, void *display_surface) "%s surface=%p"
+ppm_save(int fd, void *display_surface) "fd=%d surface=%p"
# gtk.c
# gtk-gl-area.c
--
2.24.0.308.g228f53135a
- [PULL 0/7] Screendump patches, Marc-André Lureau, 2020/01/02
- [PULL 3/7] ui: add pixman image g_autoptr support, Marc-André Lureau, 2020/01/02
- [PULL 2/7] ppm-save: pass opened fd,
Marc-André Lureau <=
- [PULL 1/7] console: add graphic_hw_update_done(), Marc-André Lureau, 2020/01/02
- [PULL 4/7] object: add g_autoptr support, Marc-André Lureau, 2020/01/02
- [PULL 5/7] screendump: replace FILE with QIOChannel and fix close()/qemu_close(), Marc-André Lureau, 2020/01/02
- [PULL 6/7] osdep: add qemu_unlink(), Marc-André Lureau, 2020/01/02
- [PULL 7/7] screendump: use qemu_unlink(), Marc-André Lureau, 2020/01/02
- Re: [PULL 0/7] Screendump patches, Marc-André Lureau, 2020/01/02