From nobody Mon Feb 9 06:08:56 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1502199702225621.5724622997153; Tue, 8 Aug 2017 06:41:42 -0700 (PDT) Received: from localhost ([::1]:42646 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1df4lQ-0007hr-R6 for importer@patchew.org; Tue, 08 Aug 2017 09:41:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33197) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1df4jI-0005ns-5w for qemu-devel@nongnu.org; Tue, 08 Aug 2017 09:39:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1df4jD-0008Ar-D9 for qemu-devel@nongnu.org; Tue, 08 Aug 2017 09:39:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50924) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1df4j3-00083U-2g; Tue, 08 Aug 2017 09:39:13 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5A818C047B7B; Tue, 8 Aug 2017 13:39:10 +0000 (UTC) Received: from localhost (ovpn-116-54.phx2.redhat.com [10.3.116.54]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2CAB991288; Tue, 8 Aug 2017 13:39:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5A818C047B7B Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jcody@redhat.com From: Jeff Cody To: qemu-devel@nongnu.org Date: Tue, 8 Aug 2017 09:39:02 -0400 Message-Id: <7f210a8d031e371be1e3bc578b8f641ce141f224.1502198932.git.jcody@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 08 Aug 2017 13:39:10 +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 for-2.11 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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; =20 -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 =3D libssh2_init(0); + if (ret) { + error_setg(errp, "libssh2 initialization failed with %d", ret); + return ret; + } + ssh_libinit_called =3D true; + } + + memset(s, 0, sizeof *s); s->sock =3D -1; s->offset =3D -1; qemu_co_mutex_init(&s->lock); + + return 0; } =20 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 =3D bs->opaque; int ret; int ssh_flags; + Error *local_err =3D NULL; =20 - ssh_state_init(s); + ret =3D ssh_state_init(s, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return ret; + } =20 ssh_flags =3D 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] =3D { '\0' }; + Error *local_err =3D NULL; =20 - ssh_state_init(&s); + ret =3D ssh_state_init(&s, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return ret; + } =20 /* Get desired file size. */ total_size =3D ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), @@ -1213,14 +1239,6 @@ static BlockDriver bdrv_ssh =3D { =20 static void bdrv_ssh_init(void) { - int r; - - r =3D libssh2_init(0); - if (r !=3D 0) { - fprintf(stderr, "libssh2 initialization failed, %d\n", r); - exit(EXIT_FAILURE); - } - bdrv_register(&bdrv_ssh); } =20 --=20 2.9.4