From patchwork Tue Nov 24 16:34:17 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 548151 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id BB4AB1402A8 for ; Wed, 25 Nov 2015 03:34:53 +1100 (AEDT) Received: from localhost ([::1]:39586 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1GYN-0006Bc-9m for incoming@patchwork.ozlabs.org; Tue, 24 Nov 2015 11:34:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51784) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1GY8-0005sy-KW for qemu-devel@nongnu.org; Tue, 24 Nov 2015 11:34:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a1GY4-000565-JE for qemu-devel@nongnu.org; Tue, 24 Nov 2015 11:34:36 -0500 Received: from mx1.redhat.com ([209.132.183.28]:57407) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1GY4-00055b-D1 for qemu-devel@nongnu.org; Tue, 24 Nov 2015 11:34:32 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 6DB4CC0A517B; Tue, 24 Nov 2015 16:34:31 +0000 (UTC) Received: from localhost (ovpn-113-65.phx2.redhat.com [10.3.113.65]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tAOGYUIQ025413; Tue, 24 Nov 2015 11:34:30 -0500 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Tue, 24 Nov 2015 17:34:17 +0100 Message-Id: <1448382858-28616-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Michael Roth Subject: [Qemu-devel] [PATCH 1/2] qga: flush implicitely when needed X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Marc-André 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=1210246 Signed-off-by: Marc-André Lureau Reviewed-by: Eric Blake --- qga/commands-posix.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 0ebd473..3c86a4e 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_ns, Error **errp) typedef struct GuestFileHandle { uint64_t id; FILE *fh; + bool writing; QTAILQ_ENTRY(GuestFileHandle) next; } GuestFileHandle; @@ -460,6 +461,17 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, } fh = gfh->fh; + + /* implicitely flush when switching from writing to reading */ + if (gfh->writing) { + int ret = fflush(fh); + if (ret == EOF) { + error_setg_errno(errp, errno, "failed to flush file"); + return NULL; + } + gfh->writing = false; + } + buf = g_malloc0(count+1); read_count = fread(buf, 1, count, fh); if (ferror(fh)) { @@ -496,6 +508,16 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64, } fh = gfh->fh; + + if (!gfh->writing) { + int ret = fseek(fh, 0, SEEK_CUR); + if (ret == -1) { + error_setg_errno(errp, errno, "failed to seek file"); + return NULL; + } + gfh->writing = true; + } + buf = g_base64_decode(buf_b64, &buf_len); if (!has_count) {