Pádraig Brady <[email protected]> writes:

> I think this would use "ginstall" rather than "install".
> Also more generally could misattribute comm in the case where you installed
> `./configure --enable-single-binary --program-prefix=g` alongside another 
> implementation.
> In general when copying shebang wrapper scripts to another name,
> the basename of that wrapper script should be used.
>
> I.e. it seems better to use this?
>
>   prctl (PR_SET_NAME, last_component (prog_argv[0]));

Yep, you are right, and good point. How does the attached patch look?
I've tested it with all of the --single-binary options.

Since the program transformations, e.g., --program-prefix,
--program-transform-name, etc. are applied during installation we have
to create out own binary.

Collin

>From 029eaaa5fc62b121ae356faf5dad83add062e605 Mon Sep 17 00:00:00 2001
Message-ID: <029eaaa5fc62b121ae356faf5dad83add062e605.1792268580.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Sat, 17 Oct 2026 13:12:19 -0700
Subject: [PATCH] all: use the basename of argv[0] for the comm value in the
 single binary
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This preserves any program name transformations, e.g., when
--program-prefix=g is used as a configuration option, unlike commit
ef874ed6c (all: set /proc/self/comm to the program name in the single
binary, 2026-10-17).

* src/coreutils.c (launch_program): Use the last component of argv[0]
when setting the comm value using prctl (PR_SET_NAME, ...).
* tests/misc/proc-self-comm.sh: When using
--enable-single-binary=shebangs, test that the comm value preserves any
program name transformations, e.g., --program-prefix=g.
* NEWS: Adjust the NEWS entry to refer to last component of argv[0]
instead of the program name.
Suggested by Pádraig Brady.
---
 NEWS                         |  9 +++++----
 src/coreutils.c              |  2 +-
 tests/misc/proc-self-comm.sh | 18 ++++++++++++++----
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index 3ce4e5b7c..e907a6625 100644
--- a/NEWS
+++ b/NEWS
@@ -52,10 +52,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   would likely crash due to this undefined behavior.
   [bug introduced in coreutils-9.10]
 
-  On GNU/Linux, when configured with --enable-single-binary=symlinks, the comm
-  value listed in /proc/self/comm is now set to the program name rather than
-  argv[0].  Previously, when programs were invoked using their file name, the
-  comm value would contain path components.
+  On GNU/Linux, when configured with --enable-single-binary=symlinks,
+  the comm value listed in /proc/self/comm is now set to the last
+  component of argv[0] rather than argv[0] itself.  Previously, when
+  programs were invoked using their file name, the comm value would
+  contain path components.
   [bug introduced with multi-call binary support in coreutils-8.23]
 
 ** New Features
diff --git a/src/coreutils.c b/src/coreutils.c
index ab52ba17b..9701a5b48 100644
--- a/src/coreutils.c
+++ b/src/coreutils.c
@@ -114,7 +114,7 @@ launch_program (char const *prog_name, int prog_argc, char **prog_argv)
 
 #if HAVE_PRCTL && defined PR_SET_NAME
   /* Not being able to set the program name is not a fatal error.  */
-  prctl (PR_SET_NAME, prog_name);
+  prctl (PR_SET_NAME, last_component (prog_argv[0]));
 #endif
 #if HAVE_PRCTL && defined PR_SET_MM_ARG_START
   /* Shift the beginning of the command line to prog_argv[0] (if set) so
diff --git a/tests/misc/proc-self-comm.sh b/tests/misc/proc-self-comm.sh
index 97261731f..9e7caabf8 100755
--- a/tests/misc/proc-self-comm.sh
+++ b/tests/misc/proc-self-comm.sh
@@ -33,19 +33,29 @@ if test -n "$single_binary_cat" && ! $single_binary_cat /dev/null 2>err; then
   single_binary_cat=
 fi
 
+mkdir dir || framework_failure_
+
 # If we are not using --enable-single-binary=symlinks, then set
 # up a symlink in the test directory to ensure this gets tested.
 if test -n "$single_binary" && ! test -L "$abs_cat"; then
-  mkdir dir || framework_failure_
   ln -s "$single_binary" dir/cat || framework_failure_
   symlink_cat='env PATH='"$(pwd)/dir:$PATH"' cat'
+  # If we are using --enable-single-binary=shebangs, create a
+  # binary to pretend we are using --program-prefix=g.
+  if test $(wc -l < "$abs_cat") = 1; then
+    ginstall "$abs_cat" dir/gcat || framework_faiure_
+    gcat=dir/gcat
+  fi
 fi
 
-echo 'cat' >exp || framework_failure_
-for prog in cat "$abs_cat" "$single_binary_cat" "$symlink_cat"; do
+echo 'cat' >cat-exp || framework_failure_
+echo 'gcat' >gcat-exp || framework_failure_
+for prog in cat "$abs_cat" "$single_binary_cat" "$symlink_cat" "$gcat"; do
   test -z "$prog" && continue
+  exp=cat-exp
+  test "$prog" = "$gcat" && exp=gcat-exp
   $prog /proc/self/comm >out 2>err || fail=1
-  compare exp out || fail=1
+  compare $exp out || fail=1
   compare /dev/null err || fail=1
 done
 
-- 
2.56.0

Reply via email to