Received: from eggs.gnu.org ([2001:4830:134:3::10]:55345) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1HxU-0006Av-A1 for qemu-devel@nongnu.org; Tue, 24 Nov 2015 13:04:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a1HxQ-0006nR-Vi for qemu-devel@nongnu.org; Tue, 24 Nov 2015 13:04:52 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56745) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1HxQ-0006nN-QW for qemu-devel@nongnu.org; Tue, 24 Nov 2015 13:04:48 -0500 From: marcandre.lureau@redhat.com Date: Tue, 24 Nov 2015 19:04:40 +0100 Message-Id: <1448388281-18691-2-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1448388281-18691-1-git-send-email-marcandre.lureau@redhat.com> References: <1448388281-18691-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v2 1/2] qga: flush explicitly when needed List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , mdroth@linux.vnet.ibm.com From: Marc-Andr=C3=A9 Lureau According to the specification: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html "the application shall ensure that output is not directly followed by input without an intervening call to fflush() or to a file positioning function (fseek(), fsetpos(), or rewind()), and input is not directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file." Without this change, a write() followed by a read() may lose the previously written content, as shown in the following test. Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=3D1210246 Signed-off-by: Marc-Andr=C3=A9 Lureau --- qga/commands-posix.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 0ebd473..d0228ce 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -219,6 +219,7 @@ void qmp_guest_set_time(bool has_time, int64_t time_n= s, Error **errp) typedef struct GuestFileHandle { uint64_t id; FILE *fh; + bool writing; QTAILQ_ENTRY(GuestFileHandle) next; } GuestFileHandle; =20 @@ -460,6 +461,17 @@ struct GuestFileRead *qmp_guest_file_read(int64_t ha= ndle, bool has_count, } =20 fh =3D gfh->fh; + + /* explicitly flush when switching from writing to reading */ + if (gfh->writing) { + int ret =3D fflush(fh); + if (ret =3D=3D EOF) { + error_setg_errno(errp, errno, "failed to flush file"); + return NULL; + } + gfh->writing =3D false; + } + buf =3D g_malloc0(count+1); read_count =3D fread(buf, 1, count, fh); if (ferror(fh)) { @@ -496,6 +508,16 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle,= const char *buf_b64, } =20 fh =3D gfh->fh; + + if (!gfh->writing) { + int ret =3D fseek(fh, 0, SEEK_CUR); + if (ret =3D=3D -1) { + error_setg_errno(errp, errno, "failed to seek file"); + return NULL; + } + gfh->writing =3D true; + } + buf =3D g_base64_decode(buf_b64, &buf_len); =20 if (!has_count) { --=20 2.5.0