* [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable
@ 2019-09-08 10:48 Laurent Vivier
2019-09-08 10:48 ` [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag Laurent Vivier
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Laurent Vivier @ 2019-09-08 10:48 UTC (permalink / raw)
To: qemu-devel
Cc: Peter Maydell, Riku Voipio, Laurent Vivier, John Paul Adrian Glaubitz
filename is only used to open the file if AT_EXECFD is not provided.
But exec_path already contains the path of the file to open.
Remove filename as it is only used in main.c whereas exec_path is
also used in syscall.c.
Fixes: d088d664f201 ("linux-user: identify running binary in /proc/self/exe")
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
linux-user/main.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/linux-user/main.c b/linux-user/main.c
index 47917bbb20fc..28f0065b6ddf 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -49,7 +49,6 @@
char *exec_path;
int singlestep;
-static const char *filename;
static const char *argv0;
static int gdbstub_port;
static envlist_t *envlist;
@@ -586,7 +585,6 @@ static int parse_args(int argc, char **argv)
exit(EXIT_FAILURE);
}
- filename = argv[optind];
exec_path = argv[optind];
return optind;
@@ -657,9 +655,9 @@ int main(int argc, char **argv, char **envp)
execfd = qemu_getauxval(AT_EXECFD);
if (execfd == 0) {
- execfd = open(filename, O_RDONLY);
+ execfd = open(exec_path, O_RDONLY);
if (execfd < 0) {
- printf("Error while loading %s: %s\n", filename, strerror(errno));
+ printf("Error while loading %s: %s\n", exec_path, strerror(errno));
_exit(EXIT_FAILURE);
}
}
@@ -784,10 +782,10 @@ int main(int argc, char **argv, char **envp)
cpu->opaque = ts;
task_settid(ts);
- ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
+ ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
info, &bprm);
if (ret != 0) {
- printf("Error while loading %s: %s\n", filename, strerror(-ret));
+ printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
_exit(EXIT_FAILURE);
}
--
2.21.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag
2019-09-08 10:48 [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable Laurent Vivier
@ 2019-09-08 10:48 ` Laurent Vivier
2019-10-23 12:55 ` Laurent Vivier
2019-10-23 22:37 ` Christophe de Dinechin
2019-09-09 8:14 ` [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable Stefano Garzarella
2019-09-10 8:29 ` Laurent Vivier
2 siblings, 2 replies; 7+ messages in thread
From: Laurent Vivier @ 2019-09-08 10:48 UTC (permalink / raw)
To: qemu-devel
Cc: Peter Maydell, Riku Voipio, Laurent Vivier, John Paul Adrian Glaubitz
Add --preserve-arg0 in qemu-binfmt-conf.sh to configure the preserve-arg0
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>
---
Notes:
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 95134c0180b2..3568e192776c 100755
--- a/configure
+++ b/configure
@@ -498,6 +498,7 @@ libxml2=""
docker="no"
debug_mutex="no"
libpmem=""
+force_preserve_argv0="no"
default_devices="yes"
# cross compilers defaults, can be overridden with --cross-cc-ARCH
@@ -1543,6 +1544,8 @@ for opt do
;;
--disable-libpmem) libpmem=no
;;
+ --force-preserve-argv0) force_preserve_argv0=yes
+ ;;
*)
echo "ERROR: unknown option $opt"
echo "Try '$0 --help' for more information"
@@ -1740,6 +1743,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:
@@ -7736,6 +7741,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 28f0065b6ddf..02354d58e866 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -605,6 +605,7 @@ int main(int argc, char **argv, char **envp)
int i;
int ret;
int execfd;
+ bool preserve_argv0;
error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);
@@ -653,6 +654,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);
@@ -662,6 +666,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));
}
@@ -766,7 +788,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..7c9a4609c232 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-arg0 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-arg0 preserve arg[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-arg0: -- "$@")
eval set -- "$options"
while true ; do
@@ -380,6 +386,10 @@ while true ; do
shift
PERSISTENT="$1"
;;
+ -0|--preserve-arg0)
+ shift
+ PRESERVE_ARG0="$1"
+ ;;
*)
break
;;
--
2.21.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable
2019-09-08 10:48 [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable Laurent Vivier
2019-09-08 10:48 ` [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag Laurent Vivier
@ 2019-09-09 8:14 ` Stefano Garzarella
2019-09-10 8:29 ` Laurent Vivier
2 siblings, 0 replies; 7+ messages in thread
From: Stefano Garzarella @ 2019-09-09 8:14 UTC (permalink / raw)
To: Laurent Vivier
Cc: Peter Maydell, Riku Voipio, qemu-devel, John Paul Adrian Glaubitz
On Sun, Sep 08, 2019 at 12:48:15PM +0200, Laurent Vivier wrote:
> filename is only used to open the file if AT_EXECFD is not provided.
> But exec_path already contains the path of the file to open.
> Remove filename as it is only used in main.c whereas exec_path is
> also used in syscall.c.
>
> Fixes: d088d664f201 ("linux-user: identify running binary in /proc/self/exe")
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> ---
> linux-user/main.c | 10 ++++------
> 1 file changed, 4 insertions(+), 6 deletions(-)
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable
2019-09-08 10:48 [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable Laurent Vivier
2019-09-08 10:48 ` [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag Laurent Vivier
2019-09-09 8:14 ` [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable Stefano Garzarella
@ 2019-09-10 8:29 ` Laurent Vivier
2 siblings, 0 replies; 7+ messages in thread
From: Laurent Vivier @ 2019-09-10 8:29 UTC (permalink / raw)
To: qemu-devel; +Cc: Peter Maydell, Riku Voipio, John Paul Adrian Glaubitz
Le 08/09/2019 à 12:48, Laurent Vivier a écrit :
> filename is only used to open the file if AT_EXECFD is not provided.
> But exec_path already contains the path of the file to open.
> Remove filename as it is only used in main.c whereas exec_path is
> also used in syscall.c.
>
> Fixes: d088d664f201 ("linux-user: identify running binary in /proc/self/exe")
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> ---
> linux-user/main.c | 10 ++++------
> 1 file changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/linux-user/main.c b/linux-user/main.c
> index 47917bbb20fc..28f0065b6ddf 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -49,7 +49,6 @@
> char *exec_path;
>
> int singlestep;
> -static const char *filename;
> static const char *argv0;
> static int gdbstub_port;
> static envlist_t *envlist;
> @@ -586,7 +585,6 @@ static int parse_args(int argc, char **argv)
> exit(EXIT_FAILURE);
> }
>
> - filename = argv[optind];
> exec_path = argv[optind];
>
> return optind;
> @@ -657,9 +655,9 @@ int main(int argc, char **argv, char **envp)
>
> execfd = qemu_getauxval(AT_EXECFD);
> if (execfd == 0) {
> - execfd = open(filename, O_RDONLY);
> + execfd = open(exec_path, O_RDONLY);
> if (execfd < 0) {
> - printf("Error while loading %s: %s\n", filename, strerror(errno));
> + printf("Error while loading %s: %s\n", exec_path, strerror(errno));
> _exit(EXIT_FAILURE);
> }
> }
> @@ -784,10 +782,10 @@ int main(int argc, char **argv, char **envp)
> cpu->opaque = ts;
> task_settid(ts);
>
> - ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
> + ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
> info, &bprm);
> if (ret != 0) {
> - printf("Error while loading %s: %s\n", filename, strerror(-ret));
> + printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
> _exit(EXIT_FAILURE);
> }
>
>
Applied to my linux-user branch.
Thanks,
Laurent
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag
2019-09-08 10:48 ` [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag Laurent Vivier
@ 2019-10-23 12:55 ` Laurent Vivier
2019-10-23 22:37 ` Christophe de Dinechin
1 sibling, 0 replies; 7+ messages in thread
From: Laurent Vivier @ 2019-10-23 12:55 UTC (permalink / raw)
To: qemu-devel; +Cc: Peter Maydell, Riku Voipio, John Paul Adrian Glaubitz
Any comments on this?
It would be interesting to have this in 4.2
Thanks,
Laurent
Le 08/09/2019 à 12:48, Laurent Vivier a écrit :
> Add --preserve-arg0 in qemu-binfmt-conf.sh to configure the preserve-arg0
> 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>
> ---
>
> Notes:
> 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 95134c0180b2..3568e192776c 100755
> --- a/configure
> +++ b/configure
> @@ -498,6 +498,7 @@ libxml2=""
> docker="no"
> debug_mutex="no"
> libpmem=""
> +force_preserve_argv0="no"
> default_devices="yes"
>
> # cross compilers defaults, can be overridden with --cross-cc-ARCH
> @@ -1543,6 +1544,8 @@ for opt do
> ;;
> --disable-libpmem) libpmem=no
> ;;
> + --force-preserve-argv0) force_preserve_argv0=yes
> + ;;
> *)
> echo "ERROR: unknown option $opt"
> echo "Try '$0 --help' for more information"
> @@ -1740,6 +1743,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:
> @@ -7736,6 +7741,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 28f0065b6ddf..02354d58e866 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -605,6 +605,7 @@ int main(int argc, char **argv, char **envp)
> int i;
> int ret;
> int execfd;
> + bool preserve_argv0;
>
> error_init(argv[0]);
> module_call_init(MODULE_INIT_TRACE);
> @@ -653,6 +654,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);
> @@ -662,6 +666,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));
> }
> @@ -766,7 +788,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..7c9a4609c232 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-arg0 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-arg0 preserve arg[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-arg0: -- "$@")
> eval set -- "$options"
>
> while true ; do
> @@ -380,6 +386,10 @@ while true ; do
> shift
> PERSISTENT="$1"
> ;;
> + -0|--preserve-arg0)
> + shift
> + PRESERVE_ARG0="$1"
> + ;;
> *)
> break
> ;;
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag
2019-09-08 10:48 ` [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag Laurent Vivier
2019-10-23 12:55 ` Laurent Vivier
@ 2019-10-23 22:37 ` Christophe de Dinechin
2019-10-24 8:19 ` Laurent Vivier
1 sibling, 1 reply; 7+ messages in thread
From: Christophe de Dinechin @ 2019-10-23 22:37 UTC (permalink / raw)
To: qemu-devel
Cc: Peter Maydell, Riku Voipio, Laurent Vivier, John Paul Adrian Glaubitz
Laurent Vivier writes:
> Add --preserve-arg0 in qemu-binfmt-conf.sh to configure the preserve-arg0
> flag.
There is an inconsistency below, where some parts use preserve-argv0
and others preserve-arg0 (no v)
Frankly, I would accept both ;-)
>
> 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
Example of 'argv0' case
>
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> ---
>
> Notes:
> 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 95134c0180b2..3568e192776c 100755
> --- a/configure
> +++ b/configure
> @@ -498,6 +498,7 @@ libxml2=""
> docker="no"
> debug_mutex="no"
> libpmem=""
> +force_preserve_argv0="no"
> default_devices="yes"
>
> # cross compilers defaults, can be overridden with --cross-cc-ARCH
> @@ -1543,6 +1544,8 @@ for opt do
> ;;
> --disable-libpmem) libpmem=no
> ;;
> + --force-preserve-argv0) force_preserve_argv0=yes
> + ;;
> *)
> echo "ERROR: unknown option $opt"
> echo "Try '$0 --help' for more information"
> @@ -1740,6 +1743,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:
> @@ -7736,6 +7741,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 28f0065b6ddf..02354d58e866 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -605,6 +605,7 @@ int main(int argc, char **argv, char **envp)
> int i;
> int ret;
> int execfd;
> + bool preserve_argv0;
>
> error_init(argv[0]);
> module_call_init(MODULE_INIT_TRACE);
> @@ -653,6 +654,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);
> @@ -662,6 +666,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));
> }
> @@ -766,7 +788,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..7c9a4609c232 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-arg0 yes|no]
Example of arg0 case
>
> 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-arg0 preserve arg[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-arg0: -- "$@")
> eval set -- "$options"
>
> while true ; do
> @@ -380,6 +386,10 @@ while true ; do
> shift
> PERSISTENT="$1"
> ;;
> + -0|--preserve-arg0)
> + shift
> + PRESERVE_ARG0="$1"
> + ;;
> *)
> break
> ;;
--
Cheers,
Christophe de Dinechin (IRC c3d)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag
2019-10-23 22:37 ` Christophe de Dinechin
@ 2019-10-24 8:19 ` Laurent Vivier
0 siblings, 0 replies; 7+ messages in thread
From: Laurent Vivier @ 2019-10-24 8:19 UTC (permalink / raw)
To: Christophe de Dinechin, qemu-devel
Cc: Peter Maydell, Riku Voipio, John Paul Adrian Glaubitz
Le 24/10/2019 à 00:37, Christophe de Dinechin a écrit :
>
> Laurent Vivier writes:
>
>> Add --preserve-arg0 in qemu-binfmt-conf.sh to configure the preserve-arg0
>> flag.
>
> There is an inconsistency below, where some parts use preserve-argv0
> and others preserve-arg0 (no v)
I agree. I'm going to fix that.
Thanks,
Laurent
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2019-10-24 8:45 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-08 10:48 [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable Laurent Vivier
2019-09-08 10:48 ` [Qemu-devel] [PATCH v2 2/2] linux-user: manage binfmt-misc preserve-arg[0] flag Laurent Vivier
2019-10-23 12:55 ` Laurent Vivier
2019-10-23 22:37 ` Christophe de Dinechin
2019-10-24 8:19 ` Laurent Vivier
2019-09-09 8:14 ` [Qemu-devel] [PATCH v2 1/2] linux-user: remove useless variable Stefano Garzarella
2019-09-10 8:29 ` Laurent Vivier
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.