From patchwork Thu Nov 10 12:41:43 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduardo Habkost X-Patchwork-Id: 124883 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1F5CFB6F18 for ; Fri, 11 Nov 2011 00:48:28 +1100 (EST) Received: from localhost ([::1]:37322 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ROTzG-0000c5-Jo for incoming@patchwork.ozlabs.org; Thu, 10 Nov 2011 07:44:10 -0500 Received: from eggs.gnu.org ([140.186.70.92]:58365) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ROTyX-0006av-8F for qemu-devel@nongnu.org; Thu, 10 Nov 2011 07:43:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ROTyU-0006rf-AZ for qemu-devel@nongnu.org; Thu, 10 Nov 2011 07:43:25 -0500 Received: from mx1.redhat.com ([209.132.183.28]:13974) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ROTyT-0006r2-Ua for qemu-devel@nongnu.org; Thu, 10 Nov 2011 07:43:22 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id pAAChJbw010604 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 10 Nov 2011 07:43:20 -0500 Received: from blackpad.lan.raisama.net (ovpn-113-78.phx2.redhat.com [10.3.113.78]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id pAAChJe0011253; Thu, 10 Nov 2011 07:43:19 -0500 Received: by blackpad.lan.raisama.net (Postfix, from userid 500) id 276F1202EB1; Thu, 10 Nov 2011 10:41:49 -0200 (BRST) From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Thu, 10 Nov 2011 10:41:43 -0200 Message-Id: <1320928908-19076-6-git-send-email-ehabkost@redhat.com> In-Reply-To: <1320928908-19076-1-git-send-email-ehabkost@redhat.com> References: <1320928908-19076-1-git-send-email-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 209.132.183.28 Cc: Michael Roth , Juan Quintela Subject: [Qemu-devel] [PATCH 05/10] qemu_fclose: return last_error if set (v3) 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 This will make sure no error will be missed as long as callers always check for qemu_fclose() return value. For reference, this is the complete list of qemu_fclose() callers: - exec_close(): already fixed to check for negative values, not -1 - migrate_fd_cleanup(): already fixed to consider only negative values as error, not any non-zero value - exec_accept_incoming_migration(): no return value check (yet) - fd_accept_incoming_migration(): no return value check (yet) - tcp_accept_incoming_migration(): no return value check (yet) - unix_accept_incoming_migration(): no return value check (yet) - do_savevm(): no return value check (yet) - load_vmstate(): no return value check (yet) Changes v1 -> v2: - Add small comment about the need to return previously-spotted errors Changes v2 -> v3: - Add braces to "if" statements to match coding style Signed-off-by: Eduardo Habkost --- savevm.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 48 insertions(+), 3 deletions(-) diff --git a/savevm.c b/savevm.c index 2dab5dc..2fef693 100644 --- a/savevm.c +++ b/savevm.c @@ -436,6 +436,22 @@ void qemu_file_set_error(QEMUFile *f, int ret) f->last_error = ret; } +/** Sets last_error conditionally + * + * Sets last_error only if ret is negative _and_ no error + * was set before. + */ +static void qemu_file_set_if_error(QEMUFile *f, int ret) +{ + if (ret < 0 && !f->last_error) { + qemu_file_set_error(f, ret); + } +} + +/** Flushes QEMUFile buffer + * + * In case of error, last_error is set. + */ void qemu_fflush(QEMUFile *f) { if (!f->put_buffer) @@ -482,12 +498,41 @@ static void qemu_fill_buffer(QEMUFile *f) qemu_file_set_error(f, len); } -int qemu_fclose(QEMUFile *f) +/** Calls close function and set last_error if needed + * + * Internal function. qemu_fflush() must be called before this. + * + * Returns f->close() return value, or 0 if close function is not set. + */ +static int qemu_close(QEMUFile *f) { int ret = 0; - qemu_fflush(f); - if (f->close) + if (f->close) { ret = f->close(f->opaque); + qemu_file_set_if_error(f, ret); + } + return ret; +} + +/** Closes the file + * + * Returns negative error value if any error happened on previous operations or + * while closing the file. Returns 0 or positive number on success. + * + * The meaning of return value on success depends on the specific backend + * being used. + */ +int qemu_fclose(QEMUFile *f) +{ + int ret; + qemu_fflush(f); + ret = qemu_close(f); + /* If any error was spotted before closing, we should report it + * instead of the close() return value. + */ + if (f->last_error) { + ret = f->last_error; + } g_free(f); return ret; }