All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Schwinge <thomas@codesourcery.com>
To: qemu-devel@nongnu.org
Cc: paul@codesourcery.com, Johannes Schauer <j.schauer@email.de>
Subject: Re: [Qemu-devel] Environment variables for user-mode QEMU
Date: Wed, 24 Apr 2013 18:11:46 +0200	[thread overview]
Message-ID: <87k3nrq56l.fsf@schwinge.name> (raw)
In-Reply-To: <87txmwoyqc.fsf@schwinge.name>


[-- Attachment #1.1: Type: text/plain, Size: 2735 bytes --]

Hi!

On Wed, 24 Apr 2013 15:16:27 +0200, I wrote:
> We have a need to pass environment variable assignments containing commas
> to user-mode QEMU.  The usage information currently says:
> 
>     You can use -E and -U options or the QEMU_SET_ENV and
>     QEMU_UNSET_ENV environment variables to set and unset
>     environment variables for the target process.
>     It is possible to provide several variables by separating them
>     by commas in getsubopt(3) style. Additionally it is possible to
>     provide the -E and -U options multiple times.
> 
>     $ env -i x86_64-linux-user/qemu-x86_64 -E x=y,y=z /usr/bin/printenv
>     y=z
>     x=y
> 
> Instead of this, we'd like to see:
> 
>     $ env -i x86_64-linux-user/qemu-x86_64 -E x=y,y=z /usr/bin/printenv
>     x=y,y=z
> 
> Due to the tokenization based on comma in
> linux-user/main.c:handle_arg_set_env, there is currently no way to
> achieve this -- other than pre-setting environment variables before
> running user-mode QEMU, which obviously isn't always desirable/possible
> (LD_LIBRARY_PATH, etc.).
> 
> Assuming there is a consensus, how would you like this implemented?
> 
> Is it OK to change the semantics of -E (as well as -U, for symmetry?) to
> not handle multiple environment variable assignments (preliminary patch
> below), or does that functionality need to be preserved?  Something needs
> to be done about QEMU_SET_ENV and QEMU_UNSET_ENV then (if anyone is using
> these at all, which we can't disprove), as these are not very useful if
> they can handle only one environment variable.

That is actually a "regression"/change introduced in commit
fc9c54124d134dbd76338a92a91804dab2df8166 (Johannes Schauer CCed, for your
information).  I'm attaching
0001-linux-user-Document-E-and-U-options.patch to update the
documentation for the linux-user QEMU.

The bsd-user QEMU has not been changed accordingly, by the way.


I'm attaching
0002-linux-user-Use-existing-envlist_parse_set-envlist_pa.patch for some
code simplifications.


> Or, should we perhaps have a new -env option that causes any later
> non-option arguments, that contain an equal sign, to be handled as
> environment variable assignments, just like the env command does?
> 
>     $ env -i x86_64-linux-user/qemu-x86_64 [some options] -env [more options] a=b,c d=e,f=a /usr/bin/printenv
>     a=b,c
>     d=e,f=a
> 
> I think I might prefer that solution.  As it is a new option, it also
> won't interfere with anyone's usage/expectations of the current behavior.

I'm attaching 0003-linux-user-New-env-option.patch for implementing that.


The patches have only been lightly tested; please review.


Grüße,
 Thomas



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-linux-user-Document-E-and-U-options.patch --]
[-- Type: text/x-diff, Size: 1534 bytes --]

From eaf53af8d75e001969a658d83d71dd48324c6bcb Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Wed, 24 Apr 2013 17:41:58 +0200
Subject: [PATCH 1/3] linux-user: Document -E and -U options.

Document changed behavior introducecd in commit
fc9c54124d134dbd76338a92a91804dab2df8166.

Signed-off-by: Thomas Schwinge <thomas@codesourcery.com>
---
 qemu-doc.texi |   12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git qemu-doc.texi qemu-doc.texi
index dfea4d3..3034af4 100644
--- qemu-doc.texi
+++ qemu-doc.texi
@@ -2683,13 +2683,11 @@ Set the x86 elf interpreter prefix (default=/usr/local/qemu-i386)
 Set the x86 stack size in bytes (default=524288)
 @item -cpu model
 Select CPU model (-cpu help for list and additional feature selection)
-@item -ignore-environment
-Start with an empty environment. Without this option,
-the initial environment is a copy of the caller's environment.
-@item -E @var{var}=@var{value}
-Set environment @var{var} to @var{value}.
-@item -U @var{var}
-Remove @var{var} from the environment.
+@item -E @var{var}=@var{value}[,@var{var2}=@var{value2},...]
+Set environment @var{var} to @var{value}, @var{var2} to @var{value2},
+and so on.
+@item -U @var{var}[,var2,...]
+Remove @var{var}, @var{var2}, and so on from the environment.
 @item -B offset
 Offset guest address by the specified number of bytes.  This is useful when
 the address region required by guest applications is reserved on the host.
-- 
1.7.10.4


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.3: 0002-linux-user-Use-existing-envlist_parse_set-envlist_pa.patch --]
[-- Type: text/x-diff, Size: 1978 bytes --]

