Don't fallback to /bin/bash when exiting with a failure return code

If the last command that was run interactively inside the toolbox
container had returned with a non-zero return code, then exiting the
toolbox would trigger the fallback to /bin/bash, just like it would
happen if $SHELL was missing from the toolbox. This is because
'podman exec ...' relays the return code of the last command.

Therefore, don't rely on the return code of 'podman exec ...' and check
the availability of $SHELL upfront. This does leave it vulnerable to
races caused by the availability of $SHELL changing between the check
and the actual attempt to use it. However, file I/O is inherently racy,
and this is better than a spurious fallback.

Keep the name of the default interactive shell localized to 'enter' by
using a subshell because 'local' is not mandated by POSIX.
This commit is contained in:
Debarshi Ray 2018-11-12 13:38:44 +00:00
parent be719eac89
commit 606c2da224

View file

@ -165,7 +165,9 @@ create()
enter()
{
(
shell_to_exec=/bin/bash
if ! $prefix_sudo podman start $toolbox_container >/dev/null 2>&42; then
echo "$0: failed to start container $toolbox_container"
exit 1
@ -175,61 +177,38 @@ enter()
set_dbus_system_bus_address="--env DBUS_SYSTEM_BUS_ADDRESS=$DBUS_SYSTEM_BUS_ADDRESS"
fi
if ! $prefix_sudo podman exec \
--env COLORTERM=$COLORTERM \
--env DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
$set_dbus_system_bus_address \
--env DESKTOP_SESSION=$DESKTOP_SESSION \
--env DISPLAY=$DISPLAY \
--env LANG=$LANG \
--env PS1="$toolbox_prompt" \
--env SHELL=$SHELL \
--env SSH_AUTH_SOCK=$SSH_AUTH_SOCK \
--env TERM=$TERM \
--env VTE_VERSION=$VTE_VERSION \
--env XDG_CURRENT_DESKTOP=$XDG_CURRENT_DESKTOP \
--env XDG_DATA_DIRS=$XDG_DATA_DIRS \
--env XDG_MENU_PREFIX=$XDG_MENU_PREFIX \
--env XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR \
--env XDG_SEAT=$XDG_SEAT \
--env XDG_SESSION_DESKTOP=$XDG_SESSION_DESKTOP \
--env XDG_SESSION_ID=$XDG_SESSION_ID \
--env XDG_SESSION_TYPE=$XDG_SESSION_TYPE \
--env XDG_VTNR=$XDG_VTNR \
--interactive \
--tty \
$toolbox_container \
$SHELL -l 2>&42; then
if ! $prefix_sudo podman exec \
--env COLORTERM=$COLORTERM \
--env DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
$set_dbus_system_bus_address \
--env DESKTOP_SESSION=$DESKTOP_SESSION \
--env DISPLAY=$DISPLAY \
--env LANG=$LANG \
--env PS1="$toolbox_prompt" \
--env SHELL=$SHELL \
--env SSH_AUTH_SOCK=$SSH_AUTH_SOCK \
--env TERM=$TERM \
--env VTE_VERSION=$VTE_VERSION \
--env XDG_CURRENT_DESKTOP=$XDG_CURRENT_DESKTOP \
--env XDG_DATA_DIRS=$XDG_DATA_DIRS \
--env XDG_MENU_PREFIX=$XDG_MENU_PREFIX \
--env XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR \
--env XDG_SEAT=$XDG_SEAT \
--env XDG_SESSION_DESKTOP=$XDG_SESSION_DESKTOP \
--env XDG_SESSION_ID=$XDG_SESSION_ID \
--env XDG_SESSION_TYPE=$XDG_SESSION_TYPE \
--env XDG_VTNR=$XDG_VTNR \
--interactive \
--tty \
$toolbox_container \
/bin/bash -l 2>&42; then
echo "$0: failed to exec $SHELL inside container $toolbox_container"
exit 1
fi
if $prefix_sudo podman exec $toolbox_container test -f $SHELL 2>&42; then
shell_to_exec=$SHELL
else
echo "$SHELL not found in $toolbox_container; using $shell_to_exec instead" >&42
fi
}
$prefix_sudo podman exec \
--env COLORTERM=$COLORTERM \
--env DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
$set_dbus_system_bus_address \
--env DESKTOP_SESSION=$DESKTOP_SESSION \
--env DISPLAY=$DISPLAY \
--env LANG=$LANG \
--env PS1="$toolbox_prompt" \
--env SHELL=$SHELL \
--env SSH_AUTH_SOCK=$SSH_AUTH_SOCK \
--env TERM=$TERM \
--env VTE_VERSION=$VTE_VERSION \
--env XDG_CURRENT_DESKTOP=$XDG_CURRENT_DESKTOP \
--env XDG_DATA_DIRS=$XDG_DATA_DIRS \
--env XDG_MENU_PREFIX=$XDG_MENU_PREFIX \
--env XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR \
--env XDG_SEAT=$XDG_SEAT \
--env XDG_SESSION_DESKTOP=$XDG_SESSION_DESKTOP \
--env XDG_SESSION_ID=$XDG_SESSION_ID \
--env XDG_SESSION_TYPE=$XDG_SESSION_TYPE \
--env XDG_VTNR=$XDG_VTNR \
--interactive \
--tty \
$toolbox_container \
$shell_to_exec -l 2>&42
)
exit_if_extra_operand()