summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/appletlib.c10
-rw-r--r--libbb/vfork_daemon_rexec.c16
-rw-r--r--shell/ash_test/ash-comm/comm.right6
-rwxr-xr-xshell/ash_test/ash-comm/comm.tests20
-rw-r--r--shell/hush_test/hush-comm/comm.right6
-rwxr-xr-xshell/hush_test/hush-comm/comm.tests20
7 files changed, 78 insertions, 2 deletions
diff --git a/include/libbb.h b/include/libbb.h
index a340f27..2a0b272 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1271,8 +1271,10 @@ void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_
#endif
void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC;
#if defined(__linux__)
+int re_execed_comm(void) FAST_FUNC;
void set_task_comm(const char *comm) FAST_FUNC;
#else
+# define re_execed_comm() 0
# define set_task_comm(name) ((void)0)
#endif
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index e8c3084..03389f5 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -1112,8 +1112,14 @@ 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) {
+ /* Careful, do not trash comm of "SCRIPT.sh" -
+ * the case when started from e.g. #!/bin/ash script.
+ * (not limited to shells - #!/bin/awk scripts also exist)
+ */
+ if (re_execed_comm())
+ set_task_comm(applet_name);
+ }
}
parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index a49fe8e..31e9705 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -28,6 +28,22 @@
# ifndef PR_GET_NAME
# define PR_GET_NAME 16
# endif
+# if ENABLE_FEATURE_SH_STANDALONE || ENABLE_FEATURE_PREFER_APPLETS || !BB_MMU
+int FAST_FUNC re_execed_comm(void)
+{
+ const char *e, *expected_comm;
+ char comm[16];
+
+ BUILD_BUG_ON(CONFIG_BUSYBOX_EXEC_PATH[0] != '/');
+ e = CONFIG_BUSYBOX_EXEC_PATH;
+ /* Hopefully (strrchr(e) - e) evaluates to constant at compile time: */
+ expected_comm = bb_busybox_exec_path + (strrchr(e, '/') - e) + 1;
+
+ prctl(PR_GET_NAME, (long)comm, 0, 0, 0);
+ //bb_error_msg("comm:'%.*s' expected:'%s'", 16, comm, expected_comm);
+ return strcmp(comm, expected_comm) == 0;
+}
+# endif
void FAST_FUNC set_task_comm(const char *comm)
{
/* okay if too long (truncates) */
diff --git a/shell/ash_test/ash-comm/comm.right b/shell/ash_test/ash-comm/comm.right
new file mode 100644
index 0000000..1d83665
--- /dev/null
+++ b/shell/ash_test/ash-comm/comm.right
@@ -0,0 +1,6 @@
+./SCRIPT.sh:
+ /proc/N/comm: SCRIPT.sh
+exec ./SCRIPT.sh:
+ /proc/N/comm: SCRIPT.sh
+sh ./SCRIPT.sh:
+ /proc/N/comm: ash
diff --git a/shell/ash_test/ash-comm/comm.tests b/shell/ash_test/ash-comm/comm.tests
new file mode 100755
index 0000000..671bfc1
--- /dev/null
+++ b/shell/ash_test/ash-comm/comm.tests
@@ -0,0 +1,20 @@
+{
+echo "#!$THIS_SH"
+echo 'procdir=/proc/$$'
+#echo 'echo " /proc/N/exe: $(basename $(readlink $procdir/exe))"'
+echo 'echo " /proc/N/comm: $(cat $procdir/comm)"'
+} >SCRIPT.sh
+chmod 755 SCRIPT.sh
+
+# comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y
+echo './SCRIPT.sh:'
+./SCRIPT.sh
+
+# comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y
+echo 'exec ./SCRIPT.sh:'
+(exec ./SCRIPT.sh)
+
+echo 'sh ./SCRIPT.sh:'
+$THIS_SH ./SCRIPT.sh
+
+rm SCRIPT.sh
diff --git a/shell/hush_test/hush-comm/comm.right b/shell/hush_test/hush-comm/comm.right
new file mode 100644
index 0000000..1b62b61
--- /dev/null
+++ b/shell/hush_test/hush-comm/comm.right
@@ -0,0 +1,6 @@
+./SCRIPT.sh:
+ /proc/N/comm: SCRIPT.sh
+exec ./SCRIPT.sh:
+ /proc/N/comm: SCRIPT.sh
+sh ./SCRIPT.sh:
+ /proc/N/comm: hush
diff --git a/shell/hush_test/hush-comm/comm.tests b/shell/hush_test/hush-comm/comm.tests
new file mode 100755
index 0000000..671bfc1
--- /dev/null
+++ b/shell/hush_test/hush-comm/comm.tests
@@ -0,0 +1,20 @@
+{
+echo "#!$THIS_SH"
+echo 'procdir=/proc/$$'
+#echo 'echo " /proc/N/exe: $(basename $(readlink $procdir/exe))"'
+echo 'echo " /proc/N/comm: $(cat $procdir/comm)"'
+} >SCRIPT.sh
+chmod 755 SCRIPT.sh
+
+# comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y
+echo './SCRIPT.sh:'
+./SCRIPT.sh
+
+# comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y
+echo 'exec ./SCRIPT.sh:'
+(exec ./SCRIPT.sh)
+
+echo 'sh ./SCRIPT.sh:'
+$THIS_SH ./SCRIPT.sh
+
+rm SCRIPT.sh