Hello,

when executing a script with ash from busybox in the shebang then the command name will contain 
"sh" instead of the name of the script as expected. This means you can't use "ps -C 
<script name>", which is possible if you use bash in the script.

This patch fixes ash, but there may be more uses of bb_busybox_exec_path that need to be 
fixed, but I'm not sure which. If there are then this patch probably reintroduces the bug 
that "exe" is used as the command name.

/Mikael

>From cf964124a82def733039be4040e1b1edd6bae859 Mon Sep 17 00:00:00 2001
From: Mikael Magnusson <[email protected]>
Date: Sun, 10 Oct 2021 02:19:50 +0200
Subject: [PATCH] ash: fix command name of shell scripts

Transfer the command name to be visible in /proc/self/comm in the environ
variable BB_COMM to child processes. This fixes a long standing bug that
replaced a scripts name with the name of the busybox applet, i.e. sh or
ash.

This fix allows you to find a specific shell script process using
"ps -C <name of the script>", the same way as when using bash or
another shell in the script.

Signed-off-by: Mikael Magnusson <[email protected]>
---
 libbb/appletlib.c                  |  9 +++++++--
 shell/ash.c                        |  4 ++++
 shell/ash_test/ash-comm/comm.right |  8 ++++++++
 shell/ash_test/ash-comm/comm.tests | 13 +++++++++++++
 4 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 shell/ash_test/ash-comm/comm.right
 create mode 100755 shell/ash_test/ash-comm/comm.tests

diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index bf26c99e9..caf7160d0 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -1112,8 +1112,13 @@ int main(int argc UNUSED_PARAM, char **argv)
 	 || ENABLE_FEATURE_PREFER_APPLETS
 	 || !BB_MMU
 	) {
-		if (NUM_APPLETS > 1)
-			set_task_comm(applet_name);
+		if (NUM_APPLETS > 1) {
+			const char *comm = getenv("BB_COMM");
+			if (comm) {
+				set_task_comm(comm);
+				unsetenv("BB_COMM");
+			}
+		}
 	}
 
 	parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
diff --git a/shell/ash.c b/shell/ash.c
index 199975191..cfcea1990 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -8296,7 +8296,11 @@ static void shellexec(char *prog, char **argv, const char *path, int idx)
 	char **envp;
 	int exerrno;
 	int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */
+	char *comm;
 
+	comm = strrchr(prog, '/');
+	comm = comm ? comm+1 : prog;
+	setvar("BB_COMM", comm, VEXPORT);
 	envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL);
 	if (strchr(prog, '/') != NULL
 #if ENABLE_FEATURE_SH_STANDALONE
diff --git a/shell/ash_test/ash-comm/comm.right b/shell/ash_test/ash-comm/comm.right
new file mode 100644
index 000000000..df610f6cb
--- /dev/null
+++ b/shell/ash_test/ash-comm/comm.right
@@ -0,0 +1,8 @@
+busybox
+comm.tmp
+busybox
+comm.tmp
+busybox
+ash
+busybox
+ash
diff --git a/shell/ash_test/ash-comm/comm.tests b/shell/ash_test/ash-comm/comm.tests
new file mode 100755
index 000000000..10b749a6f
--- /dev/null
+++ b/shell/ash_test/ash-comm/comm.tests
@@ -0,0 +1,13 @@
+cat > comm.tmp << EOF
+#!$THIS_SH
+procdir="/proc/\$\$"
+echo \$(basename \$(readlink \${procdir}/exe))
+cat \${procdir}/comm
+EOF
+
+chmod 755 comm.tmp
+./comm.tmp
+(exec ./comm.tmp)
+ash ./comm.tmp
+(exec ash ./comm.tmp)
+rm comm.tmp
-- 
2.25.1


_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to