From nobody Sat Apr 20 07:35:24 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1571934853; cv=none; d=zoho.com; s=zohoarc; b=ZnKUBz6YjnzmlH084WeZWU/FSrgfTCzxpFovQQ02PsF/G0oo+J1YbC839aoyKudwCHS0Jdyxw8tvCkTlTPJKKCIF/2yTt6jQeipUsV8ENBCP2tv+q7rKkQOsa7hJFaDAL8acFtIr6GvysaPvqx2Iz7EIELKwkQCd+YlsMMk80NQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571934853; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=rmE4hNWPuLGGE+NCA+bI4lM8MYgu1u2OZY/LvgoOD7Y=; b=c8nADohbjPLTBn1LjDQIgN+W7hrx4eQzDROfFac9TKYLTL58qkwCMMv0eKHFccrRJwbqHtzi4iPBTXQcrRUUZ2v3woIxNvcTt5BcSN13PdzBvuihXYGb56zBF8dPgoHPePuX3KI11v4mkeY2U0GV6+713/HuANtybuuh/fgKDXw= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571934852885640.1941365303688; Thu, 24 Oct 2019 09:34:12 -0700 (PDT) Received: from localhost ([::1]:47262 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNg3u-0000Xn-PB for importer@patchew.org; Thu, 24 Oct 2019 12:34:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49280) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNfCp-0005m3-Ld for qemu-devel@nongnu.org; Thu, 24 Oct 2019 11:39:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNfCl-0000S0-VD for qemu-devel@nongnu.org; Thu, 24 Oct 2019 11:39:19 -0400 Received: from mout.kundenserver.de ([217.72.192.75]:38911) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iNfCl-0000QG-Lc for qemu-devel@nongnu.org; Thu, 24 Oct 2019 11:39:15 -0400 Received: from localhost.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1Mtfa5-1i9RER2wb1-00v9YY; Thu, 24 Oct 2019 17:38:52 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Subject: [PATCH v3] linux-user: manage binfmt-misc preserve-arg[0] flag Date: Thu, 24 Oct 2019 17:38:47 +0200 Message-Id: <20191024153847.31815-1-laurent@vivier.eu> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:x5gh9EuzJOc9DIK8kGSHKB7aeZV96XQ5z4Ni9URsl4w7F61Tu36 cAr/X4Y6mYGrOFxRmAmpZds3gWPBVcYZUMT3BldbqI/lGPZ/ikyYJKw8vgi74pZ+wVXgzlQ eKEG151fhrkTrVD0iAuTapepIF+svEWro5MnFoWGodVOah2kl2viW9MVJ3sMIyzX6ISpIPC gxIFd9SEQHNYLiv2trfZw== X-UI-Out-Filterresults: notjunk:1;V03:K0:ajhX67FJl74=:flU6qwJvKy2rYmCUSEvI/M nmineMqmWCha/6e1c/RxjfLt3uUCSJIEXPHrL08vVg7eQ8ast1dBe2tteweB/zgUe79qyuTVn UumSoQjr6Jg7e7GwihTarG8hK9LJBtXBFgXBqBM77AKQAQ0qRtGSkUm3JwcH87geGGuy9aTfF r34P8zZ1KWhhk/Sb5temIFcKHJLBll7QXh0xEaGqzAedPHSVd+0DD5aA4+KPFwp4XqMLSQamU uFwjzmJlF+vk95djbR8353Rio/r/JF4FVhUf6yw0tUvGazsXMpEqW/ExyEtqEgfOBE0jXtFkn t2Ek+gT1Hj4QR9EohD9y293Z2v5lc91TJSRKH0xIXqgnd6mlC0G1Ts9wlkK8RGEawe4m3CXYi 68uohws3yvZPLJMxRsBnNHMMqTudBBpEPTFWRhQQYYIuMWuh9vNckkiVYUQChzL9DO2wpWWak d2KGSPcDhexEdUiduZO5F/D1vbDh8jAjg7XAgi+ItgMXhAR1dVYUJdu89O7w3kvxEhka5OE43 RSymB7Pi141VcBBKCCiv+38wvZsaExW385phfAKcjzfGXqFC2PQ96GR87bAjG5Hy4cEfYwGpR 25n/fCyX3MtPiUiGI291KR8O14JJfod24TR0hoqO1Qjii45heaMol4j0PqQwhNLsCPYwNs1BV 1C8CLC90wIxODslkjhmFa0nBPL3et3M61P2HcctzgJh/jnwiWYmd/4U41O9NjVlHe4ISGkQBX vJCzj7E21RQybm9J2zuOYptllaAeIsuOx8OyjkqFSbY/JXT225K0yy2k6zp/puNL5ExoiJrr2 zTrY0VOVFiyWlZ6Rg9PxPzTpZgo4uT6ldwrV/8cFOOWDxE1nHJv2ycZFUPw+28mIbA7BSSLIx YAgH8DEiwXXQkO//1E1w== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 217.72.192.75 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Christophe de Dinechin , Peter Maydell , Riku Voipio , Laurent Vivier , John Paul Adrian Glaubitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" 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=3D 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 --- 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=3D"yes" libxml2=3D"" debug_mutex=3D"no" libpmem=3D"" +force_preserve_argv0=3D"no" default_devices=3D"yes" =20 supported_cpu=3D"no" @@ -1529,6 +1530,8 @@ for opt do ;; --disable-xkbcommon) xkbcommon=3Dno ;; + --force-preserve-argv0) force_preserve_argv0=3Dyes + ;; *) 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_mi= sc 'P' + flag (preserve-argv[0]) =20 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" =3D "yes" ; then fi if test "$target_linux_user" =3D "yes" ; then echo "CONFIG_LINUX_USER=3Dy" >> $config_target_mak + if test "$force_preserve_argv0" =3D "yes" ; then + echo "CONFIG_FORCE_PRESERVE_ARGV0=3Dy" >> $config_target_mak + fi fi list=3D"" 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; =20 error_init(argv[0]); module_call_init(MODULE_INIT_TRACE); @@ -664,6 +665,9 @@ int main(int argc, char **argv, char **envp) =20 init_qemu_uname_release(); =20 + /* + * Manage binfmt-misc open-binary flag + */ execfd =3D qemu_getauxval(AT_EXECFD); if (execfd =3D=3D 0) { execfd =3D open(exec_path, O_RDONLY); @@ -673,6 +677,24 @@ int main(int argc, char **argv, char **envp) } } =20 + /* + * argv0 with an empty string will set argv[optind + 1] + * as target_argv[0] + */ +#ifdef CONFIG_FORCE_PRESERVE_ARGV0 + preserve_argv0 =3D true; +#else + preserve_argv0 =3D (argv0 !=3D NULL && argv0[0] =3D=3D 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 =3D=3D NULL) { cpu_model =3D 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 =3D 0; - if (argv0 !=3D NULL) { + if (argv0 !=3D NULL && argv0[0] !=3D 0) { target_argv[i++] =3D 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] =20 Configure binfmt_misc to use qemu interpreter =20 - --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] =20 To import templates with update-binfmts, use : =20 @@ -261,6 +263,9 @@ qemu_generate_register() { if [ "$PERSISTENT" =3D "yes" ] ; then flags=3D"${flags}F" fi + if [ "$PRESERVE_ARG0" =3D "yes" ] ; then + flags=3D"${flags}P" + fi =20 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags" } @@ -322,9 +327,10 @@ DEBIANDIR=3D"/usr/share/binfmts" QEMU_PATH=3D/usr/local/bin CREDENTIAL=3Dno PERSISTENT=3Dno +PRESERVE_ARG0=3Dno QEMU_SUFFIX=3D"" =20 -options=3D$(getopt -o ds:Q:S:e:hc:p: -l debian,systemd:,qemu-path:,qemu-su= ffix:,exportdir:,help,credential:,persistent: -- "$@") +options=3D$(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" =20 while true ; do @@ -380,6 +386,10 @@ while true ; do shift PERSISTENT=3D"$1" ;; + -0|--preserve-argv0) + shift + PRESERVE_ARG0=3D"$1" + ;; *) break ;; --=20 2.21.0