Patchwork [v3] exec: make -mem-path filenames deterministic

login
register
mail settings
Submitter Peter Feiner
Date March 4, 2013, 6:54 p.m.
Message ID <1362423265-15855-1-git-send-email-peter@gridcentric.ca>
Download mbox | patch
Permalink /patch/224783/
State New
Headers show

Comments

Peter Feiner - March 4, 2013, 6:54 p.m.
From: Peter Feiner <peter@gridcentric.ca>

Adds ramblocks' names to their backing files when using -mem-path.  Eases
introspection and debugging.

Signed-off-by: Peter Feiner <peter@gridcentric.ca>

---

The commit should probably be called "exec: add ramblocks' names to -mem-path
files" since the paths aren't deterministic.

v1 -> v2: Just add ramblock name to mkstemp template.
v2 -> v3: Safely handle ramblocks with "/" in their names

 exec.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
Peter Feiner - March 11, 2013, 5:01 p.m.
Ping.

I'm very much hoping for this to make it into 1.5.

Thanks,
Peter

On Mon, Mar 4, 2013 at 1:54 PM,  <peter@gridcentric.ca> wrote:
> From: Peter Feiner <peter@gridcentric.ca>
>
> Adds ramblocks' names to their backing files when using -mem-path.  Eases
> introspection and debugging.
>
> Signed-off-by: Peter Feiner <peter@gridcentric.ca>
>
> ---
>
> The commit should probably be called "exec: add ramblocks' names to -mem-path
> files" since the paths aren't deterministic.
>
> v1 -> v2: Just add ramblock name to mkstemp template.
> v2 -> v3: Safely handle ramblocks with "/" in their names
>
>  exec.c |   13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/exec.c b/exec.c
> index 46a2830..f84e095 100644
> --- a/exec.c
> +++ b/exec.c
> @@ -844,6 +844,8 @@ static void *file_ram_alloc(RAMBlock *block,
>                              const char *path)
>  {
>      char *filename;
> +    char *sanitized_name;
> +    char *c;
>      void *area;
>      int fd;
>  #ifdef MAP_POPULATE
> @@ -865,7 +867,16 @@ static void *file_ram_alloc(RAMBlock *block,
>          return NULL;
>      }
>
> -    filename = g_strdup_printf("%s/qemu_back_mem.XXXXXX", path);
> +    /* Make name safe to use with mkstemp by replacing '/' with '_'. */
> +    sanitized_name = g_strdup(block->mr->name);
> +    for (c = sanitized_name; *c != '\0'; c++) {
> +        if (*c == '/')
> +            *c = '_';
> +    }
> +
> +    filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
> +                               sanitized_name);
> +    g_free(sanitized_name);
>
>      fd = mkstemp(filename);
>      if (fd < 0) {
> --
> 1.7.10.4
>
Anthony Liguori - March 12, 2013, 9:44 p.m.
Applied.  Thanks.

Regards,

Anthony Liguori

Patch

diff --git a/exec.c b/exec.c
index 46a2830..f84e095 100644
--- a/exec.c
+++ b/exec.c
@@ -844,6 +844,8 @@  static void *file_ram_alloc(RAMBlock *block,
                             const char *path)
 {
     char *filename;
+    char *sanitized_name;
+    char *c;
     void *area;
     int fd;
 #ifdef MAP_POPULATE
@@ -865,7 +867,16 @@  static void *file_ram_alloc(RAMBlock *block,
         return NULL;
     }
 
-    filename = g_strdup_printf("%s/qemu_back_mem.XXXXXX", path);
+    /* Make name safe to use with mkstemp by replacing '/' with '_'. */
+    sanitized_name = g_strdup(block->mr->name);
+    for (c = sanitized_name; *c != '\0'; c++) {
+        if (*c == '/')
+            *c = '_';
+    }
+
+    filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
+                               sanitized_name);
+    g_free(sanitized_name);
 
     fd = mkstemp(filename);
     if (fd < 0) {