From 540f9fa2e9eb197bde3cb91403b6eaaa7d78ac4d Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Wed, 24 Apr 2013 17:28:02 +0200
Subject: [PATCH 2/3] linux-user: Use existing
 envlist_parse_set/envlist_parse_unset interface.

Signed-off-by: Thomas Schwinge <thomas@codesourcery.com>
---
 linux-user/main.c |   18 ++++--------------
 util/envlist.c    |    4 ++--
 2 files changed, 6 insertions(+), 16 deletions(-)

diff --git linux-user/main.c linux-user/main.c
index 4e92a0b..7f81821 100644
--- linux-user/main.c
+++ linux-user/main.c
@@ -3204,26 +3204,16 @@ static void handle_arg_log_filename(const char *arg)
 
 static void handle_arg_set_env(const char *arg)
 {
-    char *r, *p, *token;
-    r = p = strdup(arg);
-    while ((token = strsep(&p, ",")) != NULL) {
-        if (envlist_setenv(envlist, token) != 0) {
-            usage();
-        }
+    if (envlist_parse_set(envlist, arg) != 0) {
+        usage();
     }
-    free(r);
 }
 
 static void handle_arg_unset_env(const char *arg)
 {
-    char *r, *p, *token;
-    r = p = strdup(arg);
-    while ((token = strsep(&p, ",")) != NULL) {
-        if (envlist_unsetenv(envlist, token) != 0) {
-            usage();
-        }
+    if (envlist_parse_unset(envlist, arg) != 0) {
+        usage();
     }
-    free(r);
 }
 
 static void handle_arg_argv0(const char *arg)
diff --git util/envlist.c util/envlist.c
index ebc06cf..4dbca28 100644
--- util/envlist.c
+++ util/envlist.c
@@ -55,10 +55,10 @@ envlist_free(envlist_t *envlist)
 
 /*
  * Parses comma separated list of set/modify environment
- * variable entries and updates given enlist accordingly.
+ * variable entries and updates given envlist accordingly.
  *
  * For example:
- *     envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
+ *     envlist_parse_set(el, "HOME=foo,SHELL=/bin/sh");
  *
  * inserts/sets environment variables HOME and SHELL.
  *
-- 
1.7.10.4


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.4: 0003-linux-user-New-env-option.patch --]
[-- Type: text/x-diff, Size: 4442 bytes --]

From 71461a2caccfc31287e26ca1edf8ff0053a563f7 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Wed, 24 Apr 2013 17:59:17 +0200
Subject: [PATCH 3/3] linux-user: New -env option.

Signed-off-by: Thomas Schwinge <thomas@codesourcery.com>
---
 linux-user/main.c |   29 ++++++++++++++++++++++++++++-
 qemu-doc.texi     |    6 +++++-
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git linux-user/main.c linux-user/main.c
index 7f81821..7b38b54 100644
--- linux-user/main.c
+++ linux-user/main.c
@@ -38,6 +38,7 @@
 char *exec_path;
 
 int singlestep;
+int env_mode;
 const char *filename;
 const char *argv0;
 int gdbstub_port;
@@ -3202,6 +3203,11 @@ static void handle_arg_log_filename(const char *arg)
     qemu_set_log_filename(arg);
 }
 
+static void handle_arg_env(const char *arg)
+{
+    env_mode = 1;
+}
+
 static void handle_arg_set_env(const char *arg)
 {
     if (envlist_parse_set(envlist, arg) != 0) {
@@ -3354,6 +3360,8 @@ static const struct qemu_argument arg_table[] = {
      "size",       "set the stack size to 'size' bytes"},
     {"cpu",        "QEMU_CPU",         true,  handle_arg_cpu,
      "model",      "select CPU (-cpu help for list)"},
+    {"env",        "",                 false, handle_arg_env,
+     "",           "enable parsing of the ENV LIST (see below)"},
     {"E",          "QEMU_SET_ENV",     true,  handle_arg_set_env,
      "var=value",  "sets targets environment variable (see below)"},
     {"U",          "QEMU_UNSET_ENV",   true,  handle_arg_unset_env,
@@ -3390,7 +3398,7 @@ static void usage(void)
     int maxarglen;
     int maxenvlen;
 
-    printf("usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
+    printf("usage: qemu-" TARGET_ARCH " [options] [env list] program [arguments...]\n"
            "Linux CPU emulator (compiled for " TARGET_ARCH " emulation)\n"
            "\n"
            "Options and associated environment variables:\n"
@@ -3444,9 +3452,11 @@ static void usage(void)
            "It is possible to provide several variables by separating them\n"
            "by commas in getsubopt(3) style. Additionally it is possible to\n"
            "provide the -E and -U options multiple times.\n"
+           "Only with -env it is possible to specify values containing commas.\n"
            "The following lines are equivalent:\n"
            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
            "    -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
+           "    -env -U LD_PRELOAD,LD_DEBUG var1=val2 var2=val2\n"
            "    QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
            "Note that if you provide several changes to a single variable\n"
            "the last change will stay in effect.\n");
@@ -3507,6 +3517,23 @@ static int parse_args(int argc, char **argv)
         }
     }
 
+    if (env_mode) {
+        for (;;) {
+            if (optind >= argc) {
+                break;
+            }
+            r = argv[optind];
+            if (strchr(r, '=') == NULL) {
+                break;
+            }
+            if (envlist_setenv(envlist, r) != 0) {
+                usage();
+            }
+
+            optind++;
+        }
+    }
+
     if (optind >= argc) {
         usage();
     }
diff --git qemu-doc.texi qemu-doc.texi
index 3034af4..f48a53e 100644
--- qemu-doc.texi
+++ qemu-doc.texi
@@ -2671,7 +2671,7 @@ qemu-i386 /usr/local/qemu-i386/wine/bin/wine \
 @subsection Command line options
 
 @example
-usage: qemu-i386 [-h] [-d] [-L path] [-s size] [-cpu model] [-g port] [-B offset] [-R size] program [arguments...]
+usage: qemu-i386 [-h] [-d] [-L path] [-s size] [-cpu model] [-g port] [-B offset] [-R size] [env list] program [arguments...]
 @end example
 
 @table @option
@@ -2683,6 +2683,10 @@ Set the x86 elf interpreter prefix (default=/usr/local/qemu-i386)
 Set the x86 stack size in bytes (default=524288)
 @item -cpu model
 Select CPU model (-cpu help for list and additional feature selection)
+@item -env
+Enable parsing of the @var{env list}.  This means that any
+@var{var}=@var{value} assignments before @var{program} will be added
+to the environment.
 @item -E @var{var}=@var{value}[,@var{var2}=@var{value2},...]
 Set environment @var{var} to @var{value}, @var{var2} to @var{value2},
 and so on.
-- 
1.7.10.4


[-- Attachment #2: Type: application/pgp-signature, Size: 489 bytes --]

  reply	other threads:[~2013-04-24 16:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-24 13:16 [Qemu-devel] Environment variables for user-mode QEMU Thomas Schwinge
2013-04-24 16:11 ` Thomas Schwinge [this message]
2013-04-25 15:06   ` [Qemu-devel] [PATCH 1/4] util/envlist: Properly forward a callback's error in envlist_parse Thomas Schwinge
2013-04-25 15:06     ` [Qemu-devel] [PATCH 2/4] linux-user: Use existing envlist_parse_set/envlist_parse_unset interface Thomas Schwinge
2013-04-25 15:06     ` [Qemu-devel] [PATCH 3/4] linux-user: Tell handler_arg_* which context they're invoked from Thomas Schwinge
2013-04-25 15:06     ` [Qemu-devel] [PATCH 4/4] linux-user: Restore original behavior of the -E and -U command-line options Thomas Schwinge
2013-04-25 15:52       ` Peter Maydell
2013-04-25 16:18         ` Thomas Schwinge
2013-04-25 16:21           ` Peter Maydell
2013-04-25 16:41             ` [Qemu-devel] [PATCH] qemu-doc: Option -ignore-environment removed Thomas Schwinge
2013-04-25 17:00               ` [Qemu-trivial] " Peter Maydell
2013-04-25 17:00                 ` [Qemu-devel] " Peter Maydell
2013-04-26 10:52               ` Stefan Hajnoczi
2013-04-25 16:37       ` [Qemu-devel] [PATCH v2] linux-user: Restore original behavior of the -E and -U command-line options Thomas Schwinge
  -- strict thread matches above, loose matches on Subject: below --
2013-04-25 12:22 [Qemu-devel] Environment variables for user-mode QEMU Riku Voipio
2013-04-30 12:17 ` Thomas Schwinge

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=87k3nrq56l.fsf@schwinge.name \
    --to=thomas@codesourcery.com \
    --cc=j.schauer@email.de \
    --cc=paul@codesourcery.com \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.