From 41ef232fc522d91f29931ea4ee547432ca8899ee Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 13 Aug 2020 14:27:56 +0100 Subject: win32: use built-in applets for non-existent binaries with Unix-style paths Shell scripts moved from Unix may contain hard-coded paths to binaries such as /bin/sh. A recent commit made it possible to execute such binaries reliably, but that does require them to be installed. As an alternative solution: if a binary with a standard Unix path prefix can't be found but is available as a built-in applet, run the applet. Add the function unix_path() to detect paths starting with /bin, /usr/bin, /sbin or /usr/sbin. Use this function in: - the 'which' applet - shellexec(), describe_command() and find_command() in ash - mingw_spawn_1() See GitHub issue #195. --- shell/ash.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index 0a638b1df..2267e841f 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -8754,6 +8754,9 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) #endif ) { #if ENABLE_PLATFORM_MINGW32 +# if ENABLE_FEATURE_SH_STANDALONE + char *oldprog = prog; +# endif prog = auto_add_system_drive(prog); #endif tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp); @@ -8764,6 +8767,14 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) */ goto try_PATH; } +#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE + if (oldprog != prog && unix_path(oldprog)) { + if ((applet_no = find_applet_by_name(bb_basename(oldprog))) >= 0) + tryexec(applet_no, bb_basename(oldprog), argv, envp); + else + errno = ENOENT; + } +#endif e = errno; } else { try_PATH: @@ -9191,6 +9202,12 @@ describe_command(char *command, const char *path, int describe_command_verbose) case CMDNORMAL: { int j = entry.u.index; char *p; +#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE + if (j == INT_MIN) { + p = (char *)bb_basename(command); + goto describe; + } +#endif if (j < 0) { #if ENABLE_PLATFORM_MINGW32 /* can't use auto_add_system_drive, need space for extension */ @@ -9211,6 +9228,7 @@ describe_command(char *command, const char *path, int describe_command_verbose) #if ENABLE_PLATFORM_MINGW32 add_win32_extension(p); bs_to_slash(p); + IF_FEATURE_SH_STANDALONE(describe:) #endif if (describe_command_verbose) { out1fmt(" is %s", p); @@ -14268,23 +14286,15 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) struct builtincmd *bcmd; int len; +#if !ENABLE_PLATFORM_MINGW32 /* If name contains a slash, don't use PATH or hash table */ -#if ENABLE_PLATFORM_MINGW32 - if (has_path(name)) { - name = auto_add_system_drive(name); -#else if (strchr(name, '/') != NULL) { -#endif entry->u.index = -1; if (act & DO_ABS) { -#if ENABLE_PLATFORM_MINGW32 - if (auto_win32_extension(name) == NULL && stat(name, &statb) < 0) { -#else while (stat(name, &statb) < 0) { #ifdef SYSV if (errno == EINTR) continue; -#endif #endif entry->cmdtype = CMDUNKNOWN; return; @@ -14293,6 +14303,33 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) entry->cmdtype = CMDNORMAL; return; } +#else /* ENABLE_PLATFORM_MINGW32 */ + /* If name contains a slash or drive prefix, don't use PATH or hash table */ + if (has_path(name)) { +# if ENABLE_FEATURE_SH_STANDALONE + char *oldname = name; +# endif + name = auto_add_system_drive(name); + entry->u.index = -1; + if (act & DO_ABS) { + if (auto_win32_extension(name) == NULL && stat(name, &statb) < 0) { +# if ENABLE_FEATURE_SH_STANDALONE + int applet_no; + if (unix_path(oldname) && + (applet_no = find_applet_by_name(bb_basename(oldname))) >= 0) { + entry->cmdtype = CMDNORMAL; + entry->u.index = INT_MIN; + return; + } +# endif + entry->cmdtype = CMDUNKNOWN; + return; + } + } + entry->cmdtype = CMDNORMAL; + return; + } +#endif /* ENABLE_PLATFORM_MINGW32 */ /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ -- cgit v1.2.3-55-g6feb