set bash env variables before running scripts

Bash sources user configuration files under a number of conditions that
can cause issues with scripts when bash is used as the scriptlet shell.

Bash assumes it's being run under rsh/ssh if stdin is connected to a
socket and sources the user bashrc unless the environment variable
$SHLVL is >= 2.  Commit 6a4c6a02de
switched from pipes to sockets when communicating with child processes
to work around SIGPIPE issues.  Normally $SHLVL would be inherited from
the shell running pacman, but operations involving scriptlets are
generally run with sudo which does not let the $SHLVL variable through
unless specifically configured to.

Similarly $BASH_ENV can cause bash to source user-specified configuration
files if set.

https://lists.gnu.org/archive/html/help-bash/2022-02/msg00082.html

Note: the list discussion and bash source all reference SHLVL >= 2, this
is the SHLVL value *after* bash has incremented it on startup.  Setting
it to 1 in pacman is sufficient to disable the unwanted behavior.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
This commit is contained in:
Andrew Gregory 2022-03-12 08:48:42 -08:00
parent 86981383a2
commit da68447ec6

View file

@ -659,6 +659,13 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[],
"/", strerror(errno)); "/", strerror(errno));
exit(1); exit(1);
} }
/* bash assumes it's being run under rsh/ssh if stdin is a socket and
* sources ~/.bashrc if it thinks it's the top-level shell.
* set SHLVL before running to indicate that it's a child shell and
* disable this behavior */
setenv("SHLVL", "1", 0);
/* bash sources $BASH_ENV when run non-interactively */
unsetenv("BASH_ENV");
umask(0022); umask(0022);
_alpm_reset_signals(); _alpm_reset_signals();
execv(cmd, argv); execv(cmd, argv);