[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v6 19/25] screendump: replace FILE with QIOChannel and fix close(
From: |
Marc-André Lureau |
Subject: |
[PATCH v6 19/25] screendump: replace FILE with QIOChannel and fix close()/qemu_close() |
Date: |
Fri, 8 Nov 2019 19:01:17 +0400 |
The file opened for ppm_save() may be a /dev/fdset, in which case a
dup fd is added to the fdset. It should be removed by calling
qemu_close(), instead of the implicit close() on fclose().
I don't see a convenient way to solve that with stdio streams, so I
switched the code to QIOChannel which uses qemu_close().
Signed-off-by: Marc-André Lureau <address@hidden>
---
ui/console.c | 38 +++++++++++++++++---------------------
1 file changed, 17 insertions(+), 21 deletions(-)
diff --git a/ui/console.c b/ui/console.c
index 77d62fe76d..587edf4ed4 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -33,6 +33,7 @@
#include "chardev/char-fe.h"
#include "trace.h"
#include "exec/memory.h"
+#include "io/channel-file.h"
#define DEFAULT_BACKSCROLL 512
#define CONSOLE_CURSOR_PERIOD 500
@@ -313,36 +314,31 @@ 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);
- FILE *f;
+ g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
+ g_autofree char *header = NULL;
+ g_autoptr(pixman_image_t) linebuf = NULL;
+ g_autoptr(GError) error = NULL;
int y;
- int ret;
- pixman_image_t *linebuf;
- bool success = false;
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 end;
+
+ header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
+ if (qio_channel_write_all(QIO_CHANNEL(ioc),
+ header, strlen(header), errp) < 0) {
+ return false;
}
+
linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
for (y = 0; y < height; y++) {
qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
- clearerr(f);
- ret = fwrite(pixman_image_get_data(linebuf), 1,
- pixman_image_get_stride(linebuf), f);
- (void)ret;
- success = !ferror(f);
+ if (qio_channel_write_all(QIO_CHANNEL(ioc),
+ (char *)pixman_image_get_data(linebuf),
+ pixman_image_get_stride(linebuf), errp) < 0)
{
+ return false;
+ }
}
-end:
- if (!success) {
- error_setg(errp, "failed to write to PPM file: %s", strerror(errno));
- }
- qemu_pixman_image_unref(linebuf);
- fclose(f);
- return success;
+ return true;
}
void qmp_screendump(const char *filename, bool has_device, const char *device,
--
2.24.0
- [PATCH v6 09/25] qmp: simplify qmp_return_error(), (continued)
- [PATCH v6 09/25] qmp: simplify qmp_return_error(), Marc-André Lureau, 2019/11/08
- [PATCH v6 10/25] QmpSession: keep a queue of pending commands, Marc-André Lureau, 2019/11/08
- [PATCH v6 11/25] QmpSession: return orderly, Marc-André Lureau, 2019/11/08
- [PATCH v6 12/25] qmp: introduce asynchronous command type, Marc-André Lureau, 2019/11/08
- [PATCH v6 14/25] qmp: add qmp_return_is_cancelled(), Marc-André Lureau, 2019/11/08
- [PATCH v6 13/25] scripts: learn 'async' qapi commands, Marc-André Lureau, 2019/11/08
- [PATCH v6 15/25] console: add graphic_hw_update_done(), Marc-André Lureau, 2019/11/08
- [PATCH v6 16/25] ppm-save: pass opened fd, Marc-André Lureau, 2019/11/08
- [PATCH v6 17/25] ui: add pixman image g_autoptr support, Marc-André Lureau, 2019/11/08
- [PATCH v6 18/25] object: add g_autoptr support, Marc-André Lureau, 2019/11/08
- [PATCH v6 19/25] screendump: replace FILE with QIOChannel and fix close()/qemu_close(),
Marc-André Lureau <=
- [PATCH v6 20/25] osdep: add qemu_unlink(), Marc-André Lureau, 2019/11/08
- [PATCH v6 21/25] screendump: use qemu_unlink(), Marc-André Lureau, 2019/11/08
- [PATCH v6 22/25] console: make screendump asynchronous, Marc-André Lureau, 2019/11/08
- [PATCH v6 23/25] monitor: start making qmp_human_monitor_command() asynchronous, Marc-André Lureau, 2019/11/08
- [PATCH v6 24/25] monitor: teach HMP about asynchronous commands, Marc-André Lureau, 2019/11/08
- [PATCH v6 25/25] hmp: call the asynchronous QMP screendump to fix outdated/glitches, Marc-André Lureau, 2019/11/08