From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Kevin Wolf" <kwolf@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
qemu-block@nongnu.org, "Michael S. Tsirkin" <mst@redhat.com>,
"Michael Roth" <mdroth@linux.vnet.ibm.com>,
"Fabien Chouteau" <chouteau@adacore.com>,
"Max Filippov" <jcmvbkbc@gmail.com>,
"KONRAD Frederic" <frederic.konrad@adacore.com>,
qemu-arm@nongnu.org, qemu-ppc@nongnu.org,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Stafford Horne" <shorne@gmail.com>,
"Max Reitz" <mreitz@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"Aurelien Jarno" <aurelien@aurel32.net>
Subject: [PATCH-for-5.0 03/12] qga: Extract qmp_guest_file_read() to common commands.c
Date: Tue, 14 Apr 2020 15:30:43 +0200 [thread overview]
Message-ID: <20200414133052.13712-4-philmd@redhat.com> (raw)
In-Reply-To: <20200414133052.13712-1-philmd@redhat.com>
Extract the common code shared by both POSIX/Win32 implementations.
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
qga/commands-common.h | 3 +++
qga/commands-posix.c | 22 +++-------------------
qga/commands-win32.c | 20 +++-----------------
qga/commands.c | 26 ++++++++++++++++++++++++++
4 files changed, 35 insertions(+), 36 deletions(-)
diff --git a/qga/commands-common.h b/qga/commands-common.h
index af90e5481e..90785ed4bb 100644
--- a/qga/commands-common.h
+++ b/qga/commands-common.h
@@ -15,4 +15,7 @@ typedef struct GuestFileHandle GuestFileHandle;
GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp);
+GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
+ int64_t count, Error **errp);
+
#endif
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index c59c32185c..a52af0315f 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -461,29 +461,14 @@ void qmp_guest_file_close(int64_t handle, Error **errp)
g_free(gfh);
}
-struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
- int64_t count, Error **errp)
+GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
+ int64_t count, Error **errp)
{
- GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
GuestFileRead *read_data = NULL;
guchar *buf;
- FILE *fh;
+ FILE *fh = gfh->fh;
size_t read_count;
- if (!gfh) {
- return NULL;
- }
-
- if (!has_count) {
- count = QGA_READ_COUNT_DEFAULT;
- } else if (count < 0 || count >= UINT32_MAX) {
- error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
- count);
- return NULL;
- }
-
- fh = gfh->fh;
-
/* explicitly flush when switching from writing to reading */
if (gfh->state == RW_STATE_WRITING) {
int ret = fflush(fh);
@@ -498,7 +483,6 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
read_count = fread(buf, 1, count, fh);
if (ferror(fh)) {
error_setg_errno(errp, errno, "failed to read file");
- slog("guest-file-read failed, handle: %" PRId64, handle);
} else {
buf[read_count] = 0;
read_data = g_new0(GuestFileRead, 1);
diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index cfaf6b84b8..9717a8d52d 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -322,33 +322,19 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
}
}
-GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
- int64_t count, Error **errp)
+GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
+ int64_t count, Error **errp)
{
GuestFileRead *read_data = NULL;
guchar *buf;
- HANDLE fh;
+ HANDLE fh = gfh->fh;
bool is_ok;
DWORD read_count;
- GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
- if (!gfh) {
- return NULL;
- }
- if (!has_count) {
- count = QGA_READ_COUNT_DEFAULT;
- } else if (count < 0 || count >= UINT32_MAX) {
- error_setg(errp, "value '%" PRId64
- "' is invalid for argument count", count);
- return NULL;
- }
-
- fh = gfh->fh;
buf = g_malloc0(count + 1);
is_ok = ReadFile(fh, buf, count, &read_count, NULL);
if (!is_ok) {
error_setg_win32(errp, GetLastError(), "failed to read file");
- slog("guest-file-read failed, handle %" PRId64, handle);
} else {
buf[read_count] = 0;
read_data = g_new0(GuestFileRead, 1);
diff --git a/qga/commands.c b/qga/commands.c
index 4471a9f08d..5611117372 100644
--- a/qga/commands.c
+++ b/qga/commands.c
@@ -18,6 +18,7 @@
#include "qemu/base64.h"
#include "qemu/cutils.h"
#include "qemu/atomic.h"
+#include "commands-common.h"
/* Maximum captured guest-exec out_data/err_data - 16MB */
#define GUEST_EXEC_MAX_OUTPUT (16*1024*1024)
@@ -547,3 +548,28 @@ error:
g_free(info);
return NULL;
}
+
+GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
+ int64_t count, Error **errp)
+{
+ GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
+ GuestFileRead *read_data;
+
+ if (!gfh) {
+ return NULL;
+ }
+ if (!has_count) {
+ count = QGA_READ_COUNT_DEFAULT;
+ } else if (count < 0 || count >= UINT32_MAX) {
+ error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
+ count);
+ return NULL;
+ }
+
+ read_data = guest_file_read_unsafe(gfh, count, errp);
+ if (!read_data) {
+ slog("guest-file-write failed, handle: %" PRId64, handle);
+ }
+
+ return read_data;
+}
--
2.21.1
next prev parent reply other threads:[~2020-04-14 17:25 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-14 13:30 [PATCH-for-5.0 00/12] various bugfixes Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 01/12] Revert "prevent crash when executing guest-file-read with large count" Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 02/12] qga: Extract guest_file_handle_find() to commands-common.h Philippe Mathieu-Daudé
2020-04-14 13:30 ` Philippe Mathieu-Daudé [this message]
2020-04-14 13:30 ` [PATCH-for-5.0 04/12] qga: Restrict guest-file-read count to 48 MB to avoid crashes Philippe Mathieu-Daudé
2020-04-15 12:34 ` Daniel P. Berrangé
2020-04-15 13:02 ` Philippe Mathieu-Daudé
2020-04-15 15:23 ` Michael Roth
2020-04-14 13:30 ` [PATCH-for-5.0 05/12] vhost-user-gpu: Release memory returned by vu_queue_pop() with free() Philippe Mathieu-Daudé
2020-04-17 6:39 ` Michael S. Tsirkin
2020-04-14 13:30 ` [PATCH-for-5.0 06/12] hw/openrisc/pic_cpu: Use qdev gpio rather than qemu_allocate_irqs() Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 07/12] hw/misc/grlib_ahb_apb_pnp: Avoid crash when writing to AHB PnP registers Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 08/12] hw/misc/grlib_ahb_apb_pnp: Fix AHB PnP 8-bit accesses Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 09/12] hw/display/sm501: Avoid heap overflow in sm501_2d_operation() Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 10/12] hw/block/pflash: Check return value of blk_pwrite() Philippe Mathieu-Daudé
2020-04-14 18:34 ` Mansour Ahmadi
2020-04-15 8:08 ` Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 11/12] gdbstub: Do not use memset() on GByteArray Philippe Mathieu-Daudé
2020-04-14 13:30 ` [PATCH-for-5.0 12/12] gdbstub: Introduce gdb_get_freg32() to get float32 registers Philippe Mathieu-Daudé
2020-04-17 6:40 ` [PATCH-for-5.0 00/12] various bugfixes Michael S. Tsirkin
2020-04-17 8:30 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200414133052.13712-4-philmd@redhat.com \
--to=philmd@redhat.com \
--cc=aurelien@aurel32.net \
--cc=chouteau@adacore.com \
--cc=frederic.konrad@adacore.com \
--cc=jcmvbkbc@gmail.com \
--cc=kraxel@redhat.com \
--cc=kwolf@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=mreitz@redhat.com \
--cc=mst@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=shorne@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).