diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-10-11 18:39:25 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-10-11 18:44:00 +0200 |
commit | 5acf5e1f870fb5382556d4b434158f7d497893ca (patch) | |
tree | 9200222f648afe4c743c119712e66e38d58e1713 | |
parent | 23aba8a9a66637fbc6b1eed2a259cb7ddb1a1595 (diff) | |
download | busybox-w32-5acf5e1f870fb5382556d4b434158f7d497893ca.tar.gz busybox-w32-5acf5e1f870fb5382556d4b434158f7d497893ca.tar.bz2 busybox-w32-5acf5e1f870fb5382556d4b434158f7d497893ca.zip |
shell: fix script's comm field if ENABLE_FEATURE_PREFER_APPLETS=y
function old new delta
re_execed_comm - 46 +46
main 72 86 +14
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 60/0) Total: 60 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/appletlib.c | 10 | ||||
-rw-r--r-- | libbb/vfork_daemon_rexec.c | 16 | ||||
-rw-r--r-- | shell/ash_test/ash-comm/comm.right | 6 | ||||
-rwxr-xr-x | shell/ash_test/ash-comm/comm.tests | 20 | ||||
-rw-r--r-- | shell/hush_test/hush-comm/comm.right | 6 | ||||
-rwxr-xr-x | shell/hush_test/hush-comm/comm.tests | 20 |
7 files changed, 78 insertions, 2 deletions
diff --git a/include/libbb.h b/include/libbb.h index a340f27d2..2a0b272c6 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_ | |||
1271 | #endif | 1271 | #endif |
1272 | void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; | 1272 | void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; |
1273 | #if defined(__linux__) | 1273 | #if defined(__linux__) |
1274 | int re_execed_comm(void) FAST_FUNC; | ||
1274 | void set_task_comm(const char *comm) FAST_FUNC; | 1275 | void set_task_comm(const char *comm) FAST_FUNC; |
1275 | #else | 1276 | #else |
1277 | # define re_execed_comm() 0 | ||
1276 | # define set_task_comm(name) ((void)0) | 1278 | # define set_task_comm(name) ((void)0) |
1277 | #endif | 1279 | #endif |
1278 | 1280 | ||
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index e8c308467..03389f541 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -1112,8 +1112,14 @@ int main(int argc UNUSED_PARAM, char **argv) | |||
1112 | || ENABLE_FEATURE_PREFER_APPLETS | 1112 | || ENABLE_FEATURE_PREFER_APPLETS |
1113 | || !BB_MMU | 1113 | || !BB_MMU |
1114 | ) { | 1114 | ) { |
1115 | if (NUM_APPLETS > 1) | 1115 | if (NUM_APPLETS > 1) { |
1116 | set_task_comm(applet_name); | 1116 | /* Careful, do not trash comm of "SCRIPT.sh" - |
1117 | * the case when started from e.g. #!/bin/ash script. | ||
1118 | * (not limited to shells - #!/bin/awk scripts also exist) | ||
1119 | */ | ||
1120 | if (re_execed_comm()) | ||
1121 | set_task_comm(applet_name); | ||
1122 | } | ||
1117 | } | 1123 | } |
1118 | 1124 | ||
1119 | parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */ | 1125 | parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */ |
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c index a49fe8e01..31e97051f 100644 --- a/libbb/vfork_daemon_rexec.c +++ b/libbb/vfork_daemon_rexec.c | |||
@@ -28,6 +28,22 @@ | |||
28 | # ifndef PR_GET_NAME | 28 | # ifndef PR_GET_NAME |
29 | # define PR_GET_NAME 16 | 29 | # define PR_GET_NAME 16 |
30 | # endif | 30 | # endif |
31 | # if ENABLE_FEATURE_SH_STANDALONE || ENABLE_FEATURE_PREFER_APPLETS || !BB_MMU | ||
32 | int FAST_FUNC re_execed_comm(void) | ||
33 | { | ||
34 | const char *e, *expected_comm; | ||
35 | char comm[16]; | ||
36 | |||
37 | BUILD_BUG_ON(CONFIG_BUSYBOX_EXEC_PATH[0] != '/'); | ||
38 | e = CONFIG_BUSYBOX_EXEC_PATH; | ||
39 | /* Hopefully (strrchr(e) - e) evaluates to constant at compile time: */ | ||
40 | expected_comm = bb_busybox_exec_path + (strrchr(e, '/') - e) + 1; | ||
41 | |||
42 | prctl(PR_GET_NAME, (long)comm, 0, 0, 0); | ||
43 | //bb_error_msg("comm:'%.*s' expected:'%s'", 16, comm, expected_comm); | ||
44 | return strcmp(comm, expected_comm) == 0; | ||
45 | } | ||
46 | # endif | ||
31 | void FAST_FUNC set_task_comm(const char *comm) | 47 | void FAST_FUNC set_task_comm(const char *comm) |
32 | { | 48 | { |
33 | /* okay if too long (truncates) */ | 49 | /* 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 000000000..1d836656f --- /dev/null +++ b/shell/ash_test/ash-comm/comm.right | |||
@@ -0,0 +1,6 @@ | |||
1 | ./SCRIPT.sh: | ||
2 | /proc/N/comm: SCRIPT.sh | ||
3 | exec ./SCRIPT.sh: | ||
4 | /proc/N/comm: SCRIPT.sh | ||
5 | sh ./SCRIPT.sh: | ||
6 | /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 000000000..671bfc176 --- /dev/null +++ b/shell/ash_test/ash-comm/comm.tests | |||
@@ -0,0 +1,20 @@ | |||
1 | { | ||
2 | echo "#!$THIS_SH" | ||
3 | echo 'procdir=/proc/$$' | ||
4 | #echo 'echo " /proc/N/exe: $(basename $(readlink $procdir/exe))"' | ||
5 | echo 'echo " /proc/N/comm: $(cat $procdir/comm)"' | ||
6 | } >SCRIPT.sh | ||
7 | chmod 755 SCRIPT.sh | ||
8 | |||
9 | # comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y | ||
10 | echo './SCRIPT.sh:' | ||
11 | ./SCRIPT.sh | ||
12 | |||
13 | # comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y | ||
14 | echo 'exec ./SCRIPT.sh:' | ||
15 | (exec ./SCRIPT.sh) | ||
16 | |||
17 | echo 'sh ./SCRIPT.sh:' | ||
18 | $THIS_SH ./SCRIPT.sh | ||
19 | |||
20 | 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 000000000..1b62b617e --- /dev/null +++ b/shell/hush_test/hush-comm/comm.right | |||
@@ -0,0 +1,6 @@ | |||
1 | ./SCRIPT.sh: | ||
2 | /proc/N/comm: SCRIPT.sh | ||
3 | exec ./SCRIPT.sh: | ||
4 | /proc/N/comm: SCRIPT.sh | ||
5 | sh ./SCRIPT.sh: | ||
6 | /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 000000000..671bfc176 --- /dev/null +++ b/shell/hush_test/hush-comm/comm.tests | |||
@@ -0,0 +1,20 @@ | |||
1 | { | ||
2 | echo "#!$THIS_SH" | ||
3 | echo 'procdir=/proc/$$' | ||
4 | #echo 'echo " /proc/N/exe: $(basename $(readlink $procdir/exe))"' | ||
5 | echo 'echo " /proc/N/comm: $(cat $procdir/comm)"' | ||
6 | } >SCRIPT.sh | ||
7 | chmod 755 SCRIPT.sh | ||
8 | |||
9 | # comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y | ||
10 | echo './SCRIPT.sh:' | ||
11 | ./SCRIPT.sh | ||
12 | |||
13 | # comm field was wrong if CONFIG_FEATURE_PREFER_APPLETS=y | ||
14 | echo 'exec ./SCRIPT.sh:' | ||
15 | (exec ./SCRIPT.sh) | ||
16 | |||
17 | echo 'sh ./SCRIPT.sh:' | ||
18 | $THIS_SH ./SCRIPT.sh | ||
19 | |||
20 | rm SCRIPT.sh | ||