[PATCH v3] linux-user: manage binfmt-misc preserve-arg[0] flag

Laurent Vivier posted 1 patch 2 years, 7 months ago
Test docker-mingw@fedora passed
Test docker-clang@ubuntu passed
Test docker-quick@centos7 passed
Test asan passed
Test checkpatch failed
Test FreeBSD passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20191024153847.31815-1-laurent@vivier.eu
Maintainers: Laurent Vivier <laurent@vivier.eu>, Riku Voipio <riku.voipio@iki.fi>
configure                   |  8 +++++++
linux-user/main.c           | 24 +++++++++++++++++++-
scripts/qemu-binfmt-conf.sh | 44 +++++++++++++++++++++++--------------
3 files changed, 58 insertions(+), 18 deletions(-)
[PATCH v3] linux-user: manage binfmt-misc preserve-arg[0] flag
Posted by Laurent Vivier 2 years, 7 months ago
Add --preserve-argv0 in qemu-binfmt-conf.sh to configure the preserve-argv0
flag.

Now, if QEMU is started with -0 or QEMU_ARGV0 and an empty parameter
argv[0] (the full pathname provided by binfmt-misc) is removed and
replaced by argv[1] (the original argv[0] provided by binfmt-misc when
'P'/preserve-arg[0] is set)

For instance:

  $ sudo QEMU_ARGV0= chroot m68k-chroot sh -c 'echo $0'
  sh

without this patch:

  $ sudo chroot m68k-chroot sh -c 'echo $0'
  /usr/bin/sh

QEMU can be forced to always use preserve-argv[0] at configuration
time with --force-preserve-argv0

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
 configure                   |  8 +++++++
 linux-user/main.c           | 24 +++++++++++++++++++-
 scripts/qemu-binfmt-conf.sh | 44 +++++++++++++++++++++++--------------
 3 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/configure b/configure
index 145fcabbb3c3..94c41f0a068a 100755
--- a/configure
+++ b/configure
@@ -497,6 +497,7 @@ sheepdog="yes"
 libxml2=""
 debug_mutex="no"
 libpmem=""
+force_preserve_argv0="no"
 default_devices="yes"
 
 supported_cpu="no"
@@ -1529,6 +1530,8 @@ for opt do
   ;;
   --disable-xkbcommon) xkbcommon=no
   ;;
+  --force-preserve-argv0) force_preserve_argv0=yes
+  ;;
   *)
       echo "ERROR: unknown option $opt"
       echo "Try '$0 --help' for more information"
@@ -1710,6 +1713,8 @@ Advanced options (experts only):
   --enable-profiler        profiler support
   --enable-debug-stack-usage
                            track the maximum stack usage of stacks created by qemu_alloc_stack
+  --force-preserve-argv0   for linux-user only, force the use of binfmt_misc 'P'
+                           flag (preserve-argv[0])
 
 Optional features, enabled with --enable-FEATURE and
 disabled with --disable-FEATURE, default is enabled if available:
@@ -7633,6 +7638,9 @@ if test "$target_user_only" = "yes" ; then
 fi
 if test "$target_linux_user" = "yes" ; then
   echo "CONFIG_LINUX_USER=y" >> $config_target_mak
+  if test "$force_preserve_argv0" = "yes" ; then
+    echo "CONFIG_FORCE_PRESERVE_ARGV0=y" >> $config_target_mak
+  fi
 fi
 list=""
 if test ! -z "$gdb_xml_files" ; then
diff --git a/linux-user/main.c b/linux-user/main.c
index 560d053f7249..1b4df24ef483 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -616,6 +616,7 @@ int main(int argc, char **argv, char **envp)
     int ret;
     int execfd;
     unsigned long max_reserved_va;
+    bool preserve_argv0;
 
     error_init(argv[0]);
     module_call_init(MODULE_INIT_TRACE);
@@ -664,6 +665,9 @@ int main(int argc, char **argv, char **envp)
 
     init_qemu_uname_release();
 
