From patchwork Wed Aug 30 16:56:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Cody X-Patchwork-Id: 807773 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 3xjC5b4pb7z9sN5 for ; Thu, 31 Aug 2017 03:23:31 +1000 (AEST) Received: from localhost ([::1]:51753 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dn6i9-0003Lk-CK for incoming@patchwork.ozlabs.org; Wed, 30 Aug 2017 13:23:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58354) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dn6Ij-0005TE-JQ for qemu-devel@nongnu.org; Wed, 30 Aug 2017 12:57:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dn6Ii-0006pF-QW for qemu-devel@nongnu.org; Wed, 30 Aug 2017 12:57:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55190) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dn6Ie-0006mK-AY; Wed, 30 Aug 2017 12:57:08 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0E22E883BA; Wed, 30 Aug 2017 16:57:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0E22E883BA Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jcody@redhat.com Received: from localhost (ovpn-116-70.phx2.redhat.com [10.3.116.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D11A75D722; Wed, 30 Aug 2017 16:57:06 +0000 (UTC) From: Jeff Cody To: qemu-devel@nongnu.org Date: Wed, 30 Aug 2017 12:56:58 -0400 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 30 Aug 2017 16:57:07 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 1/7] block/ssh: don't call libssh2_init() in block_init() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mitake.hitoshi@lab.ntt.co.jp, namei.unix@gmail.com, kwolf@redhat.com, rjones@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" We don't need libssh2 failure to be fatal (we could just opt to not register the driver on failure). But, it is probably a good idea to avoid external library calls during the block_init(), and call the libssh2 global init function on the first usage, returning any errors. Signed-off-by: Jeff Cody --- block/ssh.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/block/ssh.c b/block/ssh.c index e8f0404..cbb0e34 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -83,12 +83,28 @@ typedef struct BDRVSSHState { bool unsafe_flush_warning; } BDRVSSHState; -static void ssh_state_init(BDRVSSHState *s) +static bool ssh_libinit_called; + +static int ssh_state_init(BDRVSSHState *s, Error **errp) { + int ret; + + if (!ssh_libinit_called) { + ret = libssh2_init(0); + if (ret) { + error_setg(errp, "libssh2 initialization failed with %d", ret); + return ret; + } + ssh_libinit_called = true; + } + + memset(s, 0, sizeof *s); s->sock = -1; s->offset = -1; qemu_co_mutex_init(&s->lock); + + return 0; } static void ssh_state_free(BDRVSSHState *s) @@ -772,8 +788,13 @@ static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags, BDRVSSHState *s = bs->opaque; int ret; int ssh_flags; + Error *local_err = NULL; - ssh_state_init(s); + ret = ssh_state_init(s, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return ret; + } ssh_flags = LIBSSH2_FXF_READ; if (bdrv_flags & BDRV_O_RDWR) { @@ -821,8 +842,13 @@ static int ssh_create(const char *filename, QemuOpts *opts, Error **errp) BDRVSSHState s; ssize_t r2; char c[1] = { '\0' }; + Error *local_err = NULL; - ssh_state_init(&s); + ret = ssh_state_init(&s, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return ret; + } /* Get desired file size. */ total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), @@ -1213,14 +1239,6 @@ static BlockDriver bdrv_ssh = { static void bdrv_ssh_init(void) { - int r; - - r = libssh2_init(0); - if (r != 0) { - fprintf(stderr, "libssh2 initialization failed, %d\n", r); - exit(EXIT_FAILURE); - } - bdrv_register(&bdrv_ssh); }