diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-10-07 15:18:45 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-10-07 15:18:45 +0200 |
commit | baa49bdc1b7b992bfb8298589cd33c04353c6b57 (patch) | |
tree | 75239fc9db31805f11281471ed006ddf91bce208 | |
parent | 0929a129fc75c556de67877491281e0bc3ef3edd (diff) | |
download | busybox-w32-baa49bdc1b7b992bfb8298589cd33c04353c6b57.tar.gz busybox-w32-baa49bdc1b7b992bfb8298589cd33c04353c6b57.tar.bz2 busybox-w32-baa49bdc1b7b992bfb8298589cd33c04353c6b57.zip |
hush: make "test -x" use cached groupinfo
While at it, correct "type" to skip non-executable files in PATH
function old new delta
builtin_source 211 316 +105
builtin_test 10 32 +22
hush_main 1150 1170 +20
builtin_type 122 137 +15
if_command_vV_print_and_exit 120 114 -6
find_in_path 131 - -131
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 4/1 up/down: 162/-137) Total: 25 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/hush.c | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/shell/hush.c b/shell/hush.c index 6b6ec7c6b..074f35f2b 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -1022,6 +1022,13 @@ struct globals { | |||
1022 | #if HUSH_DEBUG >= 2 | 1022 | #if HUSH_DEBUG >= 2 |
1023 | int debug_indent; | 1023 | int debug_indent; |
1024 | #endif | 1024 | #endif |
1025 | #if ENABLE_HUSH_TEST || BASH_TEST2 | ||
1026 | /* Cached supplementary group array (for testing executable'ity of files) */ | ||
1027 | struct cached_groupinfo groupinfo; | ||
1028 | # define GROUPINFO_INIT { G.groupinfo.euid = -1; G.groupinfo.egid = -1; } | ||
1029 | #else | ||
1030 | # define GROUPINFO_INIT /* nothing */ | ||
1031 | #endif | ||
1025 | struct sigaction sa; | 1032 | struct sigaction sa; |
1026 | char optstring_buf[sizeof("eixcs")]; | 1033 | char optstring_buf[sizeof("eixcs")]; |
1027 | #if BASH_EPOCH_VARS | 1034 | #if BASH_EPOCH_VARS |
@@ -1040,6 +1047,7 @@ struct globals { | |||
1040 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ | 1047 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
1041 | sigfillset(&G.sa.sa_mask); \ | 1048 | sigfillset(&G.sa.sa_mask); \ |
1042 | G.sa.sa_flags = SA_RESTART; \ | 1049 | G.sa.sa_flags = SA_RESTART; \ |
1050 | GROUPINFO_INIT; \ | ||
1043 | } while (0) | 1051 | } while (0) |
1044 | 1052 | ||
1045 | /* Function prototypes for builtins */ | 1053 | /* Function prototypes for builtins */ |
@@ -8193,6 +8201,8 @@ static int setup_redirects(struct command *prog, struct squirrel **sqp) | |||
8193 | return 0; | 8201 | return 0; |
8194 | } | 8202 | } |
8195 | 8203 | ||
8204 | /* Find a file in PATH, not necessarily executable */ | ||
8205 | //TODO: shares code with find_executable() in libbb, factor out? | ||
8196 | static char *find_in_path(const char *arg) | 8206 | static char *find_in_path(const char *arg) |
8197 | { | 8207 | { |
8198 | char *ret = NULL; | 8208 | char *ret = NULL; |
@@ -8635,7 +8645,7 @@ static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *exp | |||
8635 | 8645 | ||
8636 | to_free = NULL; | 8646 | to_free = NULL; |
8637 | if (!explanation) { | 8647 | if (!explanation) { |
8638 | char *path = getenv("PATH"); | 8648 | char *path = (char*)get_local_var_value("PATH"); |
8639 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ | 8649 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
8640 | if (!explanation) | 8650 | if (!explanation) |
8641 | _exit(1); /* PROG was not found */ | 8651 | _exit(1); /* PROG was not found */ |
@@ -10870,7 +10880,8 @@ static NOINLINE int run_applet_main(char **argv, int (*applet_main_func)(int arg | |||
10870 | #if ENABLE_HUSH_TEST || BASH_TEST2 | 10880 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
10871 | static int FAST_FUNC builtin_test(char **argv) | 10881 | static int FAST_FUNC builtin_test(char **argv) |
10872 | { | 10882 | { |
10873 | return run_applet_main(argv, test_main); | 10883 | int argc = string_array_len(argv); |
10884 | return test_main2(&G.groupinfo, argc, argv); | ||
10874 | } | 10885 | } |
10875 | #endif | 10886 | #endif |
10876 | #if ENABLE_HUSH_ECHO | 10887 | #if ENABLE_HUSH_ECHO |
@@ -11063,12 +11074,16 @@ static int FAST_FUNC builtin_type(char **argv) | |||
11063 | # endif | 11074 | # endif |
11064 | else if (find_builtin(*argv)) | 11075 | else if (find_builtin(*argv)) |
11065 | type = "a shell builtin"; | 11076 | type = "a shell builtin"; |
11066 | else if ((path = find_in_path(*argv)) != NULL) | ||
11067 | type = path; | ||
11068 | else { | 11077 | else { |
11069 | bb_error_msg("type: %s: not found", *argv); | 11078 | char *pathvar = (char*)get_local_var_value("PATH"); |
11070 | ret = EXIT_FAILURE; | 11079 | path = find_executable(*argv, &pathvar); |
11071 | continue; | 11080 | if (path) |
11081 | type = path; | ||
11082 | else { | ||
11083 | bb_error_msg("type: %s: not found", *argv); | ||
11084 | ret = EXIT_FAILURE; | ||
11085 | continue; | ||
11086 | } | ||
11072 | } | 11087 | } |
11073 | 11088 | ||
11074 | printf("%s is %s\n", *argv, type); | 11089 | printf("%s is %s\n", *argv, type); |