From ed8eb229df581c702e44a352aaf4ed47b64bf623 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 3 Apr 2018 09:40:47 +0100 Subject: ash: reinstate applet name variable after forkshell When 'sh --forkshell' is invoked the BB_APPLET_ environment variable is set, but then it's cleared when the environment of the child process is restored. Reinstate it in reinitvar. --- include/libbb.h | 1 + libbb/appletlib.c | 14 ++++++++------ shell/ash.c | 7 +++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index af234e7b0..da3459224 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -2075,6 +2075,7 @@ extern const char bb_path_wtmp_file[] ALIGN1; #define bb_dev_null "/dev/null" #if ENABLE_PLATFORM_MINGW32 #define bb_busybox_exec_path get_busybox_exec_path() +extern char *bb_applet_pid(void); #else extern const char bb_busybox_exec_path[] ALIGN1; #endif diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 210f9ff4d..1488d4bd6 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -796,6 +796,13 @@ static void install_links(const char *busybox UNUSED_PARAM, static void run_applet_and_exit(const char *name, char **argv) NORETURN; +#if ENABLE_PLATFORM_MINGW32 +char *bb_applet_pid(void) +{ + return auto_string(xasprintf("BB_APPLET_%d=%s", getpid(), applet_name)); +} +#endif + # if ENABLE_BUSYBOX # if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION /* @@ -1008,12 +1015,7 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar check_suid(applet_no); #if ENABLE_PLATFORM_MINGW32 - { - char var[64]; - - sprintf(var, "BB_APPLET_%d=%s", getpid(), applet_name); - putenv(var); - } + putenv(bb_applet_pid()); #endif xfunc_error_retval = applet_main[applet_no](argc, argv); diff --git a/shell/ash.c b/shell/ash.c index 6d24fb55a..2a1ddac39 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -14975,6 +14975,13 @@ reinitvar(void) varinit[i].var_func = varinit_data[i].var_func; } vlineno.var_text = linenovar; + + /* + * BB_APPLET_ was correct when 'sh --forkshell' was started + * but has now been overwritten by the environment from the forkshell + * data block. Reinstate it. + */ + setvareq(bb_applet_pid(), VEXPORT); } /* FIXME: should consider running forkparent() and forkchild() */ -- cgit v1.2.3-55-g6feb