From patchwork Mon Aug 6 08:51:06 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 175302 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 474552C009D for ; Mon, 6 Aug 2012 18:51:32 +1000 (EST) Received: from localhost ([::1]:37360 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyJ28-0002aT-PK for incoming@patchwork.ozlabs.org; Mon, 06 Aug 2012 04:51:28 -0400 Received: from eggs.gnu.org ([208.118.235.92]:42944) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyJ1x-0002aJ-NR for qemu-devel@nongnu.org; Mon, 06 Aug 2012 04:51:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SyJ1r-0007m5-LU for qemu-devel@nongnu.org; Mon, 06 Aug 2012 04:51:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:22310) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyJ1r-0007m1-Cc for qemu-devel@nongnu.org; Mon, 06 Aug 2012 04:51:11 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q768p9N7029975 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 6 Aug 2012 04:51:09 -0400 Received: from yakj.usersys.redhat.com (ovpn-112-20.ams2.redhat.com [10.36.112.20]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q768p7Iw002377; Mon, 6 Aug 2012 04:51:08 -0400 Message-ID: <501F857A.9020608@redhat.com> Date: Mon, 06 Aug 2012 10:51:06 +0200 From: Paolo Bonzini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: ronniesahlberg@gmail.com References: <501f7f62.85ed440a.6ea8.ffffc86d@mx.google.com> In-Reply-To: <501f7f62.85ed440a.6ea8.ffffc86d@mx.google.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: qemu-devel@nongnu.org Subject: Re: [Qemu-devel] [PATCH] ISCSI: Pick default initiator-name based on the name of the VM 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 Il 06/08/2012 10:24, ronniesahlberg@gmail.com ha scritto: > diff --git a/block/iscsi.c b/block/iscsi.c > index 993a86d..243496b 100644 > --- a/block/iscsi.c > +++ b/block/iscsi.c > @@ -896,23 +896,31 @@ static char *parse_initiator_name(const char *target) > QemuOptsList *list; > QemuOpts *opts; > const char *name = NULL; > + char *default_name; > + const char *iscsi_name = qemu_get_vm_name(); > + > + if (asprintf(&default_name, "iqn.2008-11.org.linux-kvm%s%s", asprintf is not portable, g_strdup_printf is. > + iscsi_name ? ":" : "", > + iscsi_name ? iscsi_name : "") == -1) { > + default_name = (char *)"iqn.2008-11.org.linux-kvm"; > + } > > list = qemu_find_opts("iscsi"); > if (!list) { > - return g_strdup("iqn.2008-11.org.linux-kvm"); > + return g_strdup(default_name); > } > > opts = qemu_opts_find(list, target); > if (opts == NULL) { > opts = QTAILQ_FIRST(&list->head); > if (!opts) { > - return g_strdup("iqn.2008-11.org.linux-kvm"); > + return g_strdup(default_name); > } > } > > name = qemu_opt_get(opts, "initiator-name"); > if (!name) { > - return g_strdup("iqn.2008-11.org.linux-kvm"); > + return g_strdup(default_name); > } > Each of these strdup calls is leaking the original default_name. > return g_strdup(name); iscsi_open is never going to free this, but neither is libiscsi. Putting all this together, we need on top of your patch something like this: Can you confirm that this is how the initiator name should be passed, so I can split the above patch and commit it? Paolo diff --git a/block/iscsi.c b/block/iscsi.c index 243496b..69044b0 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -899,31 +899,27 @@ static char *parse_initiator_name(const char *target) char *default_name; const char *iscsi_name = qemu_get_vm_name(); - if (asprintf(&default_name, "iqn.2008-11.org.linux-kvm%s%s", - iscsi_name ? ":" : "", - iscsi_name ? iscsi_name : "") == -1) { - default_name = (char *)"iqn.2008-11.org.linux-kvm"; - } + default_name = g_strdup_printf(&default_name, "iqn.2008-11.org.linux-kvm%s%s", + iscsi_name ? ":" : "", + iscsi_name ? iscsi_name : ""); list = qemu_find_opts("iscsi"); - if (!list) { - return g_strdup(default_name); - } - - opts = qemu_opts_find(list, target); - if (opts == NULL) { - opts = QTAILQ_FIRST(&list->head); + if (list) { + opts = qemu_opts_find(list, target); if (!opts) { - return g_strdup(default_name); + opts = QTAILQ_FIRST(&list->head); + } + if (opts) { + name = qemu_opt_get(opts, "initiator-name"); } } - name = qemu_opt_get(opts, "initiator-name"); - if (!name) { - return g_strdup(default_name); + if (name) { + g_free(default_name); + return g_strdup(name); + } else { + return default_name; } - - return g_strdup(name); } /* @@ -951,7 +947,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) error_report("Failed to parse URL : %s %s", filename, iscsi_get_error(iscsi)); ret = -EINVAL; - goto failed; + goto out; } memset(iscsilun, 0, sizeof(IscsiLun)); @@ -962,13 +958,13 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) if (iscsi == NULL) { error_report("iSCSI: Failed to create iSCSI context."); ret = -ENOMEM; - goto failed; + goto out; } if (iscsi_set_targetname(iscsi, iscsi_url->target)) { error_report("iSCSI: Failed to set target name."); ret = -EINVAL; - goto failed; + goto out; } if (iscsi_url->user != NULL) { @@ -977,7 +973,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) if (ret != 0) { error_report("Failed to set initiator username and password"); ret = -EINVAL; - goto failed; + goto out; } } @@ -985,13 +981,13 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) if (parse_chap(iscsi, iscsi_url->target) != 0) { error_report("iSCSI: Failed to set CHAP user/password"); ret = -EINVAL; - goto failed; + goto out; } if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { error_report("iSCSI: Failed to set session type to normal."); ret = -EINVAL; - goto failed; + goto out; } iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); @@ -1012,7 +1008,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) != 0) { error_report("iSCSI: Failed to start async connect."); ret = -EINVAL; - goto failed; + goto out; } while (!task.complete) { @@ -1023,11 +1019,7 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) error_report("iSCSI: Failed to connect to LUN : %s", iscsi_get_error(iscsi)); ret = -EINVAL; - goto failed; - } - - if (iscsi_url != NULL) { - iscsi_destroy_url(iscsi_url); + goto out; } /* Medium changer or tape. We dont have any emulation for this so this must @@ -1039,19 +1031,22 @@ static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) bs->sg = 1; } - return 0; + ret = 0; -failed: +out: if (initiator_name != NULL) { g_free(initiator_name); } if (iscsi_url != NULL) { iscsi_destroy_url(iscsi_url); } - if (iscsi != NULL) { - iscsi_destroy_context(iscsi); + + if (ret) { + if (iscsi != NULL) { + iscsi_destroy_context(iscsi); + } + memset(iscsilun, 0, sizeof(IscsiLun)); } - memset(iscsilun, 0, sizeof(IscsiLun)); return ret; }