+    /*
+     * Manage binfmt-misc open-binary flag
+     */
     execfd = qemu_getauxval(AT_EXECFD);
     if (execfd == 0) {
         execfd = open(exec_path, O_RDONLY);
@@ -673,6 +677,24 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
+     /*
+      * argv0 with an empty string will set argv[optind + 1]
+      * as target_argv[0]
+      */
+#ifdef CONFIG_FORCE_PRESERVE_ARGV0
+    preserve_argv0 = true;
+#else
+    preserve_argv0 = (argv0 != NULL && argv0[0] == 0);
+#endif
+    /*
+     * Manage binfmt-misc preserve-arg[0] flag
+     *    argv[optind]     full path to the binary
+     *    argv[optind + 1] original argv[0]
+     */
+    if (optind + 1 < argc && preserve_argv0) {
+        optind++;
+    }
+
     if (cpu_model == NULL) {
         cpu_model = cpu_get_model(get_elf_eflags(execfd));
     }
@@ -777,7 +799,7 @@ int main(int argc, char **argv, char **envp)
      * argv[0] pointer with the given one.
      */
     i = 0;
-    if (argv0 != NULL) {
+    if (argv0 != NULL && argv0[0] != 0) {
         target_argv[i++] = strdup(argv0);
     }
     for (; i < target_argc; i++) {
diff --git a/scripts/qemu-binfmt-conf.sh b/scripts/qemu-binfmt-conf.sh
index b5a16742a149..275d3cf57e83 100755
--- a/scripts/qemu-binfmt-conf.sh
+++ b/scripts/qemu-binfmt-conf.sh
@@ -170,25 +170,27 @@ usage() {
 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
                            [--help][--credential yes|no][--exportdir PATH]
                            [--persistent yes|no][--qemu-suffix SUFFIX]
+                           [--preserve-argv0 yes|no]
 
        Configure binfmt_misc to use qemu interpreter
 
-       --help:        display this usage
-       --qemu-path:   set path to qemu interpreter ($QEMU_PATH)
-       --qemu-suffix: add a suffix to the default interpreter name
-       --debian:      don't write into /proc,
-                      instead generate update-binfmts templates
-       --systemd:     don't write into /proc,
-                      instead generate file for systemd-binfmt.service
-                      for the given CPU. If CPU is "ALL", generate a
-                      file for all known cpus
-       --exportdir:   define where to write configuration files
-                      (default: $SYSTEMDDIR or $DEBIANDIR)
-       --credential:  if yes, credential and security tokens are
-                      calculated according to the binary to interpret
-       --persistent:  if yes, the interpreter is loaded when binfmt is
-                      configured and remains in memory. All future uses
-                      are cloned from the open file.
+       --help:          display this usage
+       --qemu-path:     set path to qemu interpreter ($QEMU_PATH)
+       --qemu-suffix:   add a suffix to the default interpreter name
+       --debian:        don't write into /proc,
+                        instead generate update-binfmts templates
+       --systemd:       don't write into /proc,
+                        instead generate file for systemd-binfmt.service
+                        for the given CPU. If CPU is "ALL", generate a
+                        file for all known cpus
+       --exportdir:     define where to write configuration files
+                        (default: $SYSTEMDDIR or $DEBIANDIR)
+       --credential:    if yes, credential and security tokens are
+                        calculated according to the binary to interpret
+       --persistent:    if yes, the interpreter is loaded when binfmt is
+                        configured and remains in memory. All future uses
+                        are cloned from the open file.
+       --preserve-argv0 preserve argv[0]
 
     To import templates with update-binfmts, use :
 
@@ -261,6 +263,9 @@ qemu_generate_register() {
     if [ "$PERSISTENT" = "yes" ] ; then
         flags="${flags}F"
     fi
+    if [ "$PRESERVE_ARG0" = "yes" ] ; then
+        flags="${flags}P"
+    fi
 
     echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags"
 }
@@ -322,9 +327,10 @@ DEBIANDIR="/usr/share/binfmts"
 QEMU_PATH=/usr/local/bin
 CREDENTIAL=no
 PERSISTENT=no
+PRESERVE_ARG0=no
 QEMU_SUFFIX=""
 
-options=$(getopt -o ds:Q:S:e:hc:p: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent: -- "$@")
+options=$(getopt -o ds:Q:S:e:hc:p:0: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent:,preserve-argv0: -- "$@")
 eval set -- "$options"
 
 while true ; do
@@ -380,6 +386,10 @@ while true ; do
         shift
         PERSISTENT="$1"
         ;;
+    -0|--preserve-argv0)
+        shift
+        PRESERVE_ARG0="$1"
+        ;;
     *)
         break
         ;;
-- 
2.21.0


Re: [PATCH v3] linux-user: manage binfmt-misc preserve-arg[0] flag
Posted by Laurent Vivier 2 years, 7 months ago
Le 24/10/2019 à 17:38, Laurent Vivier a écrit :
> Add --preserve-argv0 in qemu-binfmt-conf.sh to configure the preserve-argv0
> flag.
> 
> Now, if QEMU is started with -0 or QEMU_ARGV0 and an empty parameter
> argv[0] (the full pathname provided by binfmt-misc) is removed and
> replaced by argv[1] (the original argv[0] provided by binfmt-misc when
> 'P'/preserve-arg[0] is set)
> 
> For instance:
> 
>   $ sudo QEMU_ARGV0= chroot m68k-chroot sh -c 'echo $0'
>   sh
> 
> without this patch:
> 
>   $ sudo chroot m68k-chroot sh -c 'echo $0'
>   /usr/bin/sh
> 
> QEMU can be forced to always use preserve-argv[0] at configuration
> time with --force-preserve-argv0
> 
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> ---

missing revision history:

    v3: s/preserve-arg0/preserve-argv0/
    v2: add --force-preserve-argv0 configure option

>  configure                   |  8 +++++++
>  linux-user/main.c           | 24 +++++++++++++++++++-
>  scripts/qemu-binfmt-conf.sh | 44 +++++++++++++++++++++++--------------
>  3 files changed, 58 insertions(+), 18 deletions(-)
> 
> diff --git a/configure b/configure
> index 145fcabbb3c3..94c41f0a068a 100755
> --- a/configure
> +++ b/configure
> @@ -497,6 +497,7 @@ sheepdog="yes"
>  libxml2=""
>  debug_mutex="no"
>  libpmem=""
> +force_preserve_argv0="no"
>  default_devices="yes"
>  
>  supported_cpu="no"
> @@ -1529,6 +1530,8 @@ for opt do
>    ;;
>    --disable-xkbcommon) xkbcommon=no
>    ;;
> +  --force-preserve-argv0) force_preserve_argv0=yes
> +  ;;
>    *)
>        echo "ERROR: unknown option $opt"
>        echo "Try '$0 --help' for more information"
> @@ -1710,6 +1713,8 @@ Advanced options (experts only):
>    --enable-profiler        profiler support
>    --enable-debug-stack-usage
>                             track the maximum stack usage of stacks created by qemu_alloc_stack
> +  --force-preserve-argv0   for linux-user only, force the use of binfmt_misc 'P'
> +                           flag (preserve-argv[0])
>  
>  Optional features, enabled with --enable-FEATURE and
>  disabled with --disable-FEATURE, default is enabled if available:
> @@ -7633,6 +7638,9 @@ if test "$target_user_only" = "yes" ; then
>  fi
>  if test "$target_linux_user" = "yes" ; then
>    echo "CONFIG_LINUX_USER=y" >> $config_target_mak
> +  if test "$force_preserve_argv0" = "yes" ; then
> +    echo "CONFIG_FORCE_PRESERVE_ARGV0=y" >> $config_target_mak
> +  fi
>  fi
>  list=""
>  if test ! -z "$gdb_xml_files" ; then
> diff --git a/linux-user/main.c b/linux-user/main.c
> index 560d053f7249..1b4df24ef483 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -616,6 +616,7 @@ int main(int argc, char **argv, char **envp)
>      int ret;
>      int execfd;
>      unsigned long max_reserved_va;
> +    bool preserve_argv0;
>  
>      error_init(argv[0]);
>      module_call_init(MODULE_INIT_TRACE);
> @@ -664,6 +665,9 @@ int main(int argc, char **argv, char **envp)
>  
>      init_qemu_uname_release();
>  
> +    /*
> +     * Manage binfmt-misc open-binary flag
> +     */
>      execfd = qemu_getauxval(AT_EXECFD);
>      if (execfd == 0) {
>          execfd = open(exec_path, O_RDONLY);
> @@ -673,6 +677,24 @@ int main(int argc, char **argv, char **envp)
>          }
>      }
>  
> +     /*
> +      * argv0 with an empty string will set argv[optind + 1]
> +      * as target_argv[0]
> +      */
> +#ifdef CONFIG_FORCE_PRESERVE_ARGV0
> +    preserve_argv0 = true;
> +#else
> +    preserve_argv0 = (argv0 != NULL && argv0[0] == 0);
> +#endif
> +    /*
> +     * Manage binfmt-misc preserve-arg[0] flag
> +     *    argv[optind]     full path to the binary
> +     *    argv[optind + 1] original argv[0]
> +     */
> +    if (optind + 1 < argc && preserve_argv0) {
> +        optind++;
> +    }
> +
>      if (cpu_model == NULL) {
>          cpu_model = cpu_get_model(get_elf_eflags(execfd));
>      }
> @@ -777,7 +799,7 @@ int main(int argc, char **argv, char **envp)
>       * argv[0] pointer with the given one.
>       */
>      i = 0;
> -    if (argv0 != NULL) {
> +    if (argv0 != NULL && argv0[0] != 0) {
>          target_argv[i++] = strdup(argv0);
>      }
>      for (; i < target_argc; i++) {
> diff --git a/scripts/qemu-binfmt-conf.sh b/scripts/qemu-binfmt-conf.sh
> index b5a16742a149..275d3cf57e83 100755
> --- a/scripts/qemu-binfmt-conf.sh
> +++ b/scripts/qemu-binfmt-conf.sh
> @@ -170,25 +170,27 @@ usage() {
>  Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
>                             [--help][--credential yes|no][--exportdir PATH]
>                             [--persistent yes|no][--qemu-suffix SUFFIX]
> +                           [--preserve-argv0 yes|no]
>  
>         Configure binfmt_misc to use qemu interpreter
>  
> -       --help:        display this usage
> -       --qemu-path:   set path to qemu interpreter ($QEMU_PATH)
> -       --qemu-suffix: add a suffix to the default interpreter name
> -       --debian:      don't write into /proc,
> -                      instead generate update-binfmts templates
> -       --systemd:     don't write into /proc,
> -                      instead generate file for systemd-binfmt.service
> -                      for the given CPU. If CPU is "ALL", generate a
> -                      file for all known cpus
> -       --exportdir:   define where to write configuration files
> -                      (default: $SYSTEMDDIR or $DEBIANDIR)
> -       --credential:  if yes, credential and security tokens are
> -                      calculated according to the binary to interpret
> -       --persistent:  if yes, the interpreter is loaded when binfmt is
> -                      configured and remains in memory. All future uses
> -                      are cloned from the open file.
> +       --help:          display this usage
> +       --qemu-path:     set path to qemu interpreter ($QEMU_PATH)
> +       --qemu-suffix:   add a suffix to the default interpreter name
> +       --debian:        don't write into /proc,
> +                        instead generate update-binfmts templates
> +       --systemd:       don't write into /proc,
> +                        instead generate file for systemd-binfmt.service
> +                        for the given CPU. If CPU is "ALL", generate a
> +                        file for all known cpus
> +       --exportdir:     define where to write configuration files
> +                        (default: $SYSTEMDDIR or $DEBIANDIR)
> +       --credential:    if yes, credential and security tokens are
> +                        calculated according to the binary to interpret
> +       --persistent:    if yes, the interpreter is loaded when binfmt is
> +                        configured and remains in memory. All future uses
> +                        are cloned from the open file.
> +       --preserve-argv0 preserve argv[0]
>  
>      To import templates with update-binfmts, use :
>  
> @@ -261,6 +263,9 @@ qemu_generate_register() {
>      if [ "$PERSISTENT" = "yes" ] ; then
>          flags="${flags}F"
>      fi
> +    if [ "$PRESERVE_ARG0" = "yes" ] ; then
> +        flags="${flags}P"
> +    fi
>  
>      echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags"
>  }
> @@ -322,9 +327,10 @@ DEBIANDIR="/usr/share/binfmts"
>  QEMU_PATH=/usr/local/bin
>  CREDENTIAL=no
>  PERSISTENT=no
> +PRESERVE_ARG0=no
>  QEMU_SUFFIX=""
>  
> -options=$(getopt -o ds:Q:S:e:hc:p: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent: -- "$@")
> +options=$(getopt -o ds:Q:S:e:hc:p:0: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent:,preserve-argv0: -- "$@")
>  eval set -- "$options"
>  
>  while true ; do
> @@ -380,6 +386,10 @@ while true ; do
>          shift
>          PERSISTENT="$1"
>          ;;
> +    -0|--preserve-argv0)
> +        shift
> +        PRESERVE_ARG0="$1"
> +        ;;
>      *)
>          break
>          ;;
> 


Re: [PATCH v3] linux-user: manage binfmt-misc preserve-arg[0] flag
Posted by no-reply@patchew.org 2 years, 7 months ago
Patchew URL: https://patchew.org/QEMU/20191024153847.31815-1-laurent@vivier.eu/



Hi,

This series seems to have some coding style problems. See output below for
more information:

Subject: [PATCH v3] linux-user: manage binfmt-misc preserve-arg[0] flag
Type: series
Message-id: 20191024153847.31815-1-laurent@vivier.eu

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
239dee8 linux-user: manage binfmt-misc preserve-arg[0] flag

=== OUTPUT BEGIN ===
ERROR: line over 90 characters
#193: FILE: scripts/qemu-binfmt-conf.sh:333:
+options=$(getopt -o ds:Q:S:e:hc:p:0: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent:,preserve-argv0: -- "$@")

total: 1 errors, 0 warnings, 153 lines checked

Commit 239dee8e7db4 (linux-user: manage binfmt-misc preserve-arg[0] flag) has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1


The full log is available at
http://patchew.org/logs/20191024153847.31815-1-laurent@vivier.eu/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to patchew-devel@redhat.com