qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: ronniesahlberg@gmail.com
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] ISCSI: Pick default initiator-name based on the name of the VM
Date: Mon, 06 Aug 2012 10:51:06 +0200	[thread overview]
Message-ID: <501F857A.9020608@redhat.com> (raw)
In-Reply-To: <501f7f62.85ed440a.6ea8.ffffc86d@mx.google.com>

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:

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;
 }
 

Can you confirm that this is how the initiator name should be passed, so I can
split the above patch and commit it?

Paolo

  reply	other threads:[~2012-08-06  8:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-06  8:24 [Qemu-devel] [PATCH] ISCSI: Pick default initiator-name based on the name of the VM ronniesahlberg
2012-08-06  8:51 ` Paolo Bonzini [this message]
2012-08-09  1:34   ` ronnie sahlberg
  -- strict thread matches above, loose matches on Subject: below --
2012-07-30  9:03 [Qemu-devel] [PATCH] Set iscsi initiator name based on vm name. version 2 Ronnie Sahlberg
2012-07-30  9:03 ` [Qemu-devel] [PATCH] ISCSI: Pick default initiator-name based on the name of the VM Ronnie Sahlberg
2012-07-30  9:21   ` Paolo Bonzini
2012-07-28  0:44 [Qemu-devel] [PATCH] iSCSI: generate default initiator-name from vm-name Ronnie Sahlberg
2012-07-28  0:44 ` [Qemu-devel] [PATCH] ISCSI: Pick default initiator-name based on the name of the VM Ronnie Sahlberg
2012-07-30  7:41   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=501F857A.9020608@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=ronniesahlberg@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).