From patchwork Wed Oct 15 11:03:23 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Mammedov X-Patchwork-Id: 399948 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 7A62714007B for ; Wed, 15 Oct 2014 22:05:44 +1100 (EST) Received: from localhost ([::1]:43943 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XeMOk-0005bu-Lf for incoming@patchwork.ozlabs.org; Wed, 15 Oct 2014 07:05:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41526) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XeMMq-0002ue-BE for qemu-devel@nongnu.org; Wed, 15 Oct 2014 07:03:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XeMMh-00020d-7e for qemu-devel@nongnu.org; Wed, 15 Oct 2014 07:03:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48211) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XeMMh-00020Q-11 for qemu-devel@nongnu.org; Wed, 15 Oct 2014 07:03:35 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s9FB3V3l031549 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 15 Oct 2014 07:03:32 -0400 Received: from Igors-MacBook-Pro.local.com (vpn-233-6.phx2.redhat.com [10.3.233.6]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s9FB3REA006360; Wed, 15 Oct 2014 07:03:31 -0400 From: Igor Mammedov To: qemu-devel@nongnu.org Date: Wed, 15 Oct 2014 13:03:23 +0200 Message-Id: <1413371003-43597-3-git-send-email-imammedo@redhat.com> In-Reply-To: <1413371003-43597-1-git-send-email-imammedo@redhat.com> References: <1413371003-43597-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: peter.maydell@linaro.org, aliguori@amazon.com Subject: [Qemu-devel] [PATCH 2/2] vl.c: reduce exit on error code duplication 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 use exit_if_error() helper instead of a bunch of if (local_err) { error_report(foo); error_free(local_err); exit(1); } code blocks Signed-off-by: Igor Mammedov Reviewed-by: Eric Blake --- vl.c | 58 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/vl.c b/vl.c index f563da1..151ee2d 100644 --- a/vl.c +++ b/vl.c @@ -582,6 +582,30 @@ static void res_free(void) } } +static void GCC_FMT_ATTR(2, 3) exit_if_error(Error *err, const char *fmt, ...) +{ + va_list ap; + + if (!err) { + return; + } + + if (fmt) { + char *optional_msg = NULL; + + va_start(ap, fmt); + optional_msg = g_strdup_vprintf(fmt, ap); + va_end(ap); + error_report("%s: %s", optional_msg, error_get_pretty(err)); + g_free(optional_msg); + } else { + error_report("%s", error_get_pretty(err)); + } + + error_free(err); + exit(EXIT_FAILURE); +} + static int default_driver_check(QemuOpts *opts, void *opaque) { const char *driver = qemu_opt_get(opts, "driver"); @@ -2380,11 +2404,7 @@ static int chardev_init_func(QemuOpts *opts, void *opaque) Error *local_err = NULL; qemu_chr_new_from_opts(opts, NULL, &local_err); - if (local_err) { - error_report("%s", error_get_pretty(local_err)); - error_free(local_err); - return -1; - } + exit_if_error(local_err, NULL); return 0; } @@ -2790,12 +2810,7 @@ static int machine_set_property(const char *name, const char *value, string_input_visitor_cleanup(siv); g_free(qom_name); - if (local_err) { - qerror_report_err(local_err); - error_free(local_err); - return -1; - } - + exit_if_error(local_err, NULL); return 0; } @@ -4190,11 +4205,7 @@ int main(int argc, char **argv, char **envp) if (qtest_chrdev) { qtest_init(qtest_chrdev, qtest_log, &local_err); - if (local_err) { - error_report("%s", error_get_pretty(local_err)); - error_free(local_err); - exit(1); - } + exit_if_error(local_err, NULL); } machine_opts = qemu_get_machine_opts(); @@ -4434,12 +4445,8 @@ int main(int argc, char **argv, char **envp) if (vnc_display) { vnc_display_init(ds); vnc_display_open(ds, vnc_display, &local_err); - if (local_err != NULL) { - error_report("Failed to start VNC server on `%s': %s", - vnc_display, error_get_pretty(local_err)); - error_free(local_err); - exit(1); - } + exit_if_error(local_err, "Failed to start VNC server on '%s'", + vnc_display); if (show_vnc_port) { printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); @@ -4487,12 +4494,7 @@ int main(int argc, char **argv, char **envp) if (incoming) { qemu_start_incoming_migration(incoming, &local_err); - if (local_err) { - error_report("-incoming %s: %s", incoming, - error_get_pretty(local_err)); - error_free(local_err); - exit(1); - } + exit_if_error(local_err, "-incoming %s", incoming); } else if (autostart) { vm_start(